Principles and Implementation of Radix Sort
Radix sort is a non-comparative integer sorting algorithm that sorts numbers by processing individual digits sequentially. It works by distributing integers into buckets based on each digit, from the least significant digit to the most significant digit. This algorithm is suitable for data with fixed-length digits, such as integers or strings.
Algorithm Principles
The core idea of radix sort is "digit-by-digit sorting," proceeding from the Least Significant Digit (LSD) to the Most Significant Digit (MSD). Each sorting pass must use a stable sorting algorithm (typically counting sort) to ensure the results from previous passes are preserved.
Detailed Steps
-
Determine the maximum number of digits: First, find the largest number in the array to determine its digit count, d, which defines the number of sorting passes required.
-
Sort starting from the least significant digit: Begin sorting from the units place (least significant digit), then move to the tens, hundreds, and so on, up to the most significant digit.
-
Use a stable sorting algorithm: For each digit, a stable sorting algorithm (e.g., counting sort) must be used to maintain the relative order of numbers with identical digits.
-
Repeat until the most significant digit: Repeat steps 2-3 until all digits have been processed.
Counting Sort as a Subroutine
Radix sort typically uses counting sort as its subroutine:
- Count the frequency of each digit.
- Compute the prefix sum to determine the final position of each digit.
- Traverse the original array from the end to the beginning and place elements in their correct positions.
Time Complexity Analysis
- Best case: O(d × (n + k)), where d is the maximum number of digits, n is the number of elements, and k is the radix (usually 10).
- Worst case: O(d × (n + k))
- Average case: O(d × (n + k))
Space Complexity
Additional space of O(n + k) is required to store intermediate results.
Code Implementation (Python)
def radix_sort(arr):
# Find the maximum value to determine the number of digits
max_val = max(arr)
exp = 1 # Start from the units place
# Perform radix sort using counting sort
while max_val // exp > 0:
counting_sort(arr, exp)
exp *= 10
def counting_sort(arr, exp):
n = len(arr)
output = [0] * n
count = [0] * 10 # Digits 0-9
# Count the frequency of each digit
for i in range(n):
index = (arr[i] // exp) % 10
count[index] += 1
# Compute prefix sums
for i in range(1, 10):
count[i] += count[i-1]
# Traverse from the end to maintain stability
for i in range(n-1, -1, -1):
index = (arr[i] // exp) % 10
output[count[index]-1] = arr[i]
count[index] -= 1
# Copy the results back to the original array
for i in range(n):
arr[i] = output[i]
# Test example
arr = [170, 45, 75, 90, 2, 802, 24, 66]
radix_sort(arr)
print("Sorted result:", arr) # Output: [2, 24, 45, 66, 75, 90, 170, 802]
Algorithm Characteristics
- Stability: Radix sort is a stable sorting algorithm.
- Applicability: Particularly suitable for sorting integers, especially those with a limited number of digits.
- Limitations: Not suitable for sorting floating-point numbers and requires additional memory space.
By cleverly processing digits, radix sort avoids the O(n log n) lower bound of comparison-based sorting algorithms and demonstrates excellent performance in specific scenarios.