Radix sort

Sort integers digit by digit, from least to most significant, using a stable counting sort on each digit: O(d · n) for d-digit keys.

Description

Radix sort sorts numbers by their digits rather than by comparing whole values. The common least-significant-digit (LSD) version first sorts by the ones digit, then by the tens, then the hundreds, and so on.

The trick is that each pass uses a stable sort, usually counting sort. Stability means that when two numbers have the same tens digit, they stay in the order the previous pass, by ones, put them in. After the last digit, the array is fully sorted.

With d digits in base b, radix sort takes O(d · (n + b)) time. For fixed-size keys such as 32-bit integers processed one byte at a time, that's four passes: effectively linear. It also works on fixed-length strings such as dates, postal codes or IDs.

In practice radix sort shines on large arrays of integers and in GPUs, but uses extra memory and is less flexible than comparison sorts.

How it works

1700901802223244455756667

After sorting by the ones digit, before sorting by the tens digit

  1. index 0: 170
  2. index 1: 90
  3. index 2: 802
  4. index 3: 2
  5. index 4: 24
  6. index 5: 45
  7. index 6: 75
  8. index 7: 66
After sorting by the ones digit, before sorting by the tens digit

Step through: one stable pass per digit

17045759080224266

Step 1 of 5Start with 8 values.

0 comparisons, 0 writes
New data: , , ,

Complexity

OperationAverageWorst
Sort n keys of d digits in base bO(d · (n + b))O(d · (n + b))
32-bit integers, one byte per passFour passes with 256 buckets.O(n)O(n)

Space: O(n + b)

TypeScript implementation

LSD radix sort for non-negative integers, one byte (base 256) per pass.

radix-sort.ts
// LSD radix sort for non-negative 32-bit integers, one byte per pass.

export function radixSort(a: number[]): number[] {
  let src = Uint32Array.from(a);
  let dst = new Uint32Array(src.length);
  for (let shift = 0; shift < 32; shift += 8) {
    const count = new Uint32Array(257);
    for (const v of src) count[((v >>> shift) & 0xff) + 1]++;
    for (let i = 0; i < 256; i++) count[i + 1] += count[i];
    for (const v of src) dst[count[(v >>> shift) & 0xff]++] = v; // stable
    [src, dst] = [dst, src];
  }
  return Array.from(src);
}

export function demo(): string[] {
  const n = 1_000_000;
  let s = 1;
  const big = Array.from({ length: n }, () => (s = (s * 1103515245 + 12345) >>> 0));
  const t0 = performance.now();
  const r = radixSort(big);
  const t1 = performance.now();
  const ok = r.every((v, i) => i === 0 || r[i - 1] <= v);
  return [
    `[${radixSort([170, 45, 75, 90, 802, 24, 2, 66]).join(", ")}]`,
    `1 000 000 random 32-bit integers in 4 passes: ${(t1 - t0).toFixed(0)} ms, sorted: ${ok}`,
  ];
}

When to use it

Use it when

  • Large arrays of integers or fixed-length keys.
  • When comparison-sort speed isn't enough and keys have bounded length.

Avoid it when

  • Keys are variable-length, floating point or need a custom comparison.
  • Arrays are small: the constant factors make it slower than a built-in sort.

Pitfalls

  • Using an unstable inner sort, which scrambles earlier passes.
  • Negative numbers, which need special handling of the sign.
  • Starting from the most significant digit without recursion: MSD radix sort works differently.

Alternatives

Counting sort

Choose it when the key range is small enough for a single pass.

Quicksort or a built-in sort

Choose it when keys aren't fixed-size integers or strings.