Radix Sort AlgorithmIn this article, we will discuss the Radix sort Algorithm. Radix sort is the linear sorting algorithm that is used for integers. In Radix sort, there is digit by digit sorting is performed that is started from the least significant digit to the most significant digit. The process of radix sort works similar to the sorting of students names, according to the alphabetical order. In this case, there are 26 radix formed due to the 26 alphabets in English. In the first pass, the names of students are grouped according to the ascending order of the first letter of their names. After that, in the second pass, their names are grouped according to the ascending order of the second letter of their name. And the process continues until we find the sorted list. Now, let's see the algorithm of Radix sort. AlgorithmWorking of Radix sort AlgorithmNow, let's see the working of Radix sort Algorithm. The steps used in the sorting of radix sort are listed as follows -
Now let's see the working of radix sort in detail by using an example. To understand it more clearly, let's take an unsorted array and try to sort it using radix sort. It will make the explanation clearer and easier. ![]() In the given array, the largest element is 736 that have 3 digits in it. So, the loop will run up to three times (i.e., to the hundreds place). That means three passes are required to sort the array. Now, first sort the elements on the basis of unit place digits (i.e., x = 0). Here, we are using the counting sort algorithm to sort the elements. Pass 1:In the first pass, the list is sorted on the basis of the digits at 0's place. ![]() After the first pass, the array elements are - ![]() Pass 2:In this pass, the list is sorted on the basis of the next significant digits (i.e., digits at 10th place). ![]() After the second pass, the array elements are - ![]() Pass 3:In this pass, the list is sorted on the basis of the next significant digits (i.e., digits at 100th place). ![]() After the third pass, the array elements are - ![]() Now, the array is sorted in ascending order. Radix sort complexityNow, let's see the time complexity of Radix sort in best case, average case, and worst case. We will also see the space complexity of Radix sort. 1. Time Complexity
Radix sort is a non-comparative sorting algorithm that is better than the comparative sorting algorithms. It has linear time complexity that is better than the comparative algorithms with complexity O(n logn). 2. Space Complexity
Implementation of Radix sortNow, let's see the programs of Radix sort in different programming languages. Program: Write a program to implement Radix sort in C language. Output: After the execution of the above code, the output will be - ![]() Program: Write a program to implement Radix sort in C++. Output: ![]() Program: Write a program to implement Radix sort in C#. Output: ![]() Program: Write a program to implement Radix sort in Java. Output: ![]() So, that's all about the article. Hope the article will be helpful and informative to you.
Next TopicSelection Sort
|