Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- using namespace std;
- class student
- {
- public:
- int id;
- string name;
- int credit;
- float cgpa;
- void input()
- {
- cin>>id>>name>>credit>>cgpa;
- }
- void output()
- {
- cout<<"\t"<<id;
- cout<<"\t"<<name;
- cout<<"\t"<<credit;
- cout<<"\t"<<cgpa;
- cout<<endl;
- }
- };
- void insertion(student s[],int n)
- {
- int i,j;
- student key;
- for(j=2; j<=n; j++)
- {
- key=s[j];
- i=j-1;
- while(i>0&&s[i].credit<=key.credit)
- {
- if(s[i].credit==key.credit)
- {
- if(s[i].cgpa<key.cgpa)
- {
- s[i+1]=s[i];
- }
- }
- s[i+1]=s[i];
- i=i-1;
- }
- s[i+1]=key;
- }
- }
- void merge(student a[],int p,int q,int r)
- {
- int n1,n2,i,j,k;
- student l[200];
- student m[200];
- n1=q-p+1;
- n2=r-q;
- for(i=1; i<=n1; i++)
- {
- l[i]=a[p+i-1];
- }
- for(j=1; j<=n2; j++)
- {
- m[j]=a[q+j];
- }
- l[n1+1].credit=-234154;
- m[n2+1].credit=-123555;
- i=1;
- j=1;
- for(k=p; k<=r; k++)
- {
- if(l[i].credit==m[j].credit)
- {
- if(l[i].cgpa>m[j].cgpa)
- {
- a[k]=l[i];
- i++;
- }
- else
- {
- a[k]=m[j];
- j++;
- }
- }
- else if(l[i].credit>m[j].credit)
- {
- a[k]=l[i];
- i++;
- }
- else
- {
- a[k]=m[j];
- j++;
- }
- }
- }
- void mergesort(student s[],int p,int r)
- {
- int q;
- if(p<r)
- {
- q=(p+r)/2;
- mergesort(s,p,q);
- mergesort(s,q+1,r);
- merge(s,p,q,r);
- }
- }
- int main()
- {
- ofstream myfile ("input.text");
- student s[100];
- int n,i,choice;
- cout<<"Enter the number of student:";
- cin>>n;
- cout<<"\nStudent information\n";
- for(i=1; i<=n; i++)
- {
- s[i].input();
- }
- cout<<"\n1. Insertion sort";
- cout<<"\n2. Merge sort";
- cout<<"\n3. Exit";
- while(1)
- {
- cout<<"\nEnter your choice:";
- cin>>choice;
- if(choice==1)
- {
- insertion(s,n);
- }
- else if(choice==2)
- {
- mergesort(s,1,n);
- }
- else if(choice==3)
- {
- break;
- }
- cout<<"\nStudent Sorted Information\n";
- for(i=1; i<=n; i++)
- {
- s[i].output();
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement