
Python Sorting Benchmarks: Which Algorithm Wins?
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
Continue reading on Dev.to Python
Opens in a new tab




