Overview
Radix Sort is a non-comparison-based sorting algorithm that sorts integers by processing individual digits. It processes digits from the least significant digit (LSD) to the most significant digit (MSD), using a stable sorting algorithm (typically Counting Sort) as a subroutine. Radix Sort can achieve linear time complexity when the number of digits is constant.Implementation
How It Works
- Find Maximum: Determine the number with the most digits to know how many passes are needed
- Process Each Digit: Starting from the least significant digit (rightmost)
- Stable Sort: Use a stable sort (Counting Sort) to sort by the current digit
- Move to Next Digit: Multiply the digit position by the radix base and repeat
- Complete: After processing all digits, the array is fully sorted
Radix Sort requires a stable subroutine sorting algorithm. If the subroutine isn’t stable, equal elements might be reordered, breaking the algorithm’s correctness.
Complexity Analysis
Time Complexity
- Best Case: O(d(n + k))
- Average Case: O(d(n + k))
- Worst Case: O(d(n + k))
- Where d = number of digits, k = radix base
Space Complexity
- Space: O(n + k) for auxiliary arrays
- Auxiliary: O(n) for output array
- Counting Array: O(k) where k is radix base
Usage Example
Visual Example
Let’s trace through sorting[170, 45, 75, 90, 2, 802, 24, 66] with radix base 10:
Characteristics
Stability
Stability
Radix Sort is stable when using a stable subroutine (like the Counting Sort implementation shown). Equal elements maintain their relative order.
In-Place
In-Place
No, Radix Sort is not in-place. It requires O(n + k) additional space for the auxiliary and counting arrays.
Adaptive
Adaptive
No, Radix Sort is not adaptive. It processes all digits regardless of the initial order.
Comparison-Based
Comparison-Based
No, Radix Sort is not comparison-based. It processes digits directly, allowing it to achieve linear time complexity.
When to Use
Radix Sort excels when sorting integers with a fixed number of digits or when digit count is small relative to n.
- Sorting integers with a known maximum value
- When the number of digits d is small (d << log n)
- Sorting fixed-length strings or keys
- When linear time sorting is needed
- Parallel processing (each digit can be processed independently)
- Large datasets with small key sizes
- Sorting floating-point numbers (requires conversion)
- Variable-length strings (requires padding)
- Very large digit counts (d approaches n)
- Memory is extremely limited
- Small datasets (overhead not worth it)
Performance Insights
Radix Base Selection
Code Walkthrough
Helper Function: countingSort()
Main Function: radixSort()
LSD vs MSD Radix Sort
Two Variants:LSD (Least Significant Digit) - Shown here:
- Processes digits from right to left
- Simpler to implement
- Always stable
- Sorts entire array at once
- Better for fixed-length keys
- Processes digits from left to right
- More complex (requires recursion)
- Can be faster by early termination
- Better for variable-length keys
- Used in string sorting
Comparison with Other Sorts
| Algorithm | Best Case | Average Case | Worst Case | Space | Stable | Data Type |
|---|---|---|---|---|---|---|
| Radix Sort | O(d(n + k)) | O(d(n + k)) | O(d(n + k)) | O(n + k) | Yes | Integers |
| Counting Sort | O(n + k) | O(n + k) | O(n + k) | O(k) | Yes* | Integers |
| Quick Sort | O(n log n) | O(n log n) | O(n²) | O(log n) | No | Any |
| Merge Sort | O(n log n) | O(n log n) | O(n log n) | O(n) | Yes | Any |
| Heap Sort | O(n log n) | O(n log n) | O(n log n) | O(1) | No | Any |
Real-World Applications
Where Radix Sort is Used:
- Suffix Array Construction: Text indexing algorithms
- IP Address Sorting: Network routing tables
- Date Sorting: Year-month-day format
- Card Sorting: Suit and rank
- Lexicographic Ordering: Dictionary sorting
- Database Indexing: Integer key sorting
- Parallel Processing: GPU sorting algorithms
Optimizations
Handling Negative Numbers
To sort arrays with negative numbers:Why It’s Called “Radix”
The term “radix” is Latin for “root” and in mathematics refers to the base of a number system. Radix Sort is named after this because it processes numbers digit-by-digit in a particular radix (base), such as:
- Radix 10 (decimal): digits 0-9
- Radix 2 (binary): digits 0-1
- Radix 16 (hexadecimal): digits 0-F
- Radix 256 (byte): values 0-255