Sunday, March 21, 2010

A PROGRAM TO SORT AN ARRAY OF NUMERICAL DATA USING RADIX SORT

#include"stdio.h"
#include"conio.h"
#include"math.h"
void radix(int *a,int n,int dig)
{

int aux[10][100],val[100],key[10];
int m,calc,eval,exp,hold;
int i,j,k,z,pass=0;
while(pass!=dig)
{
for(i=0;i<10;i++) /*here we reset value for 2nd part of 2-D array*/
key[i]=0;
pass++;
m=0; /*counter value reset for entering data into array a[]*/
printf("\n\nPass - %d started\n\n",pass);
for(j=0;j{
calc=a[j];
exp=pow(10,pass-1);
k=calc/exp;
k=k%10;
hold=key[k]; /*pocket position found to enter the entry*/
key[k]++; /*incremented value for 2nd part of 2-D array for each pocket*/
aux[k][hold]=a[j];/*1st par represent pocket */
/*2nd part for input of entry in each pocket*/
}
for(j=0;j<10;j++)
{
eval=key[j];
for(z=0;z{
a[m]=aux[j][z];/*each value of aux[][] entered into a[] in ascending order*/
printf("aux[%d][%d]=%d\n",j,z,a[m]);
m++;
}
}
for(i=0;iprintf("%5d",a[i]);/*value printed after each pass*/
}
}/*end of radix()*/

void main()
{
int var,a[100],num_entry;
int digit=0,i;
clrscr();
puts("\nEnter the no. of entries to be sorted");
scanf("%d",&num_entry);
puts("\nPlease enter the no. of digits for each entry");
scanf("%d",&digit);
puts("\nEnter the array digits");
for(i=0;iscanf("%d",&a[i]);
radix(a,num_entry,digit);
getch();
}

No comments:

Post a Comment