DSA Notes
Master radix sort — LSD and MSD variants, how it uses counting sort as a subroutine to sort digit by digit, why it achieves O(nk) time, sorting strings and multi-field records, comparison with counting sort and comparison sorts, and practical applications.
The Core Insight
When numbers have multiple digits, sorting digit by digit (from least significant to most significant) using a stable sort produces the correct final order.
Why does this work? If you sort by units first, then by tens, then by hundreds — a stable sort preserves the earlier digit ordering when digits at the current position are equal. The final result is sorted by all digits together.
| Input | [170, 45, 75, 90, 802, 24, 2, 66] |
| Step 2 — Sort by tens digit (stable | preserves units order for ties): |
| (802 and 2 both have 0 tens | 802 before 2 from previous step) |
| Final sorted | [2, 24, 45, 66, 75, 90, 170, 802] ✓ |
Complexity Analysis
For n numbers each with at most d digits in base b:
- d passes of counting sort
- Each pass: O(n + b) — n elements plus b digit buckets
- Total: O(d × (n + b))
For base 10, b = 10 (constant), so O(d × n).
Choosing the base (b):
| But | larger count array |
| Base 10 | d = 10 digits, 10 passes |
| Base 256 | d = 4 digits (1 byte each), 4 passes — typical choice |
| Base 65536 | d = 2 digits, 2 passes |
| Optimal tradeoff | b ≈ n gives O(n log_n(max_val)) total |
Radix Sort for Strings
MSD Radix Sort (Most Significant Digit First)
MSD processes from the most significant digit, recursively sorting each bucket:
LSD vs MSD:
- LSD: Simpler, works on whole array, better for fixed-length keys
- MSD: More complex, naturally handles variable-length strings, better for early termination on sorted data
Radix Sort vs Other Linear-Time Sorts
| Feature | Counting Sort | Radix Sort | Bucket Sort |
|---|---|---|---|
| Time | O(n + k) | O(d × (n + b)) | O(n) average |
| Range constraint | k must be small | d must be small | Uniform distribution |
| Handles negative | With offset | With modification | With offset |
| Stable | ✅ Yes | ✅ Yes | With stable subroutine |
| Practical k/d | k ≤ 10n | d ≤ log_b(max_val) | Uniform data |
When Radix Sort beats Counting Sort:
| Counting Sort | O(n + k) = O(10^9) — terrible, count array too large |
| Radix Sort (base 10) | d = 10 digits, O(10 × (n + 10)) = O(10n) — fast |
| Radix Sort (base 256) | d = 4 digits, O(4 × (n + 256)) = O(4n) — faster |
When to Use Radix Sort
Use Radix Sort when
✅ Sorting large integers (phone numbers, IDs, timestamps)
✅ Range too large for counting sort
✅ Number of digits d is small relative to n
✅ Stable sort of multi-key records (sort by secondary key, then primary)
Avoid Radix Sort when
❌ Data is floating point (requires careful handling)
❌ Data is arbitrary objects without natural digit decomposition
❌ Memory is very limited (needs O(n + b) extra)
❌ n is small (Quick Sort's constant is smaller)
Multi-Key Sorting Using Radix Sort
LSD Radix Sort naturally handles multi-key record sorting:
Summary
Radix sort: sort digit by digit from least (or most) significant, using a stable subroutine.
| Property | Value |
|---|---|
| Time | O(d × (n + b)) — effectively O(kn) for k digits |
| Space | O(n + b) |
| Stable | ✅ Yes |
| In-place | ❌ No |
| Comparison sort | ❌ No |
| Constraint | Integer (or digit-decomposable) inputs |
Key advantage over counting sort: Works when the value range is large but the number of digits is small. Sorting 1 million 9-digit numbers takes O(9n) time with radix sort — linear.
*Next Lesson: Sorting Comparison*
Exam Focus
Revise definitions, diagrams, examples, and short-answer points for Radix Sort — O(nk) Digit-by-Digit Sorting for Large Integer Ranges.
Interview Use
Prepare one clear explanation, one practical example, and one common mistake for this Data Structures & Algorithms topic.
Search Terms
data-structures-algorithms, data structures & algorithms, data, structures, algorithms, sorting, radix, sort
Related Data Structures & Algorithms Topics