Radix Sort

 #include<iostream.h>  
 #include<conio.h>  
 class radix  
 {  
      private:  
           int i, j, k, max, re, pass, dc, div, bucket[10][10];  
           int bc[10], a[15], n;  
      public:  
           void get();  
           void sort();  
           void maxi();  
           void disp();  
 };  
 void radix :: get()  
 {  
      cout<<endl<<"Enter the array size : ";  
      cin>>n;  
      cout<<endl<<"Enter the elements : ";  
      for(i=0; i<n; i++)  
           cin>>a[i];  
 }  
 void radix :: maxi()  
 {  
      max=a[0];  
      for(i=1; i<n; i++)  
      {  
           if(max < a[i])  
                max=a[i];  
      }  
      dc=0;  
      div=1;  
      while(max>0)  
      {  
           dc++;  
           max = max/10;  
      }  
 }  
 void radix :: sort()  
 {  
      for(pass=1; pass<=dc; pass++)  
      {  
           for(i=0; i<10; i++)  
                bc[i]=0;  
           for(i=0; i<n; i++)  
           {  
                re=(a[i]/div)%10;  
                bucket[re][bc[re]]=a[i];  
                bc[re]++;  
           }  
           for(k=0; k<10; k++)  
           {  
                for(j=0; j<bc[k]; j++)  
                     a[i++]=bucket[k][j];  
           }  
           div *=10;  
           cout<<endl<<"PASS "<< pass <<" : ";  
           for(k=0; k<n; k++)  
                cout<<'\t'<<a[k];  
      }  
 }  
 void radix :: disp()  
 {  
      cout<<endl<<endl<<"SORTED ARRAY"<<endl<<endl;  
      for(i=0; i<n; i++)  
           cout<<'\t'<<a[i];  
 }  
 void main()  
 {  
      clrscr();  
      radix r;  
      r.get();  
      r.maxi();  
      r.sort();  
      r.disp();  
      getch();  
 }  

Share This!


No comments:

Post a Comment

Code Of The day - Suggest An Output For The Snippet

  #include<stdio.h>   
  int main()   
  {   
   float a=3.15529;   
   printf("%2.1f\n", a);   
   return 0;   
  }   
· A Code Archive - code1archive