How To Sort A List In Python Without Sort Function
Last Updated : Jan 1, 2023
IN - Python
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.
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)
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)
- 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.
- In the first example, we will perform sorting in ascending order, and in the second example, we will perform sorting in descending order.
- In the next line, there is a variable called the original_list1, which is a list and has some integer values that are unordered.
- 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.
- 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.
- The inner for loop will iterate 5 times for each iteration of the outer for loop to do the sorting.
- In the inner for loop, there is an if condition that will check whether to swap the elements or not.
- If the first element is bigger than the second element, then swap the elements; otherwise, do not swap the elements.
- This technic is known as the bubble sort where you compare each element with other elements to perform sorting.
- In the next line the swapping takes place between two elements which needed to be swapped.
- The next line is a print statement which will print the sorted list as the output.
- 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.
- In the next line, there is a variable named originalList2, which is a list that contains some integer values in it.
- 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.
- 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.
- In the next line there is a while loop which will check the condition of whether to swap the elements or not.
- If the condition is true, then swapping will be done; otherwise, swapping will not take place.
- In the next line, the swapping takes place between two elements. After that, the value of the variable j will be decremented by 1.
- The last line is the print statement, which will display the resultant list sorted in descending order.
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.