Open In App

Sort a list in Python without sort Function

Last Updated : 04 Jul, 2025
Summarize
Comments
Improve
Suggest changes
Share
Like Article
Like
Report

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.

Practice Tags :

Similar Reads