Radix Sort (LSD)

Sorts by processing digits from least significant to most significant, using counting sort as a subroutine.

Best O(d(n+k)) Avg O(d(n+k)) Worst O(d(n+k)) Space O(n+k) Stable Yes In-place No Distribution

How it works

Sorts by processing digits from least significant to most significant, using counting sort as a subroutine.

Implementation

function radixSort(arr) {
  const max = Math.max(...arr);
  for (let exp = 1; Math.floor(max/exp) > 0; exp *= 10) {
    const buckets = Array.from({length: 10}, () => []);
    for (const v of arr)
      buckets[Math.floor(v/exp) % 10].push(v);
    let idx = 0;
    for (const b of buckets)
      for (const v of b) arr[idx++] = v;
  }
}