All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

How To Sort A List In Python Without Sort Function

Last Updated : Mar 11, 2024

How To Sort A List In Python Without Sort Function

In this tutorial we will show you the solution of how to sort a list in python without sort function, when working as a programmer, there might be a requirement that needs some data to be sorted.

It is a very common and easy task to do with some built-in methods in some programming languages.

Without using any built-in methods to perform sorting, it can be a little difficult to do in some programming languages. So let’s get started.

Step By Step Guide On How To Sort A List In Python Without Sort Function :-

Python provides a built-in function named sort() to ease the task of sorting while working as a programmer.

You can do the sorting without the built-in sort() function as well, which is provided by Python.

#First example

originalList1 = [2, -1, -2, 0, 1]
for i in range(len(originalList1)):
    for j in range(i + 1, len(originalList1)):
        if originalList1[i] > originalList1[j]:
            originalList1[i], originalList1[j] = originalList1[j], originalList1[i]
print(originalList1)

#Second example

originalList2 = [2, -1, -2, 0, 1]
for i in originalList2[1:]:
    j = originalList2.index(i)
    while j > 0 and originalList2[j-1] < originalList2[j]:
        originalList2[j], originalList2[j-1] = originalList2[j-1], originalList2[j]
        j = j - 1
print(originalList2)
  1. The first line is a comment saying “first example”, which is the first method you can use to sort a list of elements in Python.
  2. In the first example, we will perform sorting in ascending order, and in the second example, we will perform sorting in descending order.
  3. In the next line, there is a variable called the original_list1, which is a list and has some integer values that are unordered.
  4. In the next line, there is a for loop which will iterate through the whole list n times, where n is the number of elements in the list. Here it is 5. So the for loop will iterate five times.
  5. In the next line, there is another for loop, which is inside of another for loop. This is called the nested for loop. This for loop will start from the second element of the list and iterate through all the elements in the list.
  6. The inner for loop will iterate 5 times for each iteration of the outer for loop to do the sorting.
  7. In the inner for loop, there is an if condition that will check whether to swap the elements or not.
  8. If the first element is bigger than the second element, then swap the elements; otherwise, do not swap the elements.
  9. This technic is known as the bubble sort where you compare each element with other elements to perform sorting.
  10. In the next line the swapping takes place between two elements which needed to be swapped.
  11. The next line is a print statement which will print the sorted list as the output.
  12. In the next line there is another comment saying “Second example”, which is very similar to the above mentioned approach, with the slight difference of some functions that are not used.
  13. In the next line, there is a variable named originalList2, which is a list that contains some integer values in it.
  14. In the next line there is a for loop which will iterate through all the elements. It is similar to above mentioned for loop but without the range() and len() functions.
  15. The next has a variable which will hold the value of the index() function. The Python index() function helps you find the index position of an element.
  16. In the next line there is a while loop which will check the condition of whether to swap the elements or not.
  17. If the condition is true, then swapping will be done; otherwise, swapping will not take place.
  18. In the next line, the swapping takes place between two elements. After that, the value of the variable j will be decremented by 1.
  19. The last line is the print statement, which will display the resultant list sorted in descending order.

Conclusion :-

So finally, in conclusion, we can say that with the help of this article, you can now sort a list without using the built-in sort() function in a Python program.

I hope this this tutorial on how to sort a list in python without sort function helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Pragati

Experienced coding content writer who enjoys breaking down complex concepts in programming languages like Java, Python, C, and C++. Over three years of experience producing interesting and relevant content for a variety of entities. Committed to providing concise and easy-to-understand articles that assist readers in easily understanding technology and industry trends in the world of coding and software development.

Follow Pragati On Linkedin 🡪