CMD Guide
HomeDSAAdvanced Patterns

Radix Sort Algorithm

Radix Sort sorts integers without comparing them: it repeatedly buckets numbers by one digit position at a time using a stable counting-sort pass, so that by the time the most significant digit has been processed the whole array is ordered — the trick being that stability lets the ordering established by earlier (less significant) digits survive intact through every later pass.

Recognize the pattern

Brute force → optimal

Brute force: a comparison sort (Merge/Quick/Heap Sort) treats each key as opaque and compares whole keys pairwise — O(n log n) comparisons, and comparing two d-digit numbers itself costs O(d), giving O(n·d·log n) overall.

Optimal — Radix Sort: never compares two full keys directly. It runs d passes, each a stable Counting Sort over a single digit in base b (e.g. b = 10). Total work O(d·(n + b)) — linear in n for fixed d and b, which beats the Ω(n log n) lower bound that only binds comparison-based sorts.

Complexity, derived from first principles

Let n = elements, d = digits in the largest element, b = radix (base).

Traced worked example

Input: [180, 55, 85, 90, 903, 243, 2, 6]. Max = 903 → d = 3 digits, so 3 counting-sort passes (exp = 1, 10, 100).

Pass (exp)Digit examinedArray after this pass
1 (units)ones place[180, 90, 2, 903, 243, 55, 85, 6]
2 (tens)tens place[2, 903, 6, 243, 55, 180, 85, 90]
3 (hundreds)hundreds place[2, 6, 55, 85, 90, 180, 243, 903]

Pass 1 detail (units digit): in input order the units digits are 0,5,5,0,3,3,2,6 for 180,55,85,90,903,243,2,6 — count array [2,0,1,2,0,2,1,0,0,0] (index = digit, e.g. two numbers end in 0: 180 and 90). Cumulative sums give the final slot for each digit; scanning the input right to left and placing each number at count[digit]-1 then decrementing preserves relative order among equal digits — that's what makes the whole algorithm work.

Pass 2 detail (tens digit), applied to Pass 1's output [180, 90, 2, 903, 243, 55, 85, 6]: tens digits are 180→8, 90→9, 2→0, 903→0, 243→4, 55→5, 85→8, 6→0. The three numbers with tens digit 0 — 2, 903, 6 — must keep their relative order from the Pass-1 array, giving the sub-sequence (2, 903, 6); the two numbers with tens digit 8 — 180, 85 — must appear as (180, 85), ordered before the single tens-digit-9 number (90) and after tens-digit-4 (243) and tens-digit-5 (55). Concatenating groups by ascending tens digit (0,4,5,8,9) yields [2, 903, 6, 243, 55, 180, 85, 90] — this is the array shown above, and it is what stability guarantees: the units-digit order (2 before 903 before 6, and 180 before 85) survives unchanged inside each tens-digit group.

Java implementation (LSD radix sort, base 10)

public class RadixSort {
    public static void sort(int[] arr) {
        if (arr.length == 0) return;
        int max = arr[0];
        for (int v : arr) if (v > max) max = v;
        for (int exp = 1; max / exp > 0; exp *= 10) {
            countingSortByDigit(arr, exp);
        }
    }

    private static void countingSortByDigit(int[] arr, int exp) {
        int n = arr.length;
        int[] output = new int[n];
        int[] count = new int[10];

        for (int i = 0; i < n; i++) {
            int digit = (arr[i] / exp) % 10;
            count[digit]++;
        }
        for (int i = 1; i < 10; i++) {
            count[i] += count[i - 1];
        }
        for (int i = n - 1; i >= 0; i--) {
            int digit = (arr[i] / exp) % 10;
            output[count[digit] - 1] = arr[i];
            count[digit]--;
        }
        System.arraycopy(output, 0, arr, 0, n);
    }
}

Pitfalls

When to use / when not — vs Comparison Sorts

Radix SortQuicksort / Merge Sort (named alternative)
TimeO(d·(n+b)), can be linear in nO(n log n) average/worst (merge sort)
SpaceO(n+b), not in-placeO(log n) (quicksort) / O(n) (merge sort)
Works onFixed-width integers / strings onlyAny comparable type
StabilityStable by constructionMerge sort stable; quicksort not

Use Radix Sort when keys are integers or fixed-length strings with bounded digit count and n is large relative to d — e.g. sorting timestamps, IP addresses, fixed-width IDs. Avoid it for floating-point keys, arbitrary objects with custom comparators, very large key ranges relative to n, or when extra O(n) memory is unacceptable — reach for Quicksort/Merge Sort instead.

Takeaways

Recall question: Why must the counting-sort subroutine used inside each digit pass be stable, and what breaks if it isn't?


Synthesized from the original page's step-by-step walkthrough, standard CLRS-style Radix/Counting Sort analysis, and worked-example verification (recomputed by hand, digit by digit, to confirm every intermediate array).

🤖 Don't fully get this? Learn it with Claude

Stuck on Radix Sort Algorithm? Open Claude, copy a block below, and it'll teach you this exact concept — visually and interactively.

🎨 Explain it visually

Build the mental picture, not memorization.

I just read a lesson on **Radix Sort Algorithm** (DSA) and want to truly understand it. Explain Radix Sort Algorithm from first principles using ONE vivid real-world analogy and a visual mental model — draw it as ASCII art or a clear step-by-step diagram — with a concrete example using real numbers. Then ask me one question to check I got the mental picture, and wait for my reply. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
🤔 Walk me through it (interactive)

Socratic — adapts to where you're stuck.

Teach me **Radix Sort Algorithm** interactively. Ask me ONE guiding question at a time, wait for my answer, and adapt to my confusion — build the idea with me step by step instead of explaining it all at once. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
🧪 Quiz me & fix my gaps

Active recall exposes what you missed.

Quiz me on **Radix Sort Algorithm** with 5 questions, easy to tricky, ONE at a time. Tell me if each answer is right; at the end, explain clearly what I got wrong and why. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
🧠 Make it stick

Intuition + hook + flashcards for long-term memory.

Help me remember **Radix Sort Algorithm** for the long term: give the one-sentence intuition, a memorable hook/mnemonic, a tiny worked example, and 3 active-recall flashcards (Q -> A). If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.

📝 My notes