Tweet
#include<stdio.h>
void radix(int a[],int n)
{
int i,j,k=1,l,temp,b[10][n],cnt;
int max=a[0],c=0;
for(i=0;i<n;i++)
if(a[i]>max)
max=a[i];
while(max%10)
{
c++;
max/=10;
}
for(i=0;i<10;i++)
for(j=0;j<n;j++)
b[i][j]=-999;
for(l=0;l<c;l++)
{
for(j=0;j<n;j++)
{
temp=(a[j]/k)%10;
b[temp][j]=a[j];
}
k=k*10; cnt=0;
for(i=0;i<10;i++)
for(j=0;j<n;j++)
if(b[i][j]!=-999)
a[cnt++]=b[i][j];
for(i=0;i<10;i++)
for(j=0;j<n;j++)
b[i][j]=-999;
}
}
main()
{
int i,n;
printf("Enter array capacity \t: ");
scanf("%d",&n);
int a[n];
for(i=0;i<n;i++)
a[i]=rand()%100;
printf("\nArray Before Sorting : ");
for(i=0;i<n;i++)
printf("%4d",a[i]);
radix(a,n);
printf("\n\nArray After Sorting : ");
for(i=0;i<n;i++)
printf("%4d",a[i]);
printf("\n");
}
Radix Sort in C programming
Posted by
LAHAUL SETH
~
Radix Sort in C programming
2011-11-29T10:57:00+05:30
LAHAUL SETH
Programming in C
|
Sorting
|
Comments
Radix Sort in C programming
2011-11-29T10:57:00+05:30
LAHAUL SETH
Programming in C
|
Sorting
|