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
| Property | Counting sort | Radix sort |
|---|---|---|
| Technique | Count each value, turn the counts into positions and place the values, with no comparisons | Sort digit by digit, least significant first, with a stable counting sort for each digit |
| Best case time | O(n + k) | O(d (n + k)) |
| Average case time | O(n + k) | O(d (n + k)) |
| Worst case time | O(n + k) | O(d (n + k)) |
| Extra space | O(n + k) | O(n + k) |
| Compares values with each other | No | No |
| Stable (equal values keep their order) | Yes | Yes |
| In place (no extra array) | No | No |
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.
| Input | Counting sort | Radix sort |
|---|---|---|
| Mixed | 47 | 141 |
| Already sorted | 47 | 141 |
| Reversed | 47 | 141 |
| Nearly sorted | 47 | 141 |
| Duplicates | 47 | 141 |
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
- Bubble sort vs Selection sort
- Bubble sort vs Insertion sort
- Bubble sort vs Merge sort
- Bubble sort vs Quick sort
- Bubble sort vs Heap sort
- Bubble sort vs Counting sort
- Bubble sort vs Radix sort
- Selection sort vs Insertion sort
- Selection sort vs Merge sort
- Selection sort vs Quick sort
- Selection sort vs Heap sort
- Selection sort vs Counting sort
- Selection sort vs Radix sort
- Insertion sort vs Merge sort
- Insertion sort vs Quick sort
- Insertion sort vs Heap sort
- Insertion sort vs Counting sort
- Insertion sort vs Radix sort
- Merge sort vs Quick sort
- Merge sort vs Heap sort
- Merge sort vs Counting sort
- Merge sort vs Radix sort
- Quick sort vs Heap sort
- Quick sort vs Counting sort
- Quick sort vs Radix sort
- Heap sort vs Counting sort
- Heap sort vs Radix sort