DSA
    intermediate

    Radix Sort

    Software Engineer path · lesson 15 of 99
    time O(d(n+b))
    space O(n+k)

    Whole numbers 0–9999. One pass per digit of the largest value.

    Try:
    ÷11700451752903802424526667
    0123456789
    Being placedIts bucket

    Sort by the ones digit, then tens, then hundreds — each pass is stable.

    Code (running line highlighted)
    1. 1exp = 1
    2. 2while max(a) // exp > 0:
    3. 3 buckets = 10 empty lists
    4. 4 for x in a: buckets[(x // exp) % 10].append(x)
    5. 5 a = concatenate(buckets) # stable pass
    6. 6 exp *= 10
    Passes
    0 / 3
    Moves
    0 / 24

    Tap any element in the diagram to inspect its state at this step.

    1 / 32

    Keys: ← → step, Space play/pause. Predict mode pauses before key steps and asks what happens next.

    Least significant digit first

    Sequence

    Order the passes of an LSD radix sort on three-digit numbers.

    Tap a piece, then tap where it goes — or drag it. Tap the tray to take one back.

    1. 1Drop a step here
    2. 2Drop a step here
    3. 3Drop a step here
    4. 4Drop a step here
    5. 5Drop a step here
    5 pieces left to place

    Test your knowledge

    20 questions
    Question 1 of 20
    Score: 0

    Why must the per-digit sort in Radix Sort be stable?

    The short version

    Think of sorting a library by call numbers. First sort by the last digit, then the second-to-last, and so on. After processing all digits, the books are perfectly ordered!

    In more depth

    LSD Radix Sort (Least Significant Digit)

    Process digits from right to left:

    1.Sort all numbers by ones digit
    2.Sort by tens digit (stable sort preserves ones order)
    3.Sort by hundreds digit...
    4.Continue until all digits processed

    Why LSD Works

    Later passes override earlier ones, but stable sorting ensures ties are broken by earlier digit order.

    When to Use

    Fixed-length integers or strings
    Range is large but digit count is small
    Alternative to O(n log n) sorts

    When to reach for this

    • Sorting large integers or fixed-length strings
    • Need O(n) time for integer sorting
    • Values have a fixed number of digits
    • Sorting by multiple keys (like dates: year, month, day)

    Interview Questions

    Google
    Microsoft
    Amazon
    Airbnb
    Facebook
    Bloomberg
    Apple
    Uber

    Love this visualization?

    Sign up to track your progress, earn achievements, use the AI assistant, and access your personal learning dashboard.

    100% free • No credit card required