-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSTUDENTSORTbubble.cpp
More file actions
54 lines (43 loc) · 981 Bytes
/
STUDENTSORTbubble.cpp
File metadata and controls
54 lines (43 loc) · 981 Bytes
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
#include<stdio.h>
#include<string.h>
FILE *fp;
struct student{
char nim[11];
char nama[21];
};
void tukar(struct student *a, struct student *b) {
struct student temp = *a;
*a = *b;
*b = temp;
}
void bubbleSort(struct student stdn[], int n){
for (int i = 0; i < n - 1; i++) {
int isSwapped = 0;
for (int j = 0; j < n - i - 1; j++) {
if (strcmp(stdn[j].nim, stdn[j + 1].nim) > 0) {
tukar(&stdn[j], &stdn[j + 1]);
isSwapped = 1;
}
}
if(isSwapped == 0){
return;
}
}
}
int main(){
struct student stdn[1001];
int n;
fp = fopen("testdata.in", "r");
fscanf(fp, "%d\n", &n);
int i = 0;
while(!feof(fp)){
fscanf(fp, "%s %[^\n]\n", &stdn[i].nim, &stdn[i].nama);
i++;
}
bubbleSort(stdn, n);
for(int j = 0; j < n; j++){
printf("%s %s\n", stdn[j].nim, stdn[j].nama);
}
fclose(fp);
return 0;
}