Radix Sort Algorithm in Data Structure
Radix Sort is a non-comparative algorithm that sorts the data in lexicographical (dictionary) order using Counting Sort as a subroutine. It is ideal for sorting integers digit by digit and strings character by character, it distributes elements into buckets by digit value, sorting repeatedly from least to most significant digit for final order.
Radix Sort Algorithm
In counting sort, we have seen that we can sort an array based on the frequency of elements in the array. But the only difficulty there was if the range of elements is very large then it's not efficient to use counting sort.
So we have come up with the Radix sort where we apply counting sort digit by digit from least-significant digit to most-significant digit.
Radix sort is a stable sorting method that uses counting sort as a subroutine. It is recommended to use radix sort on positive numbers only, however with some modification to standard radix sort algorithm we can also sort an array containing negative elements.
Steps Involved in Radix sort to sort an array in ascending order are --
- Find the maximum element of the array, let it be
- Find the number of digits in , let it be .
- For each, ranging from To , apply the counting sort algorithm for the least-significant digit of each element. If any element has less than digits consider at its place (Because can also be represented as ).
Using this method you can also sort numbers that fit in 32/64-bit data types (int, float, double, etc) which was practically not possible with counting sort algorithm.
Note - If you haven't gone through Counting Sort yet, please have an eye on it once as it is a prerequisite for Radix Sort and if you want to sort an array in descending order, then tweak the counting sort function such that it sorts array in reverse order.
Example of Radix Sort Algorithm
Let's assume we have an array -
Here the maximum element is 682 which is of 3 digits, so we will apply the counting sort on the least significant digit last digit -
Now we apply counting sort on the second digit of the numbers, after which the numbers will get sorted on the basis of least-significant digits -
Now it's time to sort the numbers based on the digit from right the Most-significant digits. -
And it's done, now the array is sorted in ascending order.
Pseudocode of the Radix Sort
In pseudocode, we will be having the function RadixSort which is the main function implementing the Radix sort functionality.
Firstly, the maximum element of the array is determined, then countingSort function is called times where is the number of digits in the maximum element found in the previous step.
Implementation of Radix Sort
For implementing the radix sort algorithm we will be using two functions -
- radixSort - It takes an array (say ) and its size (say ) as arguments. Firstly, the maximum element () present in is determined, then countingSort is called with a variable div till where is multiplied by after each iteration.
- countingSort - It takes an array (), size (), and as arguments. Counting sort will sort the array based on the result obtained from . Where in each iteration corresponds to digit of where .
Here denotes, the number of digits present in .
C/C++ Implementation of Radix Sort
Java Implementation of Radix Sort
Python Implementation of Radix Sort
C# Implementation of Radix Sort
Complexity Analysis of Radix Sort
Radix Sort Time Complexity
Best Case - In the best case when the array is already sorted, we do not need to apply the algorithm instead we can check if the array is sorted in a single traversal of the array. Hence time complexity is in the best case.
Worst Case - In the worst case array is sorted in reverse order, we need to apply the counting Sort (an process) for times. Where is the number of digits present in the largest element present in .
Hence, the overall time complexity is
- Average Case - In the average case elements of the array are arbitrarily arranged, again we need to apply counting sort on the array for times. Hence in the average case also, the time complexity is .
Radix Sort Space Complexity
Since we are using a array in the Counting Sort process of size and a array of size the space complexity is .
Where is the base of elements in the original array, since in the above case we are dealing with decimals (base 10), .
Applications of Radix Sort
- It is highly beneficial to run the Radix sort on parallel machines, making the sorting process super-fast.
- It is also used in constructing the suffix_array (An array that contains all possible suffixes of a string in sorted order.)
For example, If str="radix" then suffix array will be -
Code with Confidence! Enroll in Our Online Data Structures Courses and Master Efficient Algorithms.
- Radix sort is a non-comparison sorting algorithm that uses the counting sort algorithm in its implementation.
- Other than integers, other data types like strings can also be sorted in lexicographical order using Radix sort.
- The time complexity of the Radix sort is where is the number of digits present in the largest element of the array.