Radix sort never compares two numbers. It sorts them one digit at a time, starting with the ones place, by dropping each number into one of ten buckets for that digit. After the last digit, the whole array is sorted. For many keys with few digits, that beats any comparison sort.
Think of sorting a stack of index cards numbered 0 to 999 by hand. You could deal them into ten piles by their last digit, stack the piles back up in order 0 to 9, then deal again by the middle digit, and finally by the first digit. After the third pass the cards are in order.
That is least significant digit (LSD) radix sort. Each pass:
Why does this work? Each pass must be stable: numbers with the same digit stay in the order the previous pass left them. In the example below, 2, 24, 45, 66, 75 and 90 all have hundreds digit 0. Because the hundreds pass is stable, they stay in the order the first two passes gave them, which is already sorted by their last two digits. In general, after pass k the array is sorted by its last k digits. If a pass were not stable, it would scramble the work of the earlier passes.
Sorting {170, 45, 75, 90, 802, 24, 2, 66}. The largest value, 802, has three digits, so there are three passes. The highlighted digit is the one each pass sorts by. Short numbers are treated as if they had leading zeros: 45 is 045.
Real code does not build ten lists for every pass. It uses a counting sort on the current digit, which has the same effect with plain arrays:
count[d].count[d] is the position just past where the d-bucket ends in the output.output[count[d] − 1] and decrease count[d]. Going through the array from the end puts later numbers further right in their bucket, which is what keeps the pass stable.The lab below shows both views at once: the buckets as they fill, and the count[] values under each bucket.
Each bucket keeps numbers in arrival order, oldest at the bottom. Faded numbers have already been placed into output.
Radix sort written in the style of the class sorting code. The array in main() changes to match what you sort.
Let n be the number of keys, d the number of digits in the largest key, and b the base (10 here). Each pass does a counting sort that touches every key a few times plus every bucket once, so
time = O(d · (n + b)) extra space = O(n + b)
When d is small and fixed, that is linear in n. A comparison sort such as merge sort cannot do better than about n log₂ n comparisons, so radix sort wins when there are many keys with few digits: sorting a million 6-digit IDs takes 6 passes, while n log₂ n is about 20 million comparisons. When the keys are long (large d), the advantage shrinks or disappears.
"Element writes" counts writes into output plus the copy back: 2 · n per pass. The last column is the approximate number of comparisons a good comparison sort needs.
Four, because the largest value, 3009, has four digits. Every number goes through all four passes, even 7.
Within a bucket, positions are handed out from right to left. Going from the end of the array means the last number with a given digit gets the rightmost position, so numbers with equal digits keep their order. That stability is what makes the earlier passes count.
The final pass, on the ones digit, would reorder everything by its last digit and destroy the order from the earlier passes. MSD radix sort exists, but it has to sort each bucket separately and recursively.
It needs keys made of digits, extra memory, and enough keys for the passes to pay off. For long keys, d can be larger than log₂ n, and general-purpose comparison sorts work on any data that can be compared.