Monday, October 31, 2016

Radix Sort :) :)

Tags

Radix Sort is the method to sort array elements according to the Numbers at unit place,tens place and then we get the sorted list.

Following Program Illustrates Radix Sort:

#include<iostream>
using namespace std;
main()
{   int b[10][10],a[100],n,num,pass,g=1;

cout<<"Enter The No Of Elements ";
cin>>n;

cout<<"Enter The Maximum No Of Digits ";
cin>>pass;

cout<<"Enter The Elements\n";
for(int i=0;i<n;i++)
cin>>a[i];

  for(int h=0;h<pass;h++)    //for number of passes
{for(int i=0;i<10;i++)
for(int j=0;j<10;j++)
b[i][j]=-99;               //intializing all elements to  default value

int j=0,k=0;

for(int i=0;i<n;i++)
{
num=((a[i]/g)%10);    //finding remainder
b[j++][num]=a[i];     //inserting elements at proper position
   }
    g=g*10;                      //updating divisor  
   
   for(int i=0;i<10;i++)
for(int j=0;j<10;j++)
   {
if(b[j][i]!=-99)
       a[k++]=b[j][i];     // updating array    
       }  
cout<<"Pass "<<h+1<<": ";
for(int i=0;i<n;i++)
{  
cout<<a[i]<<"\t";       //printing the results of particular pass
}
cout<<"\n";
}
cout<<"Soterd Array\n";
for(int i=0;i<n;i++)
{
cout<<a[i]<<"\t";     //printing the result
}
}
Output: