Python's built-in sort() beats every hand-rolled sorting algorithm you'll write — by a lot. On 100,000 integers, Timsort finishes in ~6ms while a clean quicksort implementation takes ~80ms on the same machine. That gap surprises people who assume "understanding" the algorithm means implementing it competitively.
But benchmarks only tell part of the story. Interviews ask you to implement these algorithms from scratch, and knowing why one is faster than another is what separates a passing answer from a strong one. So let's actually run the numbers, understand them, and figure out what to say when the interviewer asks.
Bubble Sort vs Quicksort vs Mergesort: Raw Speed Numbers
Here's the benchmark harness I used. All times are wall-clock on CPython 3.11, averaged over 10 runs, on a list of random integers:
import random
import time
from typing import Callable
def benchmark(sort_fn: Callable, sizes: list[int], trials: int = 10) -> dict:
results = {}
for n in sizes:
times = []
for _ in range(trials):
arr = random.sample(range(n * 10), n) # no duplicates
start = time.perf_counter()
sort_fn(arr.copy()) # copy so each trial gets a fresh unsorted array
elapsed = time.perf_counter() - start
times.append(elapsed)
results[n] = sum(times) / len(times)
return results
sizes = [100, 1_000, 10_000, 50_000, 100_000]
Now the implementations:
python
---
*Continue reading the full article on [TildAlice](https://tildalice.io/python-sorting-algorithms-benchmark/)*
Top comments (0)