4 Radix Sort
4 Radix Sort
4 Radix Sort
Each pass sorts elements according to the digits in a particular place value, using a
stable sorting algorithm (usually counting sort as a subroutine). It can be
implemented to start the sorting from the least significant digit (LSD) or the most
significant digit (MSD).
You might’ve guessed this by now — the number of passes required to fully sort the
elements is equal to the number of place values (digits) present in the largest
element among all the input elements to be sorted. These passes continue to run for
each place value until the sorting is done.
Radix sort generally uses counting sort as an intermediate sorting algorithm. This
article will show you how counting sort is used in radix sort, but we encourage you to
brush up on the counting sort algorithm before reading on.
Radix sort can be applied to data that can be sorted lexicographically, such as
words and integers. It is also used for stably sorting strings.
It is a good option when the algorithm runs on parallel machines, making the
sorting faster. To use parallelization, we divide the input into several buckets,
enabling us to sort the buckets in parallel, as they are independent of each
other.
It is used for constructing a suffix array. (An array that contains all the
possible suffixes of a string in sorted order is called a suffix array. For
example: If the string is “sort,” then the suffix array SA[] will be:
SA[0] = “or”
SA[1] = ”ort”
SA[2] = ”sort”
SA[3] = ”t”
1
Radix Sort Example: LSD to MSD
The following array needs sorting:
Array[] = {455, 61, 63, 45, 67, 135, 74, 49, 15, 5}
We start off by finding the maximum element in the unsorted input array
(maxim). The number of digits (d) in maxim gives us the number of passes we
need to run to get the fully sorted output.
Here, the maxim = 455 and d = 3. This tells us that 3 passes will be required
to fully sort the array
The loop will run once for units place, once for the tens place, and once for
hundreds place before the array is finally sorted.
This array consists of integers in the decimal number system — so the digits
will range from 0 to 9. We have to maintain a count of 10 digits for each place
value since the base (or radix) is 10. Now, we know we’ll need a count array
of size 10.
We start sorting from the least significant digit (LSD).
2
Note: “135 is before 15” and “15 is before 5” despite the same units place
(5) because that’s the order in which they occur in the original array. This is
a feature of stable sorting algorithms.
Once the array is sorted based on units place, we sort the result of the
previous step based on tens place. Finally, we sort the result based on
hundreds place (MSD).
After the final pass, we get the sorted array.
Step 1: Find the largest element in the unsorted input array (Array)
Step 2: Create a for expression that loops d times, where d = number of digits in the largest
element (maxim)
Step 3: For the first place value, call counting sort, jump place value by 10 to move to the
next significant digit
Step 4: Continue step 3 for all place values (finish all d passes)
Step 6: Exit
1. We first find the maximum element in the array (maxim) and then call radix sort on
that array.
2. Within radix sort, we call counting sort “d” times (d = number of digits in maxim). If
all the place values in the maximum number are sorted, the whole array must be
sorted.
3. With each loop, we are jumping to the next significant digit.
4. Radix sort calls counting sort as long as maxim/place > 0 (“place” starts from 1 and
jumps by a factor of 10 each time. This works because “maxim/place > 0” will be
satisfied as many times as the number of digits in the maxim.)
For example, if maxim = 456, 456/1>0, 456/10>0, 456/100>0, 456/1000<=0 and so the loop
will stop after running thrice.
radixSort()
end radixSort()
4
C Code
5
// For each digit in max, calling
// the countingSort for ith digit.
for(int div=1;max/div>0;div*=10){
countingSort(a,n,div);
}
}
// Main function
int main(){
// Initializing the array.
int a[]={4,2,12,6,1,9,21};
int n=7;
// Applying the Radix sort function.
radixSort(a,n);
return 0;
}
6
We’ll now let’s walk through how the radix sort algorithm runs to give the desired
sorted array.
Input array: {455, 61, 63, 45, 67, 135, 74, 49, 15, 5}
Count array:
Count array after storing the count of each unit digit in the original array:
Count array after storing the cumulative count so that their values indicate their
sorted positions:
Next, we go through the original array from right to left (since the cumulative count
will give the last element’s position first).
The rightmost digit in the original array has 5 in the unit place. The value at index 5
of the count array is 8.
So, 8 - 1 = 7 is both the new value at index 5 of the count array as well as the index
at which we will place the rightmost number 5 in the sorted array after the first pass.
Similarly, we find out the positions of the remaining elements in the input array,
moving from right to left.
7
Array after pass 1:
Count array:
Count array after storing the count of each tens digit in the resultant array of pass 1:
Count array after storing the cumulative count so that their values indicate their
sorted positions:
In the second pass, 49 is the rightmost digit with 4 at the tens place. At index 4 of the
count array, the cumulative value is 5
Therefore, 5 - 1 = 4 is both the updated value at index 5 of the count array as well as
the index at which 49 will be placed in the sorted array after the second pass.
8
After pass 2:
Note that after pass 2, all the two-digit numbers are sorted relative to each other.
Count array:
Count array after storing the count of each tens digit in the resultant array of pass 1:
Count array after storing the cumulative count so that their values indicate their
sorted positions:
Like the last two passes, we start from the rightmost digit, 74, which has 0 in its
hundreds place. At the index 0 of the count array, the cumulative value stored is 8.
(We know only three passes are required as the maximum number 455 has only 3
digits.)
9
After pass 3:
The output is this final sorted array after all three passes.
Here,
The time complexity for radix sort is O(d*(n+b)) because counting sort is called d
times.
10
3. Since the radix sort algorithm sorts digit by digit, the number of possible digits
is limited to the base of the number system used. This means that it can sort
multi-digit numbers (like 15 digit numbers) without increasing the range of
digits over which the sorting must be done. The range remains 0 to 9 for
numbers in the decimal number system, regardless of number size. This is an
advantage since an increased range of digits to sort might decrease the
algorithm’s efficiency.
11