Comparisons

Counting Sort vs Radix Sort

Counting Sort vs Radix Sort compared. Both are non-comparison linear sorts, but they handle value ranges very differently. Learn which to use and when.

Aman Jaiman
8 min read
On this page +
  1. In plain English
  2. The relationship
  3. When to use each
  4. Choosing between them in practice
  5. Frequently asked questions

Counting Sort vs Radix Sort compares two non-comparison sorts that both achieve near-linear time. They are closely related — Radix Sort actually uses Counting Sort internally — but they handle the range of values in fundamentally different ways.

The relationship #

Counting Sort tallies every distinct value, needing O(k) memory for value range k. Radix Sort sidesteps the memory blow-up by sorting digit by digit, applying a stable Counting Sort on each digit so k stays tiny (just 10 for decimal digits).

When to use each #

Use Counting Sort when the value range k is small (scores 0-100, ages). Use Radix Sort when values are large integers but have a fixed number of digits — it keeps memory low while still beating O(n log n). Both are stable and non-comparison.

Choosing between them in practice #

The decision comes down to one number: the range of your keys relative to how many items you have.

Counting Sort wins when the range is small and known. Sorting a million exam scores from 0 to 100, ages from 0 to 120, or bytes from 0 to 255 — here k is tiny, so O(n + k) is effectively O(n) and you cannot do better. It is a single counting pass and a single output pass.

Counting Sort collapses when the range is large. Sorting a thousand 32-bit integers would require a counting array of over four billion entries to hold values you mostly do not have. The memory cost is O(k) regardless of how few items you are actually sorting, which makes it unusable for sparse data over a wide range.

Radix Sort is the fix for exactly that case. It applies a stable Counting Sort one digit at a time, so the counting array only ever needs as many slots as there are possible digit values — ten for decimal, or 256 if you process a byte at a time. Sorting those same 32-bit integers becomes four passes with a 256-entry array instead of one pass with a four-billion-entry array.

This dependency is worth stating explicitly, because it is often missed: Radix Sort requires a stable sub-sort to work at all. Each pass must preserve the ordering established by the previous, less significant digit. If the per-digit sort were unstable, sorting by the tens digit would scramble the units ordering and the algorithm would simply produce wrong output. Counting Sort's stability is not an incidental nice-to-have here — it is the load-bearing property.

A quick rule: if k is comparable to n or smaller, use Counting Sort. If k is enormous but keys have a bounded number of digits, use Radix Sort. If keys are unbounded, arbitrary-precision or need a custom comparison, neither applies and you are back to comparison sorting.

Found this useful?

Share it with someone who is learning this too.

Questions

Frequently asked questions

What is the difference between Counting Sort and Radix Sort? +
Counting Sort sorts by exact value and needs O(k) memory for the value range. Radix Sort sorts digit by digit using Counting Sort as a sub-routine, keeping memory low even for large value ranges.
Does Radix Sort use Counting Sort? +
Yes, LSD Radix Sort typically uses a stable Counting Sort for each digit position. The stability of each pass is what makes Radix Sort produce correct results.
When should I use Counting Sort instead of Radix Sort? +
When the range of possible key values is small and known in advance — exam scores, ages, byte values, small enumerations. There Counting Sort is a single pass and unbeatable. Switch to Radix Sort once the range grows large enough that a counting array of size k becomes wasteful or impossible, since Radix only needs a small array per digit.
Why does Radix Sort need a stable sub-sort? +
Because each digit pass must preserve the order established by the previous one. Radix Sort works from the least significant digit upwards, so when it sorts by the tens digit it is relying on items with equal tens digits still being in units-digit order. An unstable sub-sort would destroy that ordering and produce an incorrectly sorted result. This is why Counting Sort, which is stable, is the standard choice inside Radix Sort.

See it in motion

Watch this algorithm and nine others run step by step, with live pseudocode and comparison counters.

Launch the visualiser
Aman Jaiman
Written by
Aman Jaiman

Software engineer at a stealth-stage startup, and previously a front-end engineer for around a year and a half.

Keep reading

Related guides

Link copied