Advertisement
Guest User

Lab 2 = Insert and Merge

a guest
Feb 17th, 2020
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.63 KB | None | 0 0
  1. #include<iostream>
  2. using namespace std;
  3.  
  4. class student
  5. {
  6. public:
  7.     int id;
  8.     string name;
  9.     int credit;
  10.     float cgpa;
  11.  
  12.     void input()
  13.     {
  14.         cin>>id>>name>>credit>>cgpa;
  15.     }
  16.     void output()
  17.     {
  18.         cout<<"\t"<<id;
  19.         cout<<"\t"<<name;
  20.         cout<<"\t"<<credit;
  21.         cout<<"\t"<<cgpa;
  22.         cout<<endl;
  23.     }
  24. };
  25.  
  26. void insertion(student s[],int n)
  27. {
  28.     int i,j;
  29.     student key;
  30.     for(j=2; j<=n; j++)
  31.     {
  32.         key=s[j];
  33.         i=j-1;
  34.         while(i>0&&s[i].credit<=key.credit)
  35.         {
  36.             if(s[i].credit==key.credit)
  37.             {
  38.                 if(s[i].cgpa<key.cgpa)
  39.                 {
  40.                     s[i+1]=s[i];
  41.                 }
  42.             }
  43.             s[i+1]=s[i];
  44.             i=i-1;
  45.         }
  46.         s[i+1]=key;
  47.     }
  48. }
  49.  
  50.  
  51. void merge(student a[],int p,int q,int r)
  52. {
  53.     int n1,n2,i,j,k;
  54.     student l[200];
  55.     student m[200];
  56.     n1=q-p+1;
  57.     n2=r-q;
  58.     for(i=1; i<=n1; i++)
  59.     {
  60.         l[i]=a[p+i-1];
  61.     }
  62.     for(j=1; j<=n2; j++)
  63.     {
  64.         m[j]=a[q+j];
  65.     }
  66.     l[n1+1].credit=-234154;
  67.     m[n2+1].credit=-123555;
  68.     i=1;
  69.     j=1;
  70.     for(k=p; k<=r; k++)
  71.     {
  72.         if(l[i].credit==m[j].credit)
  73.         {
  74.             if(l[i].cgpa>m[j].cgpa)
  75.             {
  76.  
  77.                 a[k]=l[i];
  78.                 i++;
  79.             }
  80.             else
  81.             {
  82.                 a[k]=m[j];
  83.                 j++;
  84.             }
  85.         }
  86.  
  87.         else if(l[i].credit>m[j].credit)
  88.         {
  89.  
  90.             a[k]=l[i];
  91.             i++;
  92.         }
  93.         else
  94.         {
  95.             a[k]=m[j];
  96.             j++;
  97.         }
  98.     }
  99.  
  100. }
  101. void mergesort(student s[],int p,int r)
  102. {
  103.     int q;
  104.     if(p<r)
  105.     {
  106.         q=(p+r)/2;
  107.         mergesort(s,p,q);
  108.         mergesort(s,q+1,r);
  109.         merge(s,p,q,r);
  110.  
  111.     }
  112. }
  113.  
  114. int main()
  115. {
  116.     ofstream myfile ("input.text");
  117.     student s[100];
  118.     int n,i,choice;
  119.  
  120.     cout<<"Enter the number of student:";
  121.     cin>>n;
  122.     cout<<"\nStudent information\n";
  123.     for(i=1; i<=n; i++)
  124.     {
  125.         s[i].input();
  126.     }
  127.     cout<<"\n1. Insertion sort";
  128.     cout<<"\n2. Merge sort";
  129.     cout<<"\n3. Exit";
  130.     while(1)
  131.     {
  132.         cout<<"\nEnter your choice:";
  133.         cin>>choice;
  134.  
  135.         if(choice==1)
  136.         {
  137.             insertion(s,n);
  138.         }
  139.         else if(choice==2)
  140.         {
  141.             mergesort(s,1,n);
  142.         }
  143.         else if(choice==3)
  144.         {
  145.             break;
  146.         }
  147.         cout<<"\nStudent Sorted Information\n";
  148.         for(i=1; i<=n; i++)
  149.         {
  150.             s[i].output();
  151.         }
  152.     }
  153.     return 0;
  154. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement