Sort a list in Python without sort Function
Last Updated :
04 Jul, 2025
Sorting a list in Python means arranging the elements in a specific order (ascending or descending) using custom logic. For example, in a list like [5, 2, 9, 1], we can manually rearrange the elements as [1, 2, 5, 9].
Let’s explore different methods of sorting a list without using built-in sort function.
Using merge sort()
Merge Sort is a divide-and-conquer algorithm that splits the list into two halves, recursively sorts each half and then merges the two sorted halves into a single sorted list. It is highly efficient for large datasets.
Python
def merge_sort(arr):
if len(arr) > 1:
mid = len(arr) // 2
L = arr[:mid]
R = arr[mid:]
merge_sort(L)
merge_sort(R)
i = j = k = 0
while i < len(L) and j < len(R):
if L[i] > R[j]: # Descending
arr[k] = L[i]
i += 1
else:
arr[k] = R[j]
j += 1
k += 1
while i < len(L):
arr[k] = L[i]
i += 1
k += 1
while j < len(R):
arr[k] = R[j]
j += 1
k += 1
a = [76, 23, 45, 12, 54, 9]
merge_sort(a)
print(a)
Output[76, 54, 45, 23, 12, 9]
Explanation:
- The list is divided into two parts and recursively sorted.
- The merge step ensures that elements are placed in descending order.
Using quick sort
Quick Sort is a divide-and-conquer algorithm that selects a pivot and partitions the list into two sublists, one with elements greater than the pivot and one with lesser or equal. It recursively sorts these sublists to produce a descending order.
Python
def quick_sort(arr):
if len(arr) <= 1:
return arr
else:
pivot = arr[0]
greater = [x for x in arr[1:] if x > pivot]
lesser = [x for x in arr[1:] if x <= pivot]
return quick_sort(greater) + [pivot] + quick_sort(lesser)
a = [76, 23, 45, 12, 54, 9]
a = quick_sort(a)
print(a)
Output[76, 54, 45, 23, 12, 9]
Explanation:
- The pivot element divides the list into two sublists.
- Both sublists are recursively sorted, placing larger elements first for descending order.
Using Bubble Sort
Bubble Sort is a simple comparison-based sorting algorithm. It works by repeatedly swapping adjacent elements if they are in the wrong order. In this case, it checks if the left element is smaller than the right and swaps them to sort in descending order.
Python
a = [76, 23, 45, 12, 54, 9]
n = len(a)
for i in range(n):
for j in range(0, n - i - 1):
if a[j] < a[j + 1]: # Descending
a[j], a[j + 1] = a[j + 1], a[j]
print(a)
Output[76, 54, 45, 23, 12, 9]
Explanation:
- Bubble Sort repeatedly swaps adjacent elements to push larger ones to the front.
- The process continues until the list is sorted.
Using Selection Sort
Selection Sort divides the list into a sorted and unsorted part. It repeatedly selects the maximum element from the unsorted part and swaps it with the first unsorted element. This method sorts the list in descending order by always placing the largest remaining value at the current position.
Python
a = [76, 23, 45, 12, 54, 9]
n = len(a)
for i in range(n):
max_idx = i
for j in range(i+1, n):
if a[j] > a[max_idx]:
max_idx = j
a[i], a[max_idx] = a[max_idx], a[i]
print(a)
Output[76, 54, 45, 23, 12, 9]
Explanation:
- Selection Sort selects the largest element in each iteration and places it in the sorted part.
- It works well for small lists but is inefficient for large lists.
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