Advertisement
Guest User

Lab 6= Radix

a guest
Feb 17th, 2020
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.50 KB | None | 0 0
  1. #include<iostream>
  2. using namespace std;
  3. class student
  4. {
  5. public:
  6.     int id;
  7.     string name;
  8.     int credit;
  9.     float cgpa;
  10.     void input()
  11.     {
  12.         cin>>id>>name>>credit>>cgpa;
  13.     }
  14.     void output()
  15.     {
  16.         cout<<"\t"<<id;
  17.         cout<<"\t"<<name;
  18.         cout<<"\t"<<credit;
  19.         cout<<"\t"<<cgpa;
  20.         cout<<endl;
  21.     }
  22. };
  23. void CountingSort(student u[200],int n,int loc)
  24. {
  25.     int i,j;
  26.     int w[10]= {0};
  27.     student v[200];
  28.  
  29.     for(i=0; i<n; i++)
  30.     {
  31.         w[(u[i].id/loc)%10] =w[(u[i].id/loc)%10]+1;
  32.     }
  33.     for(i=1; i<=10; i++)
  34.     {
  35.         w[i]=w[i]+w[i-1];
  36.     }
  37.     for(j=n-1; j>=0; j--)
  38.     {
  39.         v[w[(u[j].id/loc)%10]-1]=u[j];
  40.         w[(u[j].id/loc)%10]--;
  41.     }
  42.     for(j=0; j<n; j++)
  43.     {
  44.         u[j]=v[j];
  45.     }
  46. }
  47. void RadixSort(student s[200],int n)
  48. {
  49.     int i,j,max=0,loc;
  50.     for(i=0; i<n; i++)
  51.     {
  52.         if(s[i].id>max)
  53.         {
  54.             max=s[i].id;
  55.         }
  56.     }
  57.     for(loc=1; max/loc>0; loc=loc*10)
  58.     {
  59.         CountingSort(s,n,loc);
  60.     }
  61. }
  62.  
  63. int main()
  64. {
  65.     student s[200];
  66.     int i,j,n;
  67.     cout<<"\nEnter the number of students:";
  68.     cin>>n;
  69.     cout<<"\nEnter Student information: \n";
  70.     for(i=0; i<n; i++)
  71.     {
  72.         s[i].input();
  73.     }
  74.     cout<<endl;
  75.  
  76.     RadixSort(s,n);
  77.  
  78.     cout<<"\tID";
  79.     cout<<"\tName";
  80.     cout<<"\tCredit";
  81.     cout<<"\tCGPA";
  82.     cout<<endl;
  83.  
  84.     for(i=0; i<n; i++)
  85.     {
  86.         s[i].output();
  87.     }
  88.     cout<<endl;
  89.     return 0;
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement