Skip to content
SimpleScope

Sorting

Counting sort vs Radix sort

Counting sort and Radix sort both put an array in order, in different ways. Counting sort: count each value, turn the counts into positions and place the values, with no comparisons. Radix sort: sort digit by digit, least significant first, with a stable counting sort for each digit.

At a glance

PropertyCounting sortRadix sort
TechniqueCount each value, turn the counts into positions and place the values, with no comparisonsSort digit by digit, least significant first, with a stable counting sort for each digit
Best case timeO(n + k)O(d (n + k))
Average case timeO(n + k)O(d (n + k))
Worst case timeO(n + k)O(d (n + k))
Extra spaceO(n + k)O(n + k)
Compares values with each otherNoNo
Stable (equal values keep their order)YesYes
In place (no extra array)NoNo

When to choose each

Counting sort: when to choose it

Choose counting sort when every key fits in one small range you can afford to allocate — ages, grades, byte values, bucket ids. It is a single pass of counting and placing, O(n + k), and it is the simpler of the two by a long way: radix sort is literally counting sort repeated per digit. If k is small, there is nothing to gain from splitting the key into digits, and each digit you split into is another full pass over the data.

Radix sort: when to choose it

Choose radix sort when the keys are too wide for one count array. Sorting 32-bit integers with counting sort would need four billion counters; radix sort sorts them as four passes over a range of 256, trading d passes for a small k. Its cost is O(d(n + k)) against counting sort's O(n + k), so the question is only whether d small passes over a narrow range beat one pass over a range you cannot allocate. It relies on counting sort's stability to work at all.

The same inputs, counted

These numbers come from running both real implementations: operations (comparisons plus writes, a swap counting as two writes). Counting sort makes no comparisons, so its count is writes only. It includes the k + 1 cells of the count array. Radix sort makes no comparisons, so its count is writes only. The lesson always runs 3 digit passes, even on these one-digit numbers.

InputCounting sortRadix sort
Mixed47141
Already sorted47141
Reversed47141
Nearly sorted47141
Duplicates47141

What the numbers show

The relationship here is exact rather than approximate: counting sort costs 47 operations on every one of the five arrays and radix sort costs 141, which is precisely three times as many. The lesson's radix sort always runs 3 digit passes, and each pass is a complete stable counting sort over the same seven values and the same range — so on this input radix sort is, operation for operation, counting sort done three times. The ratio holds on all five arrays: 47 against 141 when sorted, 47 against 141 reversed, 47 against 141 nearly sorted, 47 against 141 with duplicates.

On these arrays two of those passes are provably pointless. The values are one-digit numbers, so sorting them by their tens digit and then their hundreds digit compares nothing, moves nothing into a new relative order, and still pays the full 47 operations each time: zeroing the count array, counting, prefix-summing, placing and copying back. Neither algorithm makes a single comparison — both rows in the table say No — so both totals are writes.

That 3× gap is what the extra generality costs when you do not need it, and it is why the choice is made on k, not on n. Counting sort's single pass needs k + 1 counters; radix sort needs d passes but only ever k = 9 counters per pass. For one-digit values, d = 1 would make them identical, and the lesson's fixed d = 3 is exactly the overhead of a key format wider than the data. For three-digit keys the comparison inverts: counting sort would need a 1,000-cell count array where radix sort still needs ten.

See them run

More comparisons