Overview & Concept
Radix Sort is a non-comparative integer sorting algorithm that sorts data by processing individual digits, either from Least Significant Digit (LSD) to Most Significant Digit (MSD). It uses a stable sub-sorting algorithm (typically Counting Sort) for each digit place value, achieving linear O(d × (n + b)) time.
How It Works
Digit Extraction: Numbers are evaluated digit-by-digit from units, tens, hundreds, up to max digits d.
Stable Bucket Sorting: A stable sorting pass (Counting Sort with base b = 10) sorts items by the current digit place.
Preserved Order: Because each pass is stable, earlier sorted lower digits remain in correct relative order.
Step-by-Step Execution
Asymptotic Complexity Analysis
| Metric | Best Case | Average Case | Worst Case | Space Complexity |
|---|---|---|---|---|
| Complexity Bound | O(d · (n + k)) | O(d · (n + k)) | O(d · (n + k)) | O(n + k) |
| Explanation | Optimal input arrangement minimizing operations. | Expected time for uniformly distributed randomized inputs. | Adversarial degenerate conditions requiring full traversal. | Auxiliary memory allocated beyond original input size. |
Pseudocode & Invariants
procedure radixSort(A):
find maximum value in A
exp = 1 // start with least significant digit
while max / exp > 0:
distribute elements into 10 buckets by digit (A[i]/exp)%10
collect elements back from buckets in stable order
exp *= 10 // move to next digit place
return A
end while
return A- Digit Subarray Sortedness: After pass p, the array is strictly sorted according to the lower p digits.
Advantages & Disadvantages
Key Advantages
- •Linear O(d × n) time complexity: significantly outperforms comparison sorts when digit count d is small.
- •Stable sorting.
Trade-offs & Limitations
- •Requires O(n + b) auxiliary memory for output and bucket arrays.
- •Performance degrades if keys are extremely wide with large d.
Real-World Applications
Fixed-Length Key Sorting
Sorting phone numbers, postal codes, and IP addresses.
Suffix Array Construction
Fast suffix sorting in bioinformatics and text search engines.
Decision Guide: When to Use
Ideal Scenarios
- ✓When sorting large volumes of fixed-width integers or strings.
Consider Alternatives When
- ✕When key lengths vary wildly or comparison-based sorts are sufficient.
Explore Related Algorithms
Compare trade-offs and complexity across complementary tools in AlgoViz.