Web Analytics

🃏 Radix Sort

Intermediate ~12 min read

You have a deck of playing cards to sort. Instead of comparing every card, you have a clever strategy: Sort by suit first, then by rank. Or for numbers: Sort by ones digit, then tens, then hundreds. Each pass uses Counting Sort. That's Radix Sort - extending Counting Sort to larger ranges!

The Card Sorter

Imagine a machine with 10 buckets (0-9). Feed numbers through once for each digit position:

The Strategy (LSD - Least Significant Digit)

  1. Pass 1: Sort by ones digit (rightmost) using Counting Sort
  2. Pass 2: Sort by tens digit using Counting Sort
  3. Pass 3: Sort by hundreds digit using Counting Sort
  4. Continue: Until all digits processed
  5. Result: Fully sorted!

Key insight: Each digit has small range (0-9), perfect for Counting Sort!

Extending Counting Sort

The Breakthrough

Counting Sort limitation: Only works for small ranges (like 0-100)

Radix Sort solution: Sort digit-by-digit!

  • Each digit: Small range (0-9) ✓
  • Multiple passes: d passes for d digits
  • Total time: O(d × n) - linear for fixed d!
  • Uses: Stable Counting Sort for each digit

See It in Action

Watch how Radix Sort processes each digit position:

Understanding the Process

  • Pass 1 (ones): Sort by rightmost digit
  • Pass 2 (tens): Sort by second digit (stable!)
  • Pass 3 (hundreds): Sort by third digit (stable!)
  • Each pass: O(n + k) where k=10
  • Total: O(d × n) for d digits
  • Stability crucial: Preserves previous sorting!

The Code

Output
Click Run to execute your code
Why Stability Matters: Without stable sorting, each pass would destroy the work of previous passes! After sorting by ones digit, numbers with the same tens digit must stay in order by ones digit. That's why we use stable Counting Sort!

Quick Quiz

  • Why does LSD Radix Sort start from the rightmost digit?
    Show answer Because of stability! After sorting by ones digit, all numbers ending in 0 are together, all ending in 1 are together, etc. When we sort by tens digit (stably!), numbers with the same tens digit stay in order by ones digit. This builds up the final sorted order digit by digit. Starting from the left (MSD) would require more complex recursive logic!
  • When would Radix Sort be slower than Quick Sort?
    Show answer When d (number of digits) is large! If sorting 64-bit integers (d=20 digits), Radix Sort is O(20n). Quick Sort is O(n log n) ≈ O(20n) for n=1M. They're comparable! But for variable-length or very large d, Quick Sort wins. Radix Sort is best for fixed-length keys with small d!

Why O(d × n)?

Let's break down the complexity:

Step Time Why
Find max (get d) O(n) One scan to find number of digits
Each pass O(n + k) Counting Sort for one digit (k=10)
Number of passes d One per digit position
Total O(d × (n + k)) d passes × O(n + 10) each
Simplified O(d × n) Since k=10 is constant

✅ When is it Linear?

If d is constant (fixed-length keys):

  • Phone numbers: d=10 → O(10n) = O(n) ✓
  • SSN: d=9 → O(9n) = O(n) ✓
  • 3-digit numbers: d=3 → O(3n) = O(n) ✓

Linear time for fixed-length keys!

When to Use Radix Sort

✅ Perfect For:

  • Phone numbers: Fixed 10 digits
  • Social Security numbers: Fixed 9 digits
  • IP addresses: Fixed 4 bytes
  • Fixed-length strings: Same length
  • Database integer keys: Bounded range

Rule of thumb: If d is small and fixed, Radix Sort wins!

❌ Avoid When:

  • Variable-length keys: Different d values
  • Large d: Many digits (64-bit integers)
  • Floating-point: Not integers
  • Need in-place: Radix needs O(n) space

Radix Sort vs Others

Feature Counting Sort Radix Sort Quick Sort
Type Non-comparison Non-comparison ✓ Comparison
Time O(n + k) O(d × n) ✓ O(n log n)
Space O(k) O(n + k) O(log n) ✓
Stable Yes ✓ Yes ✓ No
Use When Small range Fixed-length ✓ General purpose

The Relationship

Radix Sort builds on Counting Sort:

  • Counting Sort: Single pass, small range only
  • Radix Sort: Multiple passes, extends to larger ranges!
  • Each Radix pass: Uses Counting Sort on one digit
  • Together: Complete the non-comparison sorting toolkit!

Common Misconceptions

❌ Misconception 1: "Radix Sort is always O(n)"

Reality: It's O(d × n) - depends on d!

  • Fixed d: O(n) ✓ (phone numbers, d=10)
  • Growing d: Not O(n)! (64-bit ints, d=20)
  • Variable d: Worst case dominates

Only linear for constant d!

❌ Misconception 2: "Radix Sort doesn't need stability"

Reality: Stability is CRUCIAL!

  • Without stability: Each pass destroys previous work
  • Example: [12, 22] sorted by tens → both have 1
  • If unstable: Could become [22, 12] after ones sort!
  • With stability: Preserves order, builds up sorting

Must use stable Counting Sort!

❌ Misconception 3: "Radix Sort is faster than Quick Sort"

Reality: Depends on d and n!

  • Small d: Radix wins (d=3: O(3n) vs O(n log n))
  • Large d: Quick wins (d=20: O(20n) vs O(n log n))
  • For n=1M: O(n log n) ≈ O(20n)
  • Crossover: Depends on constants and cache

Benchmark for your specific use case!

💡 Key Takeaway

Radix Sort is a powerful extension of Counting Sort:

  • ✅ O(d × n) - linear for fixed-length keys
  • ✅ Non-comparison - breaks O(n log n) barrier
  • ✅ Stable - preserves order
  • ✅ Perfect for phone numbers, IDs, fixed-length data
  • ⚠️ Requires stability - must use stable Counting Sort
  • ⚠️ Not for variable-length or large d

Understanding Radix Sort completes your non-comparison sorting toolkit!

Summary

The 3-Point Radix Sort Guide

  1. Strategy: Sort digit-by-digit using stable Counting Sort - extends to larger ranges!
  2. Complexity: O(d × n) - linear for fixed-length keys!
  3. Trade-off: Speed for fixed-length vs limited to integers/strings

Interview tip: Mention Radix Sort for fixed-length integer keys. Explain digit-by-digit sorting, why stability is crucial, and when it beats comparison sorts!

What's Next?

You've mastered non-comparison sorting! Next: Tim Sort - the hybrid algorithm that powers Python and Java!

Practice: Implement Radix Sort for sorting phone numbers. Try both LSD and MSD approaches!