Card 04/ 11

ConceptDifficulty: Intermediate4 min

Radix Sort: One Stable Pass per Digit, Least Significant First

Sort by the last digit, then the next, and the earlier passes break the ties of the later ones.

Radix sort is a sorting algorithm for keys made of digits: it sorts the items by one digit at a time, starting with the least significant, using a stable sort for each pass, and after the pass on the most significant digit the items are in order. This card covers the least-significant-digit version, why each pass has to be stable, and what the sort costs.

A key's digits are the positions of its value written in some base b, the number of different values a digit can take: base 10 gives the decimal digits, and base 256 splits an integer into bytes. Least-significant-digit radix sort, LSD radix sort for short, makes one pass per digit position, ones first, then tens, then hundreds, and each pass is a counting sort that uses the digit as its key. A digit has only b values, so each pass needs only b counters, however wide the whole key is.

The trace sorts 13 4 10 7 14 3 10 in base 10. The largest key has two digits, so there are two passes, and each frame shows the array after one.

LSD radix sort on 13 4 10 7 14 3 10, one frame per pass

Input13 4 10 7 14 3 10

  1. Start

    1. 13
    2. 4
    3. 10
    4. 7
    5. 14
    6. 3
    7. 10

    Seven keys, none with more than two decimal digits, so radix sort makes two passes: the ones digit, then the tens digit.

  2. Ones digit

    1. 10
    2. 10
    3. 13
    4. 3
    5. 4
    6. 14
    7. 7

    A stable counting pass on the ones digit. The two 10s end in 0, 13 and 3 in 3, 4 and 14 in 4, and 7 in 7, and keys with the same ones digit keep their input order: 13 before 3, and 4 before 14.

  3. Tens digit

    1. 3
    2. 4
    3. 7
    4. 10
    5. 10
    6. 13
    7. 14

    A stable counting pass on the tens digit. 3, 4 and 7 share tens digit 0 and keep the order the ones pass gave them; the two 10s, 13 and 14 share tens digit 1 and do the same, so the array is sorted.

Result3 4 7 10 10 13 14

After the ones pass, the array is sorted by its last digit only: 13 sits before 4, because 3 is smaller than 4. The tens pass then sorts by the first digit, and within each tens group the items keep the order the ones pass gave them. That is why the pass must be stable. When two keys share a tens digit, the tens pass cannot tell them apart, and the only thing that puts 13 before 14 is that the ones pass already did and the tens pass left them alone. An unstable pass could swap them, and the sort would be wrong with no error.

Sorting a pile of exam papers by surname works the same way if you do it backwards: sort by the third letter, then the second, then the first, keeping papers in their current order whenever letters tie. The analogy breaks on length. Surnames have different lengths, and LSD radix sort needs every key to have the same number of digits, so shorter keys are padded, with leading zeros for numbers.

Sorting by the most significant digit first and never looking back does not work this way. A pass on the tens digit groups 3 4 7 apart from 10 10 13 14, but a later pass on the ones digit would scatter the groups again. Radix sort that starts at the most significant digit exists, but it has to sort each group separately, recursively, which is a different algorithm.

LSD radix sort on n keys of d digits in base b
Best, average and worst case
O(d · (n + b))
d counting passes, each over n items and b counters
Extra space
O(n + b)
one output array and b counters, reused by every pass
Stable
Yes
every pass is a stable counting sort
In place
No
each pass copies the items into a second array

The sign to reach for radix sort is keys with a fixed width that is too wide for counting sort: 32-bit or 64-bit integers, fixed-length codes, dates written as year, month and day. The base is a choice. A million 64-bit keys in base 256 take 8 passes, about 8 × (n + 256) = 8,002,048 steps, and in base 65,536 take 4 passes, about 4,262,144 steps, against roughly 20 million comparisons for an n log₂ n comparison sort; a step and a comparison do not cost the same, so measure before trusting the ratio.