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
int data ;
struct node *next;
int digit(int num, int k);
struct node *temp,*q;
printf("Enter the Number of Elements to be Inserted into the List : ");
printf("Enter element %d : ",count+1);
temp= malloc(sizeof(struct node));
printf("Sorted List is :\n");
while( q !=NULL)
printf("%d ", q->data);
q = q->next;
struct node *p, *rear, *front;
for(k=least_sig; k<=most_sig; k++)
for(count=0; count<=9 ; count++)
rear[count] = NULL;
front[count] = NULL ;
for( p=start; p!=NULL; p=p->next )
dig = digit(p->data, k);
if(front[dig] == NULL)
front[dig] = p ;
rear[dig]->next = p;
rear[dig] = p;
while(front[count] == NULL)
start = front[count];
struct node *p=start;
while(p != NULL)
if(p ->data > large)
large = p->data;
p = p->next ;
while(large != 0)
large = large/10 ;
int digit(int num, int k)
int digit, count ;
for(count=1; count<=k; count++)
digit = num % 10 ;
num = num /10 ;
If you found anything incorrect or have doubts regarding above radix sort program in C then comment below.