
//Program to sort an array using  Radix sort in C++.
#include<iostream.h>
#include<conio.h>
void main()
{
int a[50],i,n;
void radixsort(int[],int);
clrscr();
cout<<endl<<" Enter the number of elements in array: ";
cin>>n;
cout<<endl<<"enter the elements of array: ";
for(i=0;i<n;i++)
{
cin>>a[i];
}
radixsort(a,n);
cout<<endl<<"The sorted array is as follow:"<<endl;
for(i=0;i<n;i++)
{
cout<<a[i]<<" ";
}
getch();
}
void radixsort(int a[],int n)
{
int bucket[10][20],bucketcount[10];
int i,j,k,r,digitcount=0,divisor=1,largest,passno;
largest=a[0];
for(i=0;i<n;i++)
{
if(a[i]>largest)
{
largest=a[i];
}
}
while(largest>0)
{
digitcount++;
largest=largest/10;
}
for(passno=0;passno<digitcount;passno++)
{
for(k=0;k<10;k++)
{
bucketcount[k]=0;
}
for(i=0;i<n;i++)
{
r=(a[i]/divisor)%10;
bucket[r][bucketcount[r]++]=a[i];
}
i=0;
for(k=0;k<10;k++)
{
for(j=0;j<bucketcount[k];j++)
{
a[i]=bucket[k][j];
i++;
}
}
divisor=divisor*10;
}
}
#include<iostream.h>
#include<conio.h>
void main()
{
int a[50],i,n;
void radixsort(int[],int);
clrscr();
cout<<endl<<" Enter the number of elements in array: ";
cin>>n;
cout<<endl<<"enter the elements of array: ";
for(i=0;i<n;i++)
{
cin>>a[i];
}
radixsort(a,n);
cout<<endl<<"The sorted array is as follow:"<<endl;
for(i=0;i<n;i++)
{
cout<<a[i]<<" ";
}
getch();
}
void radixsort(int a[],int n)
{
int bucket[10][20],bucketcount[10];
int i,j,k,r,digitcount=0,divisor=1,largest,passno;
largest=a[0];
for(i=0;i<n;i++)
{
if(a[i]>largest)
{
largest=a[i];
}
}
while(largest>0)
{
digitcount++;
largest=largest/10;
}
for(passno=0;passno<digitcount;passno++)
{
for(k=0;k<10;k++)
{
bucketcount[k]=0;
}
for(i=0;i<n;i++)
{
r=(a[i]/divisor)%10;
bucket[r][bucketcount[r]++]=a[i];
}
i=0;
for(k=0;k<10;k++)
{
for(j=0;j<bucketcount[k];j++)
{
a[i]=bucket[k][j];
i++;
}
}
divisor=divisor*10;
}
}
 Bimalpreet Singh
Bimalpreet Singh



 0 Comments
0 Comments
 

 :))
 :))
 ;))
 ;))
 ;;)
 ;;)
 :D
 :D
 ;)
 ;)
 :p
 :p
 :((
 :((
 :)
 :)
 :(
 :(
 :X
 :X
 =((
 =((
 :-o
 :-o
 :-/
 :-/
 :-*
 :-*
 :|
 :|
 8-}
 8-}
 :)]
 :)]
 ~x(
 ~x(
 :-t
 :-t
 b-(
 b-(
 :-L
 :-L
 x(
 x(
 =))
 =))





