-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbubble_sort.c
More file actions
50 lines (40 loc) · 967 Bytes
/
Copy pathbubble_sort.c
File metadata and controls
50 lines (40 loc) · 967 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
#include <stdio.h>
#include <stdlib.h>
const int MAX_ELEMENT = 10;
void swap(int *x, int *y){
int temp;
temp = *x;
*x = *y;
*y = temp;
}
void bubblesort(int list[], int n){
for (int i = 0; i < (n-1); i++){
for (int j = 0; j < (n-(i+1)); j++){
if (list[j] > list[j+1]){
swap(&list[j], &list[j+1]);
}
}
}
}
void listArray(int list[], int n){
for (int i = 0; i < n; i++){
printf("%d, ", list[i]);
}
}
int main(){
int list[MAX_ELEMENT];
// generate randome number
for (int i = 0; i < MAX_ELEMENT; i++){
list[i] = rand();
}
// nilai asli list sebelum sorting
printf("Nilai asli : \n");
listArray(list, MAX_ELEMENT);
printf("\n");
// sorting dengan bubble sort
bubblesort(list, MAX_ELEMENT);
// nilai setelah sorting
printf("Nilai setelah bubble sort : \n");
listArray(list, MAX_ELEMENT);
return 0;
}