The ones who are crazy enough to think they can change the world are the ones who do.
- Steve Jobs

Radix Sort

Radix sort algorithm is a non-comparative integer sorting algorithm. Radix sort algorithm is most preferable for unsorted list. Let us consider the following example to understand clearly about the working of radix sort algorithm to arrage the unsorted list of array. Clearly, the number of iteration is based on size of the highest individual number.

Advantages - Radix Sort

  • Radix sort algorithm is well known for its fastest sorting algorithm for numbers and even for strings of letters.
  • Radix sort algorithm is the most efficient algorithm for elements which are arranged in descending order in an array.

Disadvantages - Radix Sort

  • Poor efficieny for most elements which are already arranged in ascending order in an array.
  • When Radix sort algorithm is applied on very small sets of data(numbers or strings), then algorithm runs in 0(n) asymptotic time.

C program - Radix Sort

Here is the program to demonstrate Radix Sort.

radix-sort.c
#include <stdio.h>
int largest(int arr[], int n);
void radix_sort(int arr[], int n);
int main()
{
int arr[10], i, n;
printf("\n Enter the size of the array : ");
scanf("%d",&n);
printf("\nEnter %d elements in the array : \n",n);
for(i = 0; i < n; i++)
scanf("%d", &arr[i]);
radix_sort(arr, n);
printf("\nSorted Array in ascending order is : \n");
for(i = 0; i < n; i++)
printf("%d",arr[i]);
return 0;
}
int largest(int arr[], int n)
{
int large = arr[0], i;
for(i = 1; i < n; i++)
{
if(arr[i] > large)
large = arr[i];
}
return large;
}
void radix_sort(int arr[], int n)
{
int bucket[10][10], bucket_count[10];
int i, j, k, remainder, NOP = 0, divisor = 1, large, pass;
large = largest(arr, n);
while(large > 0)
{
NOP++;
large /= 10;
}
for(pass = 0; pass < NOP; pass++)
{
for(i = 0; i < 10; i++)
bucket_count[i] = 0;
for(i = 0; i < n; i++)
{
remainder = (arr[i]/divisor)%10;
bucket[remainder][bucket_count[remainder]] = arr[i];
bucket_count[remainder] += 1;
}
i = 0;
for(k = 0; k < 10; k++)
{
for(j = 0; j < bucket_count[k]; j++)
{
arr[i] = bucket[k][j];
i++;
}
}
divisor *= 10;
}
}
Enter the size of the array : 5 
Enter 5 elements in the array :
4 3 1 2 5 
Sorted Array in ascending order is : 
1 2 3 4 5

Report Us

We may make mistakes(spelling, program bug, typing mistake and etc.), So we have this container to collect mistakes. We highly respect your findings.

Report

We to update you