Radix Sort
Sorting algorithms are an essential part of computer science and programming. Among the various sorting techniques, Radix Sort stands out due to its unique approach and efficiency in handling certain types of datasets.
What is Radix Sort?
Radix Sort is a non-comparative sorting algorithm that organizes data by processing individual digits. Unlike comparison-based algorithms like QuickSort or MergeSort, Radix Sort leverages the positional value of digits to sort numbers or strings efficiently.
Radix Sort works by sorting the input elements digit by digit, starting from the least significant digit (LSD) to the most significant digit (MSD). The sorting at each digit level is typically performed using a stable algorithm like Counting Sort.
Key Characteristics of Radix Sort:
Stable Sorting: Maintains the relative order of records with equal keys.
Non-Comparative: Doesn't compare elements directly but sorts based on individual digits.
Efficient for Large Keys: Works well when the range of digits is small compared to the size of the dataset.
Step-by-Step Process:
Identify the Maximum Value: Find the largest number in the dataset to determine the number of digits (or passes).
Sort by Each Digit:
Start with the least significant digit (LSD).
Use Counting Sort (or any stable sorting method) to arrange numbers based on this digit.
Move to the next significant digit and repeat.
Repeat for All Digits: Continue the process until the most significant digit (MSD) is sorted.
Example:
Consider an array: [170, 45, 75, 90, 802, 24, 2, 66]
Step 1: Sort by the Least Significant Digit (LSD)
After sorting by the LSD, the array becomes: [170, 90, 802, 2, 24, 45, 75, 66]
Step 2: Sort by the Next Digit
After sorting by the tens place, the array becomes: [802, 2, 24, 45, 66, 170, 75, 90]
Step 3: Sort by the Most Significant Digit (MSD)
Finally, sorting by the hundreds place: [2, 24, 45, 66, 75, 90, 170, 802]
Implementation in Python
Here’s how you can implement Radix Sort in Python:
def counting_sort(arr, exp):
n = len(arr)
output = [0] * n
count = [0] * 10
# Count occurrences of each digit
for i in range(n):
index = (arr[i] // exp) % 10
count[index] += 1
# Update count to have actual positions
for i in range(1, 10):
count[i] += count[i - 1]
# Build the output array
for i in range(n - 1, -1, -1):
index = (arr[i] // exp) % 10
output[count[index] - 1] = arr[i]
count[index] -= 1
# Copy the output to the original array
for i in range(n):
arr[i] = output[i]
def radix_sort(arr):
max_val = max(arr)
exp = 1
# Perform counting sort for each digit
while max_val // exp > 0:
counting_sort(arr, exp)
exp *= 10
# Example usage
arr = [170, 45, 75, 90, 802, 24, 2, 66]
radix_sort(arr)
print("Sorted array:", arr)
Complexity Analysis
Time Complexity: O(d * (n + b))
d: Number of digits in the largest number.
n: Number of elements in the dataset.
b: Base of the number system (e.g., 10 for decimal).
Space Complexity: O(n + b)
Radix Sort is most efficient when d
is relatively small compared to n
.
When to Use Radix Sort
Radix Sort is ideal in scenarios where:
The range of digits (or characters) is small.
The dataset contains integers or fixed-length strings.
Stability in sorting is critical.
However, it may not be the best choice for datasets with:
Large ranges of keys.
High memory constraints.
Conclusion
Radix Sort offers a unique and efficient approach to sorting, especially for large datasets with small key ranges. By leveraging digit-based sorting, it avoids the pitfalls of direct comparison, making it a powerful tool in the right scenarios. Understanding its mechanism and implementation can broaden your algorithmic toolkit and improve your problem-solving skills.