Every developer should understand sorting algorithms — not just to use them, but to understand the trade-offs that apply to all algorithm design.
You'll rarely implement sorting from scratch — every language has a built-in. But understanding sorting teaches you:
| Algorithm | Best | Average | Worst | Space | Stable |
|---|---|---|---|---|---|
| Bubble Sort | O(n) | O(n²) | O(n²) | O(1) | ✅ |
| Selection Sort | O(n²) | O(n²) | O(n²) | O(1) | ❌ |
| Insertion Sort | O(n) | O(n²) | O(n²) | O(1) | ✅ |
| Merge Sort | O(n log n) | O(n log n) | O(n log n) | O(n) | ✅ |
| Quick Sort | O(n log n) | O(n log n) | O(n²) | O(log n) | ❌ |
| Heap Sort | O(n log n) | O(n log n) | O(n log n) | O(1) | ❌ |
Repeatedly swap adjacent elements if they're in the wrong order.
def bubble_sort(arr):
n = len(arr)
for i in range(n):
swapped = False
for j in range(n - i - 1):
if arr[j] > arr[j+1]:
arr[j], arr[j+1] = arr[j+1], arr[j]
swapped = True
if not swapped:
break # already sorted — O(n) best case
return arrdef insertion_sort(arr):
for i in range(1, len(arr)):
key = arr[i]
j = i - 1
while j >= 0 and arr[j] > key:
arr[j+1] = arr[j] # shift right
j -= 1
arr[j+1] = key # insert in correct position
return arrPython's timsort uses insertion sort for small subarrays — that's why it's worth knowing.
def merge_sort(arr):
if len(arr) <= 1:
return arr
mid = len(arr) // 2
left = merge_sort(arr[:mid])
right = merge_sort(arr[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:])
def quick_sort(arr, low=0, high=None):
if high is None: high = len(arr) - 1
if low < high:
pivot_idx = partition(arr, low, high)
quick_sort(arr, low, pivot_idx - 1)
quick_sort(arr, pivot_idx + 1, high)
return arr
def partition(arr, low, high):
pivot = arr[high]
i = low - 1
for j in range(low, high):
if arr[j] <= pivot:
i += 1
arr[i], arr[j] = arr[j], arr[i]
In practice: Just use your language's built-in sort. Python's sorted() and .sort() use Timsort — a hybrid of merge sort and insertion sort that's O(n log n) worst case and O(n) for nearly sorted data.
When values are in a known range, you can sort in linear time:
def counting_sort(arr, max_val):
count = [0] * (max_val + 1)
for num in arr:
count[num] += 1
result = []
for val, freq in enumerate(count):
result.extend([val] * freq)
return result