-
Notifications
You must be signed in to change notification settings - Fork 0
/
sorting.cpp
187 lines (169 loc) · 3.34 KB
/
sorting.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
/*
* sorting.cpp
*
* Created on: 10-May-2021
* Author: sakshi
*/
#include<iostream>
#include<string>
using namespace std;
class student{
string name;
double cgpa;
long cno;
int rollno;
friend class sort;
public:
void acceptStudent(); //accept details for one student
void displayStudent(); //display details for one student;
};
void student :: acceptStudent(){
cout<<"-----------------------------------------------------"<<endl;
cout<<endl;
cout<<"Enter Name of Student : "<<endl;
cin>>name;
cout<<"Enter Roll No. of Student : "<<endl;
cin>>rollno;
cout<<"Enter C. No. of Student : "<<endl;
cin>>cno;
cout<<"Enter CGPA of Student : "<<endl;
cin>>cgpa;
cout<<endl;
cout<<"-----------------------------------------------------"<<endl;
}
void student :: displayStudent(){
cout<<"*****************************************************"<<endl;
cout<<endl;
cout<<" NAME : "<<name<<endl;
cout<<" ROLL NO. : "<<rollno<<endl;
cout<<" C. NO. :"<<cno<<endl;
cout<<" CGPA : "<<cgpa<<endl;
cout<<endl;
cout<<"*****************************************************"<<endl;
}
class sort{
student s[100];
student temp;
int n; //total number of student
public:
void acceptNoStudent();
void displayNoStudent();
void bubblesort();
void selectionsort();
void insertionsort();
void quicksort();
void mergesort();
};
void sort :: acceptNoStudent(){
cout<<"Enter the Total number of in Class : "<<endl;
cin>>n;
for(int i=1 ; i<=n ; i++){
cout<<"STUDENT : "<<endl;
s[i].acceptStudent();
}
}
void sort :: displayNoStudent(){
for(int i = 0 ; i<=n ;i++) {
s[i].displayStudent();
}
}
void sort :: bubblesort(){
for(int i = 0 ; i<n-1 ; i++){
for(int j = i+1 ; j<n-1 ; j++){
if(s[i].cgpa > s[j].cgpa){
temp = s[i];
s[i] = s[j];
s[j] = temp;
}
}
}
}
void sort :: selectionsort(){
int i,j;
for( i = 0 ; i<n-1; i++){
int min = i;
for(j = i+1 ; j<n ; j++){
if(s[j].cgpa < s[min].cgpa){
min = j;
}
}
if(min!=i){
temp = s[i];
s[i] = s[min];
s[min] = temp;
}
}
}
void sort :: insertionsort(){
int i , j;
for(i=1; i<n ; i++){
temp = s[i];
double l = s[i].cgpa;
j = i-1;
while (j >= 0 && s[j].cgpa>l){
s[j+1] = s[j];
j--;
}
s[j+1] = temp;
}
}
void sort :: quicksort(){
int pivot;
pivot=s[0].cgpa;
int i=1;
int j=n-1;
while(i<j)
{
while(s[i].cgpa<=pivot)
{
i++;
}
while(s[j].cgpa>=pivot)
{
j--;
}
if(i<=j)
{
temp=s[i];
s[i]=s[j];
s[j]=temp;
}
}
temp=s[0];
s[0]=s[j];
s[j]=temp;
}
int main(){
sort k;
k.acceptNoStudent();
int ch;
do{
cout<<"\tSTUDENT CGPA SORTING ALGORITHM\t"<<endl;
cout<<"\n1.DISPLAY STUDENT DETAILS \n2.BUBBLE SORT \n3.SELECTION SORT \n4.INSERTION SORT \n4.QUICK SORT"<<endl;
cout<<"Enter your choice"<<endl;
cin>>ch;
switch(ch){
case 1:
k.displayNoStudent();
break;
case 2:
k.bubblesort();
break;
case 3 :
k.selectionsort();
break;
case 4 :
k.insertionsort();
break;
case 5 :
k.quicksort();
break;
case 6 :
cout<<"\tTHANKYOU\t"<<endl;
break;
default :
cout<<"INVALID INPUT"<<endl;
break;
}
}while(ch!=6);
}