How to Sort a List in Python without sort function

You can use the sort() function on the list to sort the elements of the list in ascending order. But if you want to sort a List in Python without the sort() function then this tutorial is for you. In this tutorial, you will know How to Sort a List in Python without the sort function using for loop with steps.

Steps on How to Sort a List in Python without sort function

Step 1: Create a sample list

To create a sample list you will use the square bracket. In this list, the elements are randomly positioned.

Use the below code to create a list.

sample_list = [30, 10, 20,5]

Step 2: Use the two nested loop

Now the next step is to use the two nested loops. Use two nested loops to iterate over each element in the list and compare it with every other element.

Use the below lines of code to iterate the list and sort its elements.

for i in range(len(sample_list) - 1):
    # Inner loop for comparisons
    for j in range(len(sample_list) - i - 1):
        # Compare adjacent elements and swap if needed
        if sample_list[j] > sample_list[j + 1]:
            temp = sample_list[j]
            sample_list[j] = sample_list[j + 1]
            sample_list[j + 1] = temp

Explanation

Using the range function with the argument len(my_list) – 1 in the outer loop will control the number of passes needed since the last element will have already been put in its proper place after the preceding passes.

In the same way, using the range function with the argument len(my_list) – i – 1 in the inner loop, we can compare adjacent elements in the list, knowing that the last i elements are already in their correct positions.

Now Compare two elements side by side using an “if” statement. If the first element is larger than the second, use a temporary variable (“temp”) to exchange their positions.

After the loops are complete, the list will be sorted in ascending order.

Full code

sample_list = [30, 10, 20,5]
for i in range(len(sample_list) - 1):
    # Inner loop for comparisons
    for j in range(len(sample_list) - i - 1):
        # Compare adjacent elements and swap if needed
        if sample_list[j] > sample_list[j + 1]:
            temp = sample_list[j]
            sample_list[j] = sample_list[j + 1]
            sample_list[j + 1] = temp

print("The sorted list is:", sample_list)

Output

sorting the list without the sort function
sorting the list without the sort function

Conclusion

These are the steps you will use to Sort a List in Python without the sort() function.  Here you have used for loop instead. But you should keep in mind that don’t use this approach for large lists as it is not efficient. It’s the worst-case time complexity of O(n^2). Therefore the better and more efficient way to sort the list is the sort() function.

Hi, I am CodeTheBest. Here you will learn the best coding tutorials on the latest technologies like a flutter, react js, python, Julia, and many more in a single place.

SPECIAL OFFER!

This Offer is Limited! Grab your Discount!
15000 ChatGPT Prompts
Offer Expires In:
close-link