When Interview-Size Arrays Change Everything
Most sorting benchmarks test millions of elements. That's not your interview. You're sorting maybe 1,000 integers while the interviewer watches. So does algorithm choice actually matter at this scale?
I ran the numbers. At 1,000 elements, the gap between Python's built-in sorted() and a hand-rolled quicksort isn't 10x — it's 47x. But here's where it gets weird: at 100 elements, bubble sort sometimes beats quicksort. Not in theory. In actual microseconds on real hardware.
This matters because interviewers occasionally ask you to implement sorting from scratch. And the obvious follow-up is "what's the complexity?" But they rarely ask "what's the actual runtime?" Understanding both makes you sound like you've actually written production code.
The Benchmark Setup
I tested seven sorting algorithms on arrays of 100, 500, 1,000, 5,000, and 10,000 random integers. Each test ran 100 times with fresh random data. Python 3.11 on an M1 MacBook Air, time.perf_counter_ns() for nanosecond precision.
python
import random
import time
from typing import List, Callable
import heapq
def benchmark_sort(sort_fn: Callable, arr_sizes: List[int], runs: int = 100):
"""Returns {size: median_time_ns} for each array size."""
results = {}
for n in arr_sizes:
times = []
for _ in range(runs):
arr = [random.randint(0, 10_000) for _ in range(n)]
test_arr = arr.copy()
---
*Continue reading the full article on [TildAlice](https://tildalice.io/python-sorting-runtime-benchmark-interview-arrays/)*
Top comments (0)