Radix Sort is a non-comparative integer sorting algorithm that sorts numbers by processing individual digits. It works by sorting the elements based on their individual digits starting from the least significant digit (LSD) to the most significant digit (MSD) or vice versa. Radix Sort is particularly efficient for sorting integers or strings when the number of digits (or the number of characters in the strings) is relatively small compared to the number of items to be sorted.
Radix Sort uses a stable sorting algorithm (like Counting Sort or Bucket Sort) as a subroutine to sort the digits of the numbers. This allows Radix Sort to sort the entire array in O(nk) time, where n is the number of elements in the array and k is the number of digits in the largest number.
Radix Sort operates by repeatedly sorting the elements according to individual digits. Here's a step-by-step breakdown of the process:
Identify the Maximum Element: First, we determine the maximum number in the array to find out the number of digits (k) in the largest number. This will guide the number of passes the algorithm will make.
Sort by Each Digit: Starting with the least significant digit (LSD), the algorithm sorts the numbers based on that digit. After sorting based on the least significant digit, we move to the next more significant digit, and so on, until we have processed all the digits.
Stable Sorting: A stable sorting algorithm (like Counting Sort) is used to sort the numbers based on each digit. A stable sort preserves the relative order of equal elements, which is essential for Radix Sort to work correctly.
Repeat: This process is repeated for each digit, from the least significant to the most significant, until all digits have been processed.
Let’s break down the algorithm into steps:
// Function to perform counting sort based on the digit represented by exp
void countingSortByDigit(int arr[], int n, int exp) {
int output[n]; // Output array to store sorted numbers
int count[10] = {0}; // Count array to store frequency of digits (0-9)
// Store count of occurrences of digits in count[]
for (int i = 0; i < n; i++) {
count[(arr[i] / exp) % 10]++;
}
// Change count[i] to contain the actual position of this digit in output[]
for (int i = 1; i < 10; i++) {
count[i] += count[i - 1];
}
// Build the output array
for (int i = n - 1; i >= 0; i--) {
output[count[(arr[i] / exp) % 10] - 1] = arr[i];
count[(arr[i] / exp) % 10]--;
}
// Copy the sorted numbers into the original array
for (int i = 0; i < n; i++) {
arr[i] = output[i];
}
}
// Main function to implement Radix Sort
void radixSort(int arr[], int n) {
// Find the maximum number to know the number of digits
int max = *max_element(arr, arr + n);
// Do counting sort for every digit. The exp is 10^i where i is the current digit.
for (int exp = 1; max / exp > 0; exp *= 10) {
countingSortByDigit(arr, n, exp);
}
}
Let’s walk through an example of how Radix Sort works:
Suppose we have the following array:
arr = [170, 45, 75, 90, 802, 24, 2, 66]
The largest number in the array is 802, which has 3 digits.
First, we sort the array based on the units place (least significant digit). We use Counting Sort to sort based on this digit.
0, 5, 5, 0, 2, 4, 2, 6After sorting by the units digit, the array becomes:
arr = [170, 90, 802, 2, 24, 45, 75, 66]
Next, we sort based on the tens place.
7, 9, 0, 0, 2, 4, 7, 6After sorting by the tens digit, the array becomes:
arr = [802, 2, 24, 45, 66, 170, 75, 90]
Finally, we sort based on the hundreds place.
8, 0, 0, 0, 0, 1, 0, 0After sorting by the hundreds digit, the array becomes:
arr = [2, 24, 45, 66, 75, 90, 170, 802]
Now, the array is sorted!
The time complexity of Radix Sort depends on two factors:
The time complexity can be expressed as:
Where n is the number of elements and k is the number of digits in the largest number. The value of k is typically small compared to n, which makes Radix Sort efficient for sorting large datasets with relatively small key sizes (e.g., integers or fixed-length strings).
k digits in the largest number. So, we perform k iterations of sorting.Thus, the overall time complexity is O(n * k).
The space complexity of Radix Sort is mainly determined by the space needed for the output array and the count array used in Counting Sort. Since both are proportional to the number of elements n, the space complexity is:
Where n is the number of elements and k is the base (usually 10, for decimal numbers). This makes the space complexity linear in terms of the number of elements.
k) is small relative to the number of elements (n), the time complexity can approach O(n), making Radix Sort very efficient.Radix Sort is an efficient sorting algorithm for sorting integers or fixed-length strings, especially when the number of digits is small compared to the number of elements. It operates by sorting elements based on individual digits and using a stable sub-sorting algorithm (like Counting Sort). With a time complexity
Open this section to load past papers