-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathselection_sort.c
More file actions
56 lines (45 loc) · 1.05 KB
/
Copy pathselection_sort.c
File metadata and controls
56 lines (45 loc) · 1.05 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
#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 selectionsort(int list[], int n){
int i, j , posisi;
for (i = 0; i < (n-1); i++){
posisi = i;
for (j = i; j < n; j++){
if (list[j] < list[posisi]){
posisi = j;
}
}
if (posisi != i){
swap(&list[posisi], &list[i]);
}
}
}
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
selectionsort(list, MAX_ELEMENT);
// nilai setelah sorting
printf("Nilai setelah selection sort : \n");
listArray(list, MAX_ELEMENT);
return 0;
}