-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNISN-NILAI.cpp
More file actions
318 lines (308 loc) · 10 KB
/
NISN-NILAI.cpp
File metadata and controls
318 lines (308 loc) · 10 KB
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
#include <iostream>
#include <iomanip>
using namespace std;
int input2,input1;
int a=1,b,c,i,j,k,temp=0;
bool found=false;
struct student{
long double studentNumber;
string studentName;
int studentScore;
};
student data_main[100],data_swap[100];
void insertSortNISN(int input2,student data_main[]){
for(j=1;j<input2;j++){
i=j-1;
temp=data_main[j].studentNumber;
data_swap[0]=data_main[j];
while(data_main[i].studentNumber>temp&&i>=0){
data_main[i+1]=data_main[i];
i--;
}
data_main[i+1]=data_swap[0];
}
}
void insertSortNilai(int input2,student data_main[]){
for(j=1;j<input2;j++){
i=j-1;
temp=data_main[j].studentScore;
data_swap[0]=data_main[j];
while(data_main[i].studentScore>temp&&i>=0){
data_main[i+1]=data_main[i];
i--;
}
data_main[i+1]=data_swap[0];
}
}
void selectSortNISN(int input2,student data_main[]){
for(i=0;i<input2;i++){
k=i;
for(j=i+1;j<input2;j++){
if(data_main[j].studentNumber<data_main[k].studentNumber){
k=j;
}
}
data_swap[0]=data_main[i];
data_main[i]=data_main[k];
data_main[k]=data_swap[0];
}
}
void selectSortNilai(int input2,student data_main[]){
for(i=0;i<input2;i++){
k=i;
for(j=i+1;j<input2;j++){
if(data_main[j].studentScore<data_main[k].studentScore){
k=j;
}
}
data_swap[0]=data_main[i];
data_main[i]=data_main[k];
data_main[k]=data_swap[0];
}
}
void bubbleSortNISN(int input2,student data_main[]){
for(i=0;i<input2;i++){
for(j=0;j<input2-i-1;j++){
if(data_main[j].studentNumber>data_main[j+1].studentNumber){
data_swap[0]=data_main[j];
data_main[j]=data_main[j+1];
data_main[j+1]=data_swap[0];
}
}
}
}
void bubbleSortNilai(int input2,student data_main[]){
for(i=0;i<input2;i++){
for(j=0;j<input2-i-1;j++){
if(data_main[j].studentScore>data_main[j+1].studentScore){
data_swap[0]=data_main[j];
data_main[j]=data_main[j+1];
data_main[j+1]=data_swap[0];
}
}
}
}
void mergeSwapNISN(int low,int mid,int high,student data_main[],student data_swap[]){
int l1,l2,i;
for(l1=low,l2=mid+1,i=low;l1<=mid&&l2<=high;i++){
if(data_main[l1].studentNumber<=data_main[l2].studentNumber){
data_swap[i]=data_main[l1++];}
else{
data_swap[i]=data_main[l2++];}
}
while(l1<=mid){
data_swap[i++]=data_main[l1++];}
while(l2<=high){
data_swap[i++]=data_main[l2++];}
for(i=low;i<=high;i++){
data_main[i]=data_swap[i];}
}
void mergeSortNISN(int low,int high,student data_main[],student data_swap[]){
int mid;
if(low<high){
mid=(low+high)/2;
mergeSortNISN(low,mid,data_main,data_swap);
mergeSortNISN(mid+1,high,data_main,data_swap);
mergeSwapNISN(low,mid,high,data_main,data_swap);
}
else{
return;
}
}
void mergeSwapNilai(int low,int mid,int high,student data_main[],student data_swap[]){
int l1,l2,i;
for(l1=low,l2=mid+1,i=low;l1<=mid&&l2<=high;i++){
if(data_main[l1].studentScore<=data_main[l2].studentScore){
data_swap[i]=data_main[l1++];}
else{
data_swap[i]=data_main[l2++];}
}
while(l1<=mid){
data_swap[i++]=data_main[l1++];}
while(l2<=high){
data_swap[i++]=data_main[l2++];}
for(i=low;i<=high;i++){
data_main[i]=data_swap[i];}
}
void mergeSortNilai(int low,int high,student data_main[],student data_swap[]){
int mid;
if(low<high){
mid=(low+high)/2;
mergeSortNilai(low,mid,data_main,data_swap);
mergeSortNilai(mid+1,high,data_main,data_swap);
mergeSwapNilai(low,mid,high,data_main,data_swap);
}
else{
return;
}
}
int partitionNISN(student data_main[],int low,int high){
int pivot=data_main[high].studentNumber;
int i=(low-1);
for(int j=low;j<=high-1;j++){
if(data_main[j].studentNumber<=pivot){
i++;
data_swap[0]=data_main[i];
data_main[i]=data_main[j];
data_main[j]=data_swap[0];
}
}
data_swap[0]=data_main[i+1];
data_main[i+1]=data_main[high];
data_main[high]=data_swap[0];
return (i+1);
}
void quickSortNISN(student data_main[],int low,int high){
if(low<high){
int part=partitionNISN(data_main,low,high);
quickSortNISN(data_main,low,part-1);
quickSortNISN(data_main,part+1,high);
}
}
int partitionNilai(student data_main[],int low,int high){
int pivot=data_main[high].studentScore;
int i=(low-1);
for(int j=low;j<=high-1;j++){
if(data_main[j].studentScore<=pivot){
i++;
data_swap[0]=data_main[i];
data_main[i]=data_main[j];
data_main[j]=data_swap[0];
}
}
data_swap[0]=data_main[i+1];
data_main[i+1]=data_main[high];
data_main[high]=data_swap[0];
return (i+1);
}
void quickSortNilai(student data_main[],int low,int high){
if(low<high){
int part=partitionNilai(data_main,low,high);
quickSortNilai(data_main,low,part-1);
quickSortNilai(data_main,part+1,high);
}
}
void printtab(int input2,student data_main[]){
cout << "NISN" << setw(10) << "|" << "Nama" << setw(10) << "|" << "Nilai" << endl;
cout << "---------------------------------------------\n";
for(a=0;a<input2;a++){
cout << data_main[a].studentNumber << setw(11) << "|" << data_main[a].studentName << setw(13) << "|" << data_main[a].studentScore << endl;
}
cout << endl;
}
int main(){
int p=0,q=sizeof(data_main)/sizeof(data_main[0]),r;
long double query;
cout << "Data mahasiswa yang dimasukkan= ";
cin >> input2;
student data_main[input2];
cout << "===============Data Mahasiswa================\n";
for(a=0;a<input2;a++){
cout << "NISN = ";
cin >> data_main[a].studentNumber;
cout << "Nama = ";
cin >> data_main[a].studentName;
cout << "Nilai= ";
cin >> data_main[a].studentScore;
cout << "---------------------------------------------\n";
}
top:
cout << "===================Menu======================\n";
cout << "1. Insert Sort\n";
cout << "2. Select Sort\n";
cout << "3. Bubble Sort\n";
cout << "4. Merge Sort\n";
cout << "5. Quick Sort\n";
cout << "6. Binary Search\n";
cout << "7. Sequential Search\n";
cout << "8. Exit\n";
cout << "Menu = ";
cin >> input1;
cout << endl;
switch(input1){
case 1:
insertSortNISN(input2,data_main);
cout << "Urutan NISN\n";
printtab(input2,data_main);
insertSortNilai(input2,data_main);
cout << "Urutan Nilai\n";
printtab(input2,data_main);
goto top;
case 2:
selectSortNISN(input2,data_main);
cout << "Urutan NISN\n";
printtab(input2,data_main);
selectSortNilai(input2,data_main);
cout << "Urutan Nilai\n";
printtab(input2,data_main);
goto top;
case 3:
bubbleSortNISN(input2,data_main);
cout << "Urutan NISN\n";
printtab(input2,data_main);
bubbleSortNilai(input2,data_main);
cout << "Urutan Nilai\n";
printtab(input2,data_main);
goto top;
case 4:
mergeSortNISN(0,input2-1,data_main,data_swap);
cout << "Urutan NISN\n";
printtab(input2,data_main);
mergeSortNilai(0,input2-1,data_main,data_swap);
cout << "Urutan Nilai\n";
printtab(input2,data_main);
goto top;
case 5:
quickSortNISN(data_main,0,input2-1);
cout << "Urutan NISN\n";
printtab(input2,data_main);
quickSortNilai(data_main,0,input2-1);
cout << "Urutan Nilai\n";
printtab(input2,data_main);
goto top;
case 6:
cout << "Query= ";
cin >> query;
quickSortNISN(data_main,0,input2-1);
while(!found&&p<=q){
r=(p+q)/2;
if(data_main[r].studentNumber<query){
p=r+1;
}
else if(query==data_main[r].studentNumber){
found=true;
}
else{
q=r-1;
}
}
if(found==false){
cout << "Query not found\n";
}
else{
cout << "NISN" << setw(10) << "|" << "Nama" << setw(10) << "|" << "Nilai" << endl;
cout << data_main[i].studentNumber << setw(11) << "|" << data_main[i].studentName << setw(13) << "|" << data_main[i].studentScore << endl;
}
cout << endl;
goto top;
case 7:
cout << "Query= ";
cin >> query;
for(int i=0;i<input2;i++){
if(data_main[i].studentNumber==query){
cout << "NISN" << setw(10) << "|" << "Nama" << setw(10) << "|" << "Nilai" << endl;
cout << data_main[i].studentNumber << setw(11) << "|" << data_main[i].studentName << setw(13) << "|" << data_main[i].studentScore << endl;
found=true;
}
}
if(found==false){
cout << "Query not found\n";
}
cout << endl;
goto top;
default:
break;
}
system("pause");
return 0;
}