-
Notifications
You must be signed in to change notification settings - Fork 0
/
selection_sort.c
47 lines (42 loc) · 932 Bytes
/
selection_sort.c
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
#include <stdio.h>
// selection sort
int index_of_min(int *table, int idx, int len);
void tri_selection(int *table, int len);
void display(int *arr, int arrlen);
int main(int argc, char const *argv[])
{
int numbers[5] = {8, 2, 4, 1, 3};
printf("\nles nombres avant tries...\n");
display(numbers, 5);
printf("\nles nombres apres tries...\n");
tri_selection(numbers, 5);
display(numbers, 5);
printf("\n");
return 0;
}
int index_of_min(int *table, int idx, int len) {
int i, index, min;
min = table[idx];
index = idx;
for(i = idx+1; i < len; i++) {
if(table[i] < min) {
min = table[i];
index = i;
}
}
return index;
}
void tri_selection(int *table, int len) {
int i, temp, k;
for(i = 0; i < len; i++) {
k = index_of_min(table, i, len);
temp = table[i];
table[i] = table[k];
table[k] = temp;
}
}
void display(int *arr, int arrlen) {
for(int i = 0; i < arrlen; i++) {
printf("%i ", arr[i]);
}
}