Mr. Grummel Get the app
← All notes
LEARNING 5 MIN READ DRAFT — APRIL 2027

Why there's no single best way to sort a list

Quicksort, mergesort and heapsort each make a different trade-off between speed, memory use and worst-case behaviour, which is why all three are still in active use.

Sorting a list is one of the oldest, most thoroughly studied problems in computer science, and it still doesn't have a single settled "best" solution. Quicksort, mergesort, and heapsort all reliably sort a list correctly, and all three remain in widespread real-world use decades after they were first developed, because each makes a genuinely different trade-off among the properties that matter in practice: typical-case speed, worst-case speed, and how much extra memory the algorithm needs beyond the list itself.

Quicksort: fast on average, with a bad-case exception

Quicksort works by picking a "pivot" element, partitioning the rest of the list into elements smaller and larger than the pivot, and recursively sorting each partition. In the typical case, this achieves excellent average performance, and quicksort is often the fastest of the three algorithms in practice on real-world data, largely because of how efficiently it works with the memory access patterns of modern computer hardware. Its weakness is a genuinely bad worst case: with an unlucky choice of pivot on an already poorly-ordered or adversarially constructed list, quicksort's performance can degrade substantially compared to its typical behaviour, which matters in any context where predictable, guaranteed performance is more important than excellent average-case speed.

Mergesort and heapsort: trading speed for guarantees

Mergesort takes a different approach: it splits the list in half repeatedly down to individual elements, then merges those pieces back together in sorted order. Its defining advantage is a guaranteed worst-case performance that doesn't degrade the way quicksort's can, at the cost of requiring extra memory proportional to the size of the list to hold the merged pieces during the process — a genuine downside on memory-constrained systems, but often an acceptable one. Heapsort takes yet another approach, building the list into a specific tree-like structure called a heap and repeatedly extracting the largest remaining element; it matches mergesort's guaranteed worst-case performance while, unlike mergesort, sorting the list in place without needing that extra memory, though it's typically slower in practice than quicksort's average case and doesn't preserve the relative order of equal elements the way some other algorithms do. No single one of the three dominates the other two on every property that matters, which is exactly why a working programmer's choice between them still depends on which specific trade-off matters most for the task at hand.

Quicksort, mergesort and heapsort all solve the same problem, and none of them is simply better than the others. Each makes a different trade-off between speed, memory use and worst-case behaviour, which is why all three are still in active use.

What we're still unsure about

The theoretical performance characteristics of quicksort, mergesort, and heapsort — their average-case and worst-case behaviour, and their memory requirements — are extremely well established and rigorously proven in computer science. What's less a matter of settled theory and more a matter of ongoing practical engineering is exactly how these algorithms perform on specific real hardware and specific real-world data patterns, since actual performance depends heavily on details like processor cache behaviour and the particular structure of the data being sorted, which is why many production sorting implementations today actually use hybrid approaches that switch between different algorithms depending on the input, rather than committing to one of the three in isolation.

This sits inside Sorting Algorithms (Quicksort, Mergesort, Heapsort), one of eight topics in Algorithms, one of seven domains in Computer Science, one of seventeen subjects the app can quiz you on.

Draft — not published yet.
Try the pop quiz