Sort a list in Python without sort Function
Last Updated :
24 Aug, 2023
Python Lists are a type of data structure that is mutable in nature. This means that we can modify the elements in the list. We can sort a list in Python using the inbuilt list sort() function. But in this article, we will learn how we can sort a list in a particular order without using the list sort() method.
Sort a List Without Using Sort Function in Python
Sorting is nothing but arranging the elements of a list in a particular order and this order can be either increasing or decreasing in nature. We can sort a list in Python without using the list sort() function by using nested for loops. Let us see a few examples, of how we can sort a list without the sort() function.
Sort a List in Ascending Order Without using an Extra Variable
In this example, we will have a list and we will sort it in ascending order using just the nested loop. We will compare each element in the list one by one and swap it.
Python3
#Python program to print a list
# without using the sort() function
# without an extra variable
l1=[76, 23, 45, 12, 54, 9]
print("Original List:", l1)
# sorting list using nested loops
for i in range(0, len(l1)):
for j in range(i+1, len(l1)):
if l1[i] >= l1[j]:
l1[i], l1[j] = l1[j],l1[i]
# sorted list
print("Sorted List", l1)
Output:
Original List: [76, 23, 45, 12, 54, 9]
Sorted List [9, 12, 23, 45, 54, 76]
Sort a List in Ascending Order by Using an extra variable
In this example, we will have a list and we will sort it in ascending order by using the nested for loop and a temporary variable.
Python3
#Python program to print a list
# without using the sort() function
# using an extra variable
l1=[76,23,45,12,54,9]
print("Original List:", l1)
# sorting list using nested loops
for i in range(0, len(l1)):
for j in range(i+1, len(l1)):
if l1[i] >= l1[j]:
# temporary variable
temp = l1[i]
l1[i] = l1[j]
l1[j] = temp
# sorted list
print("Sorted List", l1)
Output:
Original List: [76, 23, 45, 12, 54, 9]
Sorted List [9, 12, 23, 45, 54, 76]
Sort a List in Descending Order
In this example, instead of a predefined list, we will take a list whose values are provided by the user. Then we will sort the list in descending order without using an extra variable.
Python3
# create an empty list
l1 = []
n = int(input("enter number of elements required: "))
# creating a list using loop
for i in range(0, n):
element = int(input())
# appending elements to the list
l1.append(element)
print("Original List:", l1)
# sorting in decending
for i in range(0, len(l1)):
for j in range(i+1, len(l1)):
if l1[i] <= l1[j]:
l1[i], l1[j] = l1[j], l1[i]
# sorted list
print("Sorted List", l1)
Output:
enter number of elements required: 6
76
23
45
12
54
9
Original List: [76, 23, 45, 12, 54, 9]
Sorted List [76, 54, 45, 23, 12, 9]
Similar Reads
How to Sort a Set of Values in Python? Sorting means arranging the set of values in either an increasing or decreasing manner. There are various methods to sort values in Python. We can store a set or group of values using various data structures such as list, tuples, dictionaries which depends on the data we are storing. We can sort val
7 min read
Python | Sort the list alphabetically in a dictionary In Python Dictionary is quite a useful data structure, which is usually used to hash a particular key with value, so that they can be retrieved efficiently. Let's see how to sort the list alphabetically in a dictionary. Sort a List Alphabetically in PythonIn Python, Sorting a List Alphabetically is
3 min read
Tag sort or Bucket sort or Bin sort in Python Tag sort, also known as Bucket sort or Bin sort, is a non-comparison based sorting algorithm that distributes elements of an array into a number of "buckets", and then sorts each bucket individually. Tag sort or Bucket sort or Bin sort Algorithm:Determine Range:Find the maximum and minimum values in
2 min read
Python sorted() Function sorted() function returns a new sorted list from the elements of any iterable like (e.g., list, tuples, strings ). It creates and returns a new sorted list and leaves the original iterable unchanged. Let's start with a basic example of sorting a list of numbers using the sorted() function.Pythona =
3 min read
Sort a List According to the Length of the Elements - Python We are given a list of strings and our task is to sort the list based on the length of each string. For example, if the input is ["Python", "C", "Java", "JavaScript", "Go"], the output should be ["C", "Go", "Java", "Python", "JavaScript"] since shorter strings come first.Using sorted() sorted() func
3 min read
Ways to sort list of dictionaries by values in Python - Using lambda function In this article, we will cover how to sort a dictionary by value in Python. Sorting has always been a useful utility in day-to-day programming. Dictionary in Python is widely used in many applications ranging from competitive domain to developer domain(e.g. handling JSON data). Having the knowledge
2 min read
Python - Sort list of list by specified index When working with a list of lists, we often need to sort the inner lists based on the values at a specific index. Sorting by a specified index is useful in the custom ordering of multidimensional data. In this article, we will explore different ways to sort a list of lists by a specified index in Py
3 min read
Python program to Sort a list according to the second element of sublist Sorting a list by the second element of each sublist means rearranging the sublists based on the value at index 1. For example, given the list [[1, 3], [2, 1], [4, 2], [3, 5]], sorting by the second element results in [[2, 1], [4, 2], [1, 3], [3, 5]], where the second elements (1, 2, 3, 5) are in as
3 min read
Python - Difference between sorted() and sort() Sorting means rearranging a given sequence of elements according to a comparison operator on the elements. The comparison operator is used to decide the new order of the elements in the respective data structure. For example, The below list of characters is sorted in increasing order of their ASCII
6 min read
Python - Sort words of sentence in ascending order Sorting words in a sentence in ascending order can be useful for tasks like text analysis, data preprocessing, or even fun applications like creating word puzzles. Itâs simple to achieve this using Python. In this article, we will explore different methods to do this.Using sorted() with SplitThe mos
3 min read