Speed:
Radix Sort
Time: O(d × (n + k))Space: O(n + k)Stable & Non-comparison
🔢How Radix Sort Works
Radix Sort is a non-comparison based sorting algorithm that sorts integers by processing individual digits. It works by sorting the numbers digit by digit, starting from the least significant digit (rightmost) to the most significant digit (leftmost).
The key insight is to use a stable sorting algorithm (like counting sort) for each digit position:
- Find maximum: Determine the number of digits needed
- Process digits: Sort by each digit position using counting sort
- Maintain stability: Preserve relative order of equal elements
- Linear time: Achieves O(d×(n+k)) where d is digit count
💻Implementation
📊Algorithm Analysis
Time Complexity
- • Best/Average/Worst: O(d × (n + k))
- • d: number of digits
- • n: number of elements
- • k: range of digits (usually 10)
Key Properties
- • Non-comparison based: doesn't compare elements
- • Stable: maintains relative order
- • Linear time: when d is constant
- • External sorting: works well with large data
🔄 Radix Sort Variants
- • LSD (Least Significant Digit): Start from rightmost digit
- • MSD (Most Significant Digit): Start from leftmost digit
- • Binary radix sort: Base-2 for binary data
- • String radix sort: For lexicographic ordering
🎯 Best Use Cases
- • Integer sorting: Fixed-width integers
- • String sorting: Fixed-length strings
- • Database indexing: Numeric keys
- • External sorting: Large datasets on disk