Radix Sort Program in C

In this tutorial you will learn about radix sort program in C.

Radix sorting technique is one of the oldest techniques of sorting.

Lets assume that we are given a list of some names and asked to sort them alphabetically. We would normally proceed by first dividing the names into 26 different sets (since, there are total of 26 alphabets) with each set containing names that start with the same alphabet. In the first set, we will put all the names which start with alphabet ‘A’. In the Second Set, we will put all the names that start with ‘B’ and so on.

Also Read: Quick Sort in C [Program & Algorithm]

After this comparison of the first alphabets of all the names, each set is further divided into different sub-sets according to the second alphabet of the names.

This process continues till the list becomes completely sorted.

However, the drawback of radix sorting technique is that we have to keep track of lots of sets and its sub-sets simultaneously.

To ease out on this drawback, there are two methods available in radix sorting:

1. Most Significant Digit Radix Sort (MSD)

2. Least Significant Digit Radix Sort (LSD)

In least significant digit radix sort, sorting is performed digit by digit starting from the least significant digit and ending at the most significant digit.

The concept of most significant sorting is to divide all the digits with an equal value into their own set and then do the same thing with all the sets until all of them are sorted.

 

Analysis of Radix Sort ALgorithm

The number of passes p is equal to the number of digits in largest element and in every pass (iteration) we have n operations where n is the total number of elements. In this program we can see that the outer loop iterates p times and inner loop iterates n times. Hence, run time complexity of radix sort is denoted by O(p*n).

If the number of digits in largest element is equal to n, then the run time becomes O(n square) which is the worst case scenario in radix sort. The best case scenario of radix sort is if the number of digits in largest element is Log n. In this case, the run time becomes O(n log n).

The major disadvantage of radix sort is the need for extra memory for maintaining Queues and hence it is not an in-place sort. It is therefore a stable sort.

 

Radix Sort Program in C

 

Output

Radix Sort Program in C

 

If you found anything incorrect or have doubts regarding above radix sort program in C then comment below.

Leave a Comment

Your email address will not be published. Required fields are marked *