Advertisement
Guest User

Lab 3 = Quick

a guest
Feb 17th, 2020
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.99 KB | None | 0 0
  1. #include<iostream>
  2. #include <cstdlib>
  3.  
  4. using namespace std;
  5.  
  6. class student
  7. {
  8. public:
  9.     int id;
  10.     string name;
  11.     float cgpa;
  12.     int phn;
  13.  
  14.     void input()
  15.     {
  16.         cin>>id>>name>>cgpa>>phn;
  17.     }
  18.     void output()
  19.     {
  20.         cout<<"\t"<<id;
  21.         cout<<"\t"<<name;
  22.         cout<<"\t"<<cgpa;
  23.         cout<<"\t"<<phn;
  24.         cout<<endl;
  25.     }
  26. };
  27. int partition1(student a[],int p,int r)
  28. {
  29.     student x;
  30.     student temp;
  31.     int i,j;
  32.     x=a[p];
  33.     i=r+1;
  34.     for(j=r; j>=p+1; j--)
  35.     {
  36.         if(a[j].cgpa>=x.cgpa)
  37.         {
  38.             i=i-1;
  39.             temp=a[j];
  40.             a[j]=a[i];
  41.             a[i]=temp;
  42.         }
  43.     }
  44.  
  45.     temp=a[i-1];
  46.     a[i-1]=a[p];
  47.     a[p]=temp;
  48.  
  49.     return i-1;
  50. }
  51. void quicksort1(student a[],int p,int r)
  52. {
  53.     int q;
  54.     if(p<r)
  55.     {
  56.         q=partition1(a,p,r);
  57.         quicksort1(a,p,q-1);
  58.         quicksort1(a,q+1,r);
  59.  
  60.     }
  61. }
  62.  
  63. int partition2(student a[],int p,int r)
  64. {
  65.     student x;
  66.     student temp;
  67.     int i,j;
  68.     x=a[r];
  69.     i=p-1;
  70.     for(j=p; j<=r-1; j++)
  71.     {
  72.         if(a[j].cgpa<=x.cgpa)
  73.         {
  74.             i=i+1;
  75.             temp=a[j];
  76.             a[j]=a[i];
  77.             a[i]=temp;
  78.         }
  79.     }
  80.  
  81.     temp=a[i+1];
  82.     a[i+1]=a[r];
  83.     a[r]=temp;
  84.  
  85.     return i+1;
  86. }
  87. void quicksort2(student a[],int p,int r)
  88. {
  89.     int q;
  90.     if(p<r)
  91.     {
  92.         q=partition2(a,p,r);
  93.         quicksort2(a,p,q-1);
  94.         quicksort2(a,q+1,r);
  95.  
  96.     }
  97. }
  98.  
  99. int partition3(student a[],int p,int r)
  100. {
  101.     student x;
  102.     student temp;
  103.     int random;
  104.     random = p + rand() % (r - p);
  105.     swap(a[random],a[r]);
  106.     int i,j;
  107.     x=a[r];
  108.     i=p-1;
  109.     for(j=p; j<=r-1; j++)
  110.     {
  111.         if(a[j].cgpa<=x.cgpa)
  112.         {
  113.             i=i+1;
  114.             temp=a[j];
  115.             a[j]=a[i];
  116.             a[i]=temp;
  117.         }
  118.  
  119.     }
  120.     temp=a[r];
  121.     a[r]=a[i+1];
  122.     a[i+1]=temp;
  123.     return i+1;
  124.  
  125. }
  126. void quicksort3(student a[],int p,int r)
  127. {
  128.     int q;
  129.     if(p<r)
  130.     {
  131.         q=partition3(a,p,r);
  132.         quicksort3(a,p,q-1);
  133.         quicksort3(a,q+1,r);
  134.     }
  135. }
  136.  
  137. int main()
  138. {
  139.     student s[100];
  140.     int n,i,choice;
  141.  
  142.     cout<<"Enter the number of student:";
  143.     cin>>n;
  144.     cout<<"\nStudent information\n";
  145.     for(i=1; i<=n; i++)
  146.     {
  147.         s[i].input();
  148.     }
  149.  
  150.     cout<<"\n1. First Pivot QuickSort";
  151.     cout<<"\n2. Last Pivot QuickSort";
  152.     cout<<"\n3. Randomized QuickSort";
  153.     cout<<"\n4. Exit";
  154.  
  155.  
  156.     while(1)
  157.     {
  158.         cout<<"\nEnter your choice:";
  159.         cin>>choice;
  160.  
  161.         if(choice==1)
  162.         {
  163.             quicksort1(s,1,n);
  164.         }
  165.         else if(choice==2)
  166.         {
  167.             quicksort2(s,1,n);
  168.         }
  169.         else if(choice==3)
  170.         {
  171.             quicksort3(s,1,n);
  172.         }
  173.         else if(choice==4)
  174.         {
  175.             break;
  176.         }
  177.         cout<<"\nStudent Sorted Information\n";
  178.         for(i=1; i<=n; i++)
  179.         {
  180.             s[i].output();
  181.         }
  182.     }
  183.     return 0;
  184. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement