Radix sort (LSD): Examples & Code

Algorithm Visualizations November 16, 2026 javascript

Published by Dimitris Kougioumtzis · Updated

Stably group values by each decimal digit, starting with units.

Stable digit passes refine the ordering from least significant digit to most significant digit.

Open the 3D visualization

Radix sort (LSD) example state preview
Preview of the example's final state. Complete values are available in the worked example and interactive timeline.

When to use Radix sort (LSD)

Nonnegative integer keys with bounded digit counts.

Choosing an alternative

Comparison sorts do not need a digit representation.

Worked example: input and state changes

Example input

38, 12, 65, 8, 43, 27, 91, 19

These checkpoints follow the actual example timeline. State details show the stored values and decisions; use the visualization to inspect every intervening step.

  1. Step 0: Initial state.

    State at this checkpoint
    {
      "values": [
        38,
        12,
        65,
        8,
        43,
        27,
        91,
        19
      ],
      "active": []
    }
  2. Step 8: Place 19 in digit bucket 9 (place 1).

    State at this checkpoint
    {
      "values": [
        38,
        12,
        65,
        8,
        43,
        27,
        91,
        19
      ],
      "active": [
        7
      ]
    }
  3. Step 17: Place 91 in digit bucket 9 (place 10).

    State at this checkpoint
    {
      "values": [
        91,
        12,
        43,
        65,
        27,
        38,
        8,
        19
      ],
      "active": [
        0
      ]
    }
  4. Step 25: Collect digit buckets for place 10.

    State at this checkpoint
    {
      "values": [
        8,
        12,
        43,
        65,
        27,
        38,
        8,
        19
      ],
      "active": [
        0
      ]
    }
  5. Step 33: Complete: values are in ascending order.

    State at this checkpoint
    {
      "values": [
        8,
        12,
        19,
        27,
        38,
        43,
        65,
        91
      ],
      "active": []
    }

Expected final state

Complete: values are in ascending order.

Complexity and input bounds

Family: Sorting. Time complexity: O(d(n + b)).

Space complexity: O(n + b) output and digit buckets.

Working-space bounds describe algorithm state and any explicitly noted output, excluding the visualization's saved timeline. Recording frames can use more memory than running the function alone. Here n is sequence length, V and E are vertex and edge counts, and other parameters refer to the named input dimensions.

2–24 comma-separated integers between 0 and 99.

The playground validates these limits before starting. Downloaded functions are callable implementations, with parameter and output-buffer contracts in their comments; they do not all repeat the playground's input validation.

Common mistakes and limitations

An unstable digit pass destroys ordering established by earlier passes.

Further reading

For background on algorithm analysis and the JavaScript function interface:

Editorial and testing approach · Browse all algorithm guides

Complete JavaScript implementation

javascript
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
// Sorts a in place. n is the array length. Return value 0 indicates completion. Reference snippets assume validated playground inputs. Fixed scratch buffers have 256 slots. Sorting variants may use a different traversal strategy than the animation.
function algorithm(a, n) {
    let maximum = 0; let temp = Array(256).fill(0);
    for (let i = 0; i < n; i++) { if (a[i] > maximum) { maximum = a[i]; } }
    for (let place = 1; place <= maximum; place *= 10) {
        let counts = Array(256).fill(0);
        for (let i = 0; i < n; i++) { counts[(Math.floor(a[i] / place)) % 10]++; }
        for (let d = 1; d < 10; d++) { counts[d] += counts[d - 1]; }
        for (let i = n - 1; i >= 0; i--) { let d = (Math.floor(a[i] / place)) % 10; temp[--counts[d]] = a[i]; }
        for (let i = 0; i < n; i++) { a[i] = temp[i]; }
    }
    return 0;
}

Source

No original paper has been verified for this entry. This source is a reference text or teaching reference, not an asserted original publication.