Every language ships a built-in sort, so writing these by hand is rarely necessary in production — but understanding how they work is what makes Big-O notation mean something concrete instead of an abstract formula.
def bubble_sort(arr):
n = len(arr)
for i in range(n):
for j in range(0, n - i - 1):
if arr[j] > arr[j + 1]:
arr[j], arr[j + 1] = arr[j + 1], arr[j]
return arr
Bubble sort repeatedly walks the list, swapping adjacent out-of-order pairs. It's O(n²) — for 10,000 items, that's roughly 100 million comparisons. It's taught because it's easy to trace by hand, not because anyone should use it.
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
return result + left[i:] + right[j:]
Merge sort splits the list in half recursively until pieces are trivially sorted (length 0 or 1), then merges sorted halves back together. It's O(n log n) — the halving is the "log n," and the merge step at each level is O(n).
def quicksort(arr):
if len(arr) <= 1:
return arr
pivot = arr[len(arr) // 2]
left = [x for x in arr if x < pivot]
middle = [x for x in arr if x == pivot]
right = [x for x in arr if x > pivot]
return quicksort(left) + middle + quicksort(right)
Quicksort picks a pivot value and partitions everything smaller to one side, larger to the other, then recurses on each side. Average case is O(n log n), same as merge sort — but a poorly chosen pivot on already-sorted input can degrade it to O(n²).
sorted(), Java's Collections.sort) actually use a hybrid — Timsort or introsort — that picks the best strategy based on the input, because no single algorithm above wins in every case.
O(n²) algorithms like bubble sort are fine for tiny lists (under a few hundred items) where simplicity matters more than speed. Anything larger, and the O(n log n) gap becomes the difference between milliseconds and minutes.
Comments