Every algorithm textbook warns you: sorting n numbers by comparing them cannot do better than in the worst case. Merge sort, heapsort, and the best quicksort variants all live at that ceiling.
But what if you stop comparing?
Counting sort, invented in the early 1950s, exploits the fact that integers live in a bounded range. Instead of asking "is A bigger than B?", it asks "how many numbers equal each value?" then reconstructs the sorted order from those counts — in time, where k is the size of the value range.
Radix sort goes further. It applies counting sort digit-by-digit — least significant digit first — sorting the array one digit position at a time. Each pass runs in , and with d digit positions the whole algorithm finishes in O(d · (n + k)). For fixed-width integers (e.g. 32-bit), d is constant, giving true linear time .
This is not a trick or a special case. It is a mathematically proven faster algorithm — proven possible because the lower bound only applies to comparison-based sorting. Once you exploit the structure of the keys, the barrier disappears.
Comments
Loading comments...