Techknow Study

Radix sort....

8:54:00 PM vikas 0 Comments Category :

 RADIX SORT

 In Radix sort is sorting is done on the base of place element{DIGIT}
exam: in 256 "6" is in ones place.


It is mainly use in arranging the name or we  can say English word. if you use it to arrange the number so please take equal place numbers. Otherwise it may give wrong output.    


#include<conio.h>
#include<stdio.h>
void radix_sort(int *a,int n);
void main()
{
int i,n;
int a[20];
clrscr();
printf("enter the no  of element");
scanf("%d",&n);
for(i=0;i<=n;i++)
{
scanf("%d",&a[i]);
}
radix_sort(a,n);
for(i=0;i<=n;i++)
{
printf("%d\n",&a[i]);
}
getch();
}
void radix_sort(int *a,int n)
{
int bucket[10][5],buck[10];
int i,j,k,l,num,div,large,passes;
div=1;
num=0;
printf("radix sort output is:");
large=a[10];
for(i=1;i<=n;i++)
{
if(a[i]>large)
large=a[i];
}
while(large>0)
{
num++;
large=large/10;
for(passes=0;passes<num;passes++)
{
for(k=0;k<=10;k++)
buck[k]=0;
for(i=0;i<=n;i++)
{
l=((a[i]/div)%10);
bucket[i][buck[l]++]=a[i];
i=0;
for(k=0;k<=10;k++)
{
for(j=0;j<=buck[k];j++)
a[i++]=bucket[k][j];
}
div=div*10;
}
}



RELATED POSTS

0 comments