How to Sort a List in Python Without Using Sort Function

How to Sort a List in Python Without Using Sort Function
Tutorial Playlist

Sorting is an essential concept in Python that helps in organizing the data efficiently. It is very important for managing large datasets and improving performance, which makes handling the information easier. Different sorting techniques can be used for different data types based on your requirements. Python provides multiple sorting techniques for handling the data efficiently. In this article, you will explore different ways to sort a list in Python without using the sort function with examples for each in detail.

Table of Contents:

Check out this YouTube video specially designed for Python beginners.

Video Thumbnail

What is Sorting?

Sorting is the process of arranging the elements in a particular order based on the requirements, like arranging the numbers from smallest to largest or words from A to Z. In Python, sorting helps to organize the data, which makes it easier to find and access when needed. Sorting can be in both ascending and descending order. It is commonly used for searching the data and analyzing the information.

There are several methods for sorting the data in Python, which can be done using sorting techniques like quick sort, bubble sort, selection sort, and insertion sort, and it can also be done using the basic sort() function. Sorting is crucial for developers and data scientists working with huge datasets; therefore sorting is a crucial topic in Python, as it helps in effective data processing and retrieval

Learn more about Python from this Python Data Science Course to get ahead in your career!

How to Sort List in Python Without Using Sort Function

Sorting a list without using the sort() function allows programmers to have more control over the arrangement of the data. Sometimes, there would be a need to customize sorting based on specific conditions, which may not always be possible using the built-in function. Learning different manual sorting methods in Python helps you to understand how the sorting algorithms work, which helps in improving your problem-solving skills. They are particularly useful while working on large datasets, where using the right method helps in sorting the data quickly and efficiently.

Practicing sorting without the sort() function helps programmers understand the sorting algorithms better. This helps sort the database records, rank the searched results, or organize large sets of datasets in a particular order in real-life situations

Below are some techniques for sorting a list in Python without using the sort function:

Get 100% Hike!

Master Most in Demand Skills Now!

Using a for-loop in Python

A for loop sorts a list by checking each number, comparing it with the next, and swapping them if needed. This continues until the entire list is sorted.”

Ascending Order

Now, let’s see how the list can be sorted in ascending order without using the sort function.

Example:

Python

Output:

Explanation: Here, the user enters numbers with spaces between them. The program sorts these numbers in ascending order using the for loop and displays the sorted list.

Descending Order

Now let’s sort a list in descending order using a for loop, without using the sort() function.

Python

Output:

Explanation: Here, a predefined list is sorted in descending order using the for loop, and the sorted list is displayed.

Prepare like a pro with our comprehensive Python interview questions and answers guide!

Using While Loop in Python

A while loop sorts a list by repeatedly moving numbers to their correct positions until the entire list is in order.

Ascending Order

Let’s sort a list in ascending order using a while loop, without using the sort() function.

Example:

Python

Output:

Explanation: Here, the user enters numbers with spaces between them. The program sorts these numbers in ascending order using a while loop and displays the sorted list.

Descending Order

Let’s sort a list in descending order using a while loop, without using the sort() function.

Python

Output:

Explanation: Here, the predefined list is sorted in descending order using a while loop and then displayed.

Using Slicing in Python

The slicing method breaks the list into smaller parts, arranges them in the required manner, and combines them to create a sorted list.

Ascending Order 

Let’s sort a list in ascending order using the slicing method, without using the sort() function.

Example:

Python

Output:

Explanation: Here, the slicing method removes the smallest number from the original list and adds it to a new list until all numbers are sorted.

Descending Order

Let’s sort a list in descending order using the slicing method, without using the sort() function.

Example:

Python

Output:

Explanation: Here, the while loop sorts the predefined list in ascending order, and then the sorted list is displayed.

Using the pop Method in Python

The pop method picks the smallest number from the list one by one and places it in a new list until all the numbers are sorted according to the condition.

Ascending Order

Firstly, let us sort the list in ascending order using the pop method, without using the sort() function.

Python

Output:

Explanation: Here, the pop method sorts the predefined list in ascending order, and then the sorted list is displayed.

Descending Order

Let’s sort a list in descending order using the pop method, without using the sort() function.

Example:

Python

Output:

Explanation: Here, the pop method sorts the predefined list in descending order, and then the sorted list is displayed.

Get ready for the high-paying Data Scientist jobs with these Top Data Science Interview Questions and Answers!

Sorting Different Data Types Without Using Sort Function

Sorting Sets Without Using the Sort Function

A set is an unordered collection of data, so first the set needs to be converted into a list, sort the converted list manually, and then converted back to a set.

Example:

Python

Output:

Explanation: Here, the predefined set is converted to a list, sorted using loops, and then converted back to a set before displaying it.

Sorting Tuples Without Using the Sort Function

As the tuples are immutable, first you need to convert them into a list, sort them manually, and then convert them back into a tuple.

Example:

Python

Output:

Explanation: Here, the predefined tuple is converted to a list, which is sorted using loops, and converted back to a tuple before displaying.

Sorting Dictionaries Without Using the Sort Function

The dictionaries can be sorted by first extracting the keys or values to sort them manually, then a new dictionary is created with the sorted elements.

Example:

Python

Output:

Explanation: Here, the predefined dictionary keys are sorted manually, and a new dictionary is created in sorted order before displaying.

Alternative Sorting Methods in Python

Using sorted() to Sort in Python

The sorted() function creates a new sorted list without changing the original. This is very helpful when you need a sorted version but still want to keep the original list as it is.

Example:

Python

Output:

Explanation: Here, the sorted() function creates a new list with sorted elements while keeping the original list unchanged.

Using NumPy to Sort in Python

NumPy is a library in Python that is mainly used for numerical computing. It has a numpy.sort() function that sorts arrays efficiently. It is very helpful when you are working on large datasets to improve the efficiency of sorting

Example:

Python

Output:

Explanation: Here, the NumPy sort() function sorts the elements in ascending order efficiently. It works best for numerical data.

Using Recursion and List Comprehension to Sort in Python

Recursion is a method where a function calls itself repeatedly until a condition is met. List comprehension is a way to create lists in a single line. By combining both, we can sort a list step by step.

Example:

Python

Output:

Explanation: Here, recursion repeatedly finds the smallest number in the list and uses list comprehension to create a new list without that number, sorting the list step by step.

Eight Advanced Sorting Techniques in Python

There are different sorting techniques in Python, and each has its own way of working. The best method being used depends on the type of data you are sorting and the amount of sorting that is required. Below are some advanced sorting techniques without using the built-in sort() function. sorting techniques:

1. Bubble Sort
Bubble Sort focuses on locating the minimum value from the list to properly position it. The procedure continues until every minimum number obtains its proper placement. It is similar to sorting the playing cards by comparing two at a time. It moves through the list, checking two numbers at a time. If the arrangement is wrong, it switches them.

Python

Output:

Explanation: Here, numbers are compared and swapped if they are in the wrong order. This repeats until the list is sorted. The largest number moves to the right in each round, which is similar to the bubbles rising to the top.

2. Selection Sort
The selection sort works by finding the smallest number in the list and placing it in the correct position. This process is repeated till all the smallest numbers are found and are placed in the correct position.

Python

Output:

Explanation: Here, the smallest number is picked and placed in the right position, then continues to pick, and the numbers will eventually be completely in order.

3. Insertion Sort
Insertion sort is very similar to the above procedure of organizing numbers, as if you were sorting the cards in your hand. The algorithm will look at a number, compare it to the numbers it has already sorted, find one position to insert where it fits properly into the numbers that are sorted one time, and will continue to insert the other numbers until it has sorted through all of the numbers in the list.

Explanation:

Python

Output:

Explanation: Here, the numbers are sorted at one time and pushed over by the larger numbers as they go into the proper position.

4. Merge Sort
Merge Sort is a sort algorithm that helps by breaking the list into smaller parts with the list sorted and then merging them back together to form a completely new sorted list. The smaller segments they sorted, and once the combined segments are merged back together, they will form a completely sorted list.

Python

Output:

Explanation: Here, the process is breaking the list into smaller lists, and then sorting the numbers in each one. Once sorted, they will come together in sorted order.

5. Quick Sort
Quick Sort will select an item as “pivot” in the list, place that value in the middle, then place all of the items that are less than the pivot on the left side of the pivot, and items greater than the pivot on the right side of the pivot. This is done repeatedly until all the items in the list are sorted.

Python

Output:

Explanation:  Here, numbers are divided into two groups, one containing smaller numbers and the other containing larger numbers according to the pivot.

6. Heap Sort
Heap Sort converts the list to a tree-like structure called a “heap”, which helps in efficient sorting of different values. The largest number is removed from the heap and placed into the sorted list one at a time.

Python

Output:

Explanation: Here, the list is put into a heap, which makes sorting easy. The minimum number is sequentially removed, one each time, until the list is sorted.

7. Counting Sort
Counting Sort is useful when sorting numbers that occur in small ranges of numbers. Rather than comparing numbers. It sorts the numbers in a list based on the frequency of the number occurring. It is very fast but only functions well with bounded ranges of integers.

Python

Output:

Explanation:  Here, a list is built based on counting the numbers and sorting the elements. It is beneficial for sorting duplicate low numbers very quickly.

8. Radix Sort
Radix Sort is a sorting technique where the list of numbers is sorted at the digit level from low to high, from the lowest value to the highest value. It performs well on larger lists.

Python

Output:

Explanation: Here, the numbers are sorted from right to left, according to the digit. It sorts large numbers well.

Best Practices for Efficient Sorting Without Sort Function in Python

Sorting Without Sort in Python is necessary for ordering the data correctly, but if there is any mistake in the sorting will make the process of sorting inefficient. Let us discuss some points that you must consider before sorting the list.

  • Optimize loop conditions: It is useful to remove and change the comparisons involved in a loop to sort as fast as you can.
  • Minimize the number of swaps: Minimize the number of swaps and stop swaps when it is not necessary.
  • Use less memory: The sorting is more efficient on a single list rather than separate lists. This helps with reducing memory usage. 
  • Divide and conquer large problems: Instead of sorting the bigger list, you can consider merge sort and quick sort to break the bigger list down into parts, which speeds the sorting process and makes it efficient.
  • Verify whether the list has already been sorted: It’s important to check if the list is already sorted before sorting the list, which is very helpful in saving time.

Real-World Use Cases of Sorting in Python

Sorting in File Systems

Sorting helps in organizing the files by name, date, or type. This makes searching the files easier and helps operating systems manage files better in a simple manner.

Example:

Python

Output:

Explanation: Here, sample files are created with different timestamps assigned to them, and then sorted based on the modification date; the last opened file is shown first.

Sorting in E-commerce Websites

Online stores use sorting to arrange the products based on price, ratings, discount, and popularity, based on the customer’s requirements, which helps the customers find what they need easily. This helps improve user experience and sales of the online store.

Example:

Python

Output:

Explanation: Here, the products in the online e-commerce store are sorted based on the price in ascending order, where the cheapest product is displayed first.

Conclusion

Sorting lists in Python without the sort function helps in managing the data better and gives more control over it. It also enhances your understanding of the advanced sorting algorithms. Learning the sorting methods helps you to create faster solutions based on your needs, and helps improve the searches, handling large datasets, or sorting important records.

To take your skills to the next level by enrolling in our Python training course and gaining hands-on experience. Also, prepare for job interviews with our Python developer interview questions, prepared by industry experts.

FAQs

Q1. Why is sorting important in Python?

Sorting organizes data, making it easier to search, retrieve, and analyze. It is widely used in databases, data science, and ranking search results.

Q2. Can we sort a list without sort()?

Yes, a list can be sorted manually using loops, swapping, or sorting algorithms like Bubble Sort, Selection Sort, and Insertion Sort.

Q3. What are the benefits of sorting manually?

It gives more control, helps understand sorting algorithms, and allows customization that the built-in sort() function may not support.

Q4. Which methods can replace sort()?

Some common techniques include Bubble Sort, Selection Sort, Insertion Sort, Merge Sort, and Quick Sort for arranging data.

Q5. When should I sort manually?

Manual sorting is useful for learning algorithms, customizing sorting for large datasets, or solving specific coding problems

Our Python Courses Duration and Fees

Program Name
Start Date
Fees
Cohort Starts on: 12th Apr 2025
₹20,007
Cohort Starts on: 19th Apr 2025
₹20,007

About the Author

Senior Consultant Analytics & Data Science

Sahil Mattoo, a Senior Software Engineer at Eli Lilly and Company, is an accomplished professional with 14 years of experience in languages such as Java, Python, and JavaScript. Sahil has a strong foundation in system architecture, database management, and API integration.