Algorithm Visualiser by Tsvetozar Biserov

Guides

Comparison sorts versus counting, radix and bucket sort

Merge sort, quick sort and heap sort cannot do much better than n log n steps, and that is a limit of the method, not a flaw in their design. This guide explains the limit with a counting argument, then shows how counting, radix and bucket sort sidestep it and what they ask in return.

What a comparison sort may know

A comparison sort learns about its input only by asking whether one element is smaller than another. It never looks inside a key. That is what makes it general: the same code sorts numbers, names or dates once it has a comparison function. Bubble, insertion, merge, quick, heap and most of the other algorithms on this site work this way, and every question they ask is tallied in the Comparisons counter.

The decision-tree argument

Picture the algorithm as a tree. Each node is one comparison with two outcomes, and each leaf is a final answer: one particular ordering of the input. Three distinct elements can arrive in six orders, and the algorithm must reach a different leaf for each, otherwise two different inputs would be rearranged the same way and one of them would come out wrong. A tree of depth d has at most 2 to the power d leaves, so we need 2 to the power d to be at least n factorial, which means d must be at least log2 of n factorial.

For 10 elements, 10 factorial is 3,628,800, so some inputs need at least 22 comparisons. In general log2 of n factorial grows like n log2 n minus about 1.44 n; for 1000 elements that is roughly 8,530. Merge sort on the visualiser with 1000 shuffled elements makes about 8,700, which is very close to the floor. The same bound holds on average, not only in the worst case.

The loophole: look inside the keys

The proof only limits algorithms that learn through yes or no questions. If an algorithm can use a key as an array index, one step can tell apart many outcomes, not two. Counting, radix and bucket sort do exactly that. In this site's versions the only comparisons outside the buckets are the n minus 1 spent finding the largest value, so on 1000 shuffled elements counting sort reports 999 comparisons against about 8,700 for merge sort.

The work did not vanish. It moved into reads and writes of helper tables, which the Reads and Writes counters include. A low comparison count is not a promise of low cost.

Counting sort and its price

Counting sort makes a table with one cell per possible value, tallies how often each value occurs, and writes the values back in order. The time is n plus k, where k is the size of the value range. The price is memory for k cells and the assumption that keys are small non-negative integers: sorting a thousand numbers between 0 and a billion would need a billion cells.

On 1000 shuffled elements the run shows about 4,000 reads and 2,000 writes: about 2,000 reads and 1,000 writes on the array, plus as many on the count table.

Radix sort: one digit at a time

Radix sort avoids the huge table by splitting each key into digits and running one stable counting pass per digit, least significant first. This site uses base 10, so at 1000 elements, where the largest value is 1000, it makes four passes, each with a 10-cell count table and an output buffer of n cells. The cost is d times n plus b, where d is the number of digits and b the base.

For fixed-width keys that is linear in n. If the keys grow with n, though, say n distinct values, they need at least about log n digits, and the advantage fades back toward n log n. Radix sort suits fixed-width integers and strings; floating point numbers and negative values need extra care to map them onto digits that sort correctly.

Bucket sort: trust the distribution

Bucket sort splits the value range into about the square root of n buckets, drops each element into its bucket with arithmetic, sorts each small bucket with insertion sort and reads them back in order. If the values are spread evenly, every bucket holds a handful of elements and the total is close to linear.

If most values land in one bucket, that bucket is sorted by insertion sort and the cost turns quadratic. Its speed is a bet on the data: good for evenly spread measurements, poor for skewed ones. Merge sort makes no such bet, and its n log n holds for any input.

Choosing between them

Reach for a non-comparison sort when the keys are integers or fixed-width, the range or digit count is modest next to n, and you can spare the memory. Otherwise use a comparison sort. Even then, measure: scattered writes into a large table can be slower on real hardware than the operation counts suggest. A quick test: if you cannot state the key range or the digit count in one sentence, you probably need a comparison sort.

Guides