← Knowledgebase

Counting sort and radix sort

Sorting without comparing

Merge sort, quicksort and heapsort learn about the order only by comparing two values, and any such sort needs about n log n comparisons in the worst case. But if the keys are small integers, or digits, you can use them directly as positions, and beat that bound.

Counting sort

To sort values by a key from 0 to 9 (say, one digit):

  1. Count how many values have each key.
  2. Turn the counts into starting slots: key d's values start right after all values with smaller keys, at count[0] + … + count[d − 1] (a prefix sum).
  3. Go through the input left to right and put each value into the next free slot for its key, in an output array.

Going left to right and filling each key's slots from the front keeps values with equal keys in their original order. A sort with that property is called stable.

Radix sort: one digit at a time

To sort numbers with several digits, counting-sort them by the ones digit, then by the tens, and so on, least significant digit first. Stability is what makes this work: when two numbers have the same tens digit, the tens pass leaves them in the order the ones pass put them in, which is the right order.

Reading the pictures

Each value's badge shows the digit of the current pass. Values move from the array into the output row below, then are copied back. Under the array, the table shows how many values have each digit, and the next free output slot for each digit.

Counting sortRadix sort
TimeO(n + k) for keys 0 … k − 1O(d · (n + 10)) for d-digit numbers
Extra spaceO(n + k)O(n + 10)
Stable?Yes, filled front to backYes
CatchOnly works on keys that break into small pieces: integers, fixed-length strings, dates.