Last Updated on June 27, 2026 by Rajeev Bagra
Sorting is one of the most fundamental operations in computer science. Whenever we arrange numbers, names, dates, or records in a specific order, we’re performing a sort.
Many beginners start with Bubble Sort, Selection Sort, and Insertion Sort because they’re easy to understand. However, modern software often relies on more advanced algorithms such as Merge Sort, Quick Sort, Heap Sort, and Tim Sort.
This article explains the most popular sorting algorithms in plain English and provides Python implementations for each.
What Is a Sorting Algorithm?
A sorting algorithm is a step-by-step procedure that arranges data into a particular order.
For example:
[5, 2, 8, 1, 3]
After sorting in ascending order:
[1, 2, 3, 5, 8]
Sorting makes searching, reporting, and analyzing data much more efficient.
1. Bubble Sort
How It Works
Bubble Sort repeatedly compares adjacent elements and swaps them if they are in the wrong order.
Think of bubbles rising to the surface of water. The largest values gradually move toward the end of the list.
Example
[5, 2, 8, 1]
Compare:
5 and 2 → swap
5 and 8 → no swap
8 and 1 → swap
Repeat until the list is sorted.
Python Code
def bubble_sort(L):
n = len(L)
for i in range(n):
for j in range(n - i - 1):
if L[j] > L[j + 1]:
L[j], L[j + 1] = L[j + 1], L[j]
return L
Time Complexity
Best: O(n)
Average: O(n²)
Worst: O(n²)
2. Selection Sort
How It Works
Selection Sort repeatedly finds the smallest element and places it in its correct position.
Imagine arranging playing cards by repeatedly selecting the smallest remaining card.
Example
[5, 2, 8, 1]
Find smallest:
1
Move it to the front:
[1, 2, 8, 5]
Repeat for the remaining elements.
Python Code
def selection_sort(L):
n = len(L)
for i in range(n):
min_index = i
for j in range(i + 1, n):
if L[j] < L[min_index]:
min_index = j
L[i], L[min_index] = L[min_index], L[i]
return L
Time Complexity
Best: O(n²)
Average: O(n²)
Worst: O(n²)
3. Insertion Sort
How It Works
Insertion Sort builds a sorted section one element at a time.
It’s similar to sorting cards in your hand.
Example
[5, 2, 4, 6]
Insert 2 before 5:
[2, 5, 4, 6]
Insert 4 between 2 and 5:
[2, 4, 5, 6]
Python Code
def insertion_sort(L):
for i in range(1, len(L)):
key = L[i]
j = i - 1
while j >= 0 and L[j] > key:
L[j + 1] = L[j]
j -= 1
L[j + 1] = key
return L
Time Complexity
Best: O(n)
Average: O(n²)
Worst: O(n²)
4. Merge Sort
How It Works
Merge Sort follows the “divide and conquer” strategy.
- Split the list into halves.
- Sort each half.
- Merge the sorted halves.
Example
[8, 3, 5, 1]
Split:
[8, 3]
[5, 1]
Sort:
[3, 8]
[1, 5]
Merge:
[1, 3, 5, 8]
Python Code
def merge_sort(L):
if len(L) <= 1:
return L
mid = len(L) // 2
left = merge_sort(L[:mid])
right = merge_sort(L[mid:])
return merge(left, right)
def merge(left, right):
result = []
i = j = 0
while i < len(left) and j < len(right):
if left[i] < right[j]:
result.append(left[i])
i += 1
else:
result.append(right[j])
j += 1
result.extend(left[i:])
result.extend(right[j:])
return result
Time Complexity
Best: O(n log n)
Average: O(n log n)
Worst: O(n log n)
5. Quick Sort
How It Works
Quick Sort selects a pivot element and places smaller values to its left and larger values to its right.
Example
[8, 3, 5, 1]
Pivot:
5
Partition:
[3,1] 5 [8]
Sort both sides recursively.
Python Code
def quick_sort(L):
if len(L) <= 1:
return L
pivot = L[len(L) // 2]
left = [x for x in L if x < pivot]
middle = [x for x in L if x == pivot]
right = [x for x in L if x > pivot]
return quick_sort(left) + middle + quick_sort(right)
Time Complexity
Best: O(n log n)
Average: O(n log n)
Worst: O(n²)
6. Heap Sort
How It Works
Heap Sort uses a special data structure called a heap.
The largest element is repeatedly removed and placed in its final position.
Python Code
import heapq
def heap_sort(L):
heapq.heapify(L)
result = []
while L:
result.append(heapq.heappop(L))
return result
Time Complexity
Best: O(n log n)
Average: O(n log n)
Worst: O(n log n)
7. Counting Sort
How It Works
Instead of comparing elements, Counting Sort counts how many times each value appears.
Example
[4, 2, 2, 8, 3]
Count occurrences and rebuild the list.
Python Code
def counting_sort(L):
maximum = max(L)
count = [0] * (maximum + 1)
for num in L:
count[num] += 1
result = []
for value in range(len(count)):
result.extend([value] * count[value])
return result
Time Complexity
O(n + k)
where k is the range of values.
8. Radix Sort
How It Works
Radix Sort sorts digits one position at a time.
For example:
170
45
75
90
802
Sort by:
- Ones digit
- Tens digit
- Hundreds digit
Time Complexity
O(d(n + k))
where:
- d = number of digits
- k = range of digit values
9. Bucket Sort
How It Works
Bucket Sort distributes values into buckets.
Each bucket is sorted individually.
Finally, all buckets are combined.
Best For
- Uniformly distributed data
- Decimal numbers
Time Complexity
Average: O(n)
Worst: O(n²)
10. Tim Sort
How It Works
Tim Sort combines:
- Insertion Sort
- Merge Sort
It takes advantage of partially sorted data.
This is the algorithm used by Python’s built-in sorting functions.
Example
numbers = [5, 2, 8, 1]
numbers.sort()
or
sorted(numbers)
Both use Tim Sort internally.
Time Complexity
Best: O(n)
Average: O(n log n)
Worst: O(n log n)
Comparison Table
| Algorithm | Best | Average | Worst |
|---|---|---|---|
| Bubble Sort | O(n) | O(n²) | O(n²) |
| Selection Sort | O(n²) | O(n²) | O(n²) |
| Insertion Sort | O(n) | O(n²) | O(n²) |
| Merge Sort | O(n log n) | O(n log n) | O(n log n) |
| Quick Sort | O(n log n) | O(n log n) | O(n²) |
| Heap Sort | O(n log n) | O(n log n) | O(n log n) |
| Counting Sort | O(n+k) | O(n+k) | O(n+k) |
| Radix Sort | O(d(n+k)) | O(d(n+k)) | O(d(n+k)) |
| Bucket Sort | O(n) | O(n) | O(n²) |
| Tim Sort | O(n) | O(n log n) | O(n log n) |
Which Sorting Algorithm Should You Learn First?
A recommended learning path is:
- Bubble Sort
- Selection Sort
- Insertion Sort
- Merge Sort
- Quick Sort
- Heap Sort
- Counting Sort
- Radix Sort
- Bucket Sort
- Tim Sort
This progression moves from simple comparison-based algorithms to more advanced and efficient techniques used in modern software systems.
Final Thoughts
Sorting algorithms are an excellent way to learn problem-solving, recursion, data structures, and algorithm analysis.
While Bubble Sort, Selection Sort, and Insertion Sort are ideal for beginners, professional software typically relies on algorithms such as Merge Sort, Quick Sort, Heap Sort, and Tim Sort because they scale much better as datasets grow.
Understanding how these algorithms work provides a strong foundation for computer science, coding interviews, and real-world software development.
Discover more from Progaiz.com
Subscribe to get the latest posts sent to your email.


Leave a Reply