-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStudent_Insertion_Shell_Quick.cpp
153 lines (133 loc) · 3.38 KB
/
Student_Insertion_Shell_Quick.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
#include<iostream>
using namespace std;
const int MAX = 20;
class Student {
float perc[MAX];
int n;
public:
void accept();
void display();
void insertionSort();
void shellSort();
void displayTop5();
void quickSort(int start, int end);
int getn() {
return n;
}
};
void Student::accept() {
cout << "\nEnter number of students: ";
cin >> n;
cout << "\nEnter percentages of " << n << " students: ";
for (int i = 0; i < n; i++) {
cin >> perc[i];
}
}
void Student::display() {
cout << "\nStudent list: ";
for (int i = 0; i < n; i++) {
cout << perc[i] << " ";
}
}
void Student::displayTop5() {
int c;
for (int i = n - 1, c = 0; i >= 0 && c < 5; i--, c++) {
cout << c + 1 << " )" << perc[i] << "\n";
}
}
void Student::insertionSort() {
int i, j;
for (int i = 1; i < n; i++) {
float temp = perc[i];
for (j = i - 1; j >= 0 && perc[j] > temp; j--) {
perc[j + 1] = perc[j];
}
perc[j + 1] = temp;
}
cout << "\nSorted list is: ";
display();
}
void Student::shellSort() {
int i, j, k;
float temp;
for (int i = n / 2; i > 0; i = i / 2) {
for (j = i; j < n; j++) {
temp = perc[j];
for (k = j - 1; k >= 0 && perc[k] > temp; k--) {
perc[k + 1] = perc[k];
}
perc[k + 1] = temp;
}
}
cout << "\nSorted list is: ";
display();
}
void Student::quickSort(int start, int end) {
int pivot, i, j;
float temp;
if (start < end) {
pivot = start;
i = start + 1;
j = end;
while (i < j) {
while (perc[i] <= perc[pivot] && i < j)
i++;
while (perc[j] >= perc[pivot] && i <= j)
j--;
if (i <= j) {
temp = perc[i];
perc[i] = perc[j];
perc[j] = temp;
}
}
temp = perc[pivot];
perc[pivot] = perc[j];
perc[j] = temp;
quickSort(start, j - 1);
quickSort(j + 1, end);
}
}
int main() {
Student s;
int choice, num;
do {
cout << "\n==========================MENU==========================";
cout << "\n1.Insertion sort";
cout << "\n2.Shell sort";
cout << "\n3.Display top 5";
cout << "\n4.Quick sort";
cout << "\n0.Exit";
cout << "\nEnter the choice: ";
cin >> choice;
switch (choice) {
case 1:
s.accept();
cout << "\nBefore sorting: ";
s.display();
cout << "\nAfter sorting: ";
s.insertionSort();
break;
case 2:
s.accept();
cout << "\nBefore sorting: ";
s.display();
cout << "\nAfter sorting: ";
s.shellSort();
break;
case 3:
cout << "\n5 Toppers are: ";
s.displayTop5();
break;
case 4:
s.accept();
cout << "\nBefore sorting: ";
s.display();
cout << "\nAfter sorting: ";
num = s.getn();
s.quickSort(0, num - 1);
s.display();
break;
}
} while (choice != 0);
return 0;
}