-
Notifications
You must be signed in to change notification settings - Fork 0
/
search.c
60 lines (40 loc) · 957 Bytes
/
search.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
48
49
50
51
52
53
54
55
56
57
58
59
60
#include <stdio.h>
int get_imax(const int arr[], int size_of_array);
void sel_sort(int arr[], int size);
int main(void){
int my_array[10] = {0, 17, -41, 2, 93, 71, -104, 91, 1, 42};
int largest_bucket = get_imax(my_array, 10);
printf("The largest value is: %d, in bucket %d\n", my_array[largest_bucket], largest_bucket);
sel_sort(my_array, 10);
for(int i = 0; i < 10; i++){
printf("Sorted array: %d, %d\n", i, my_array[i]);
}
return 0;
}
int get_imax(const int arr[], int size_of_array){
int imax;
int max;
for(int i = 0; i < size_of_array; i++){
if(i == 0){
max = arr[i];
imax = i;
}
else if(arr[i] > max){
max = arr[i];
imax = i;
}
}
return imax;
}
void sel_sort(int arr[], int size){
int imax;
int temp;
for( int i = 0; i < size - 1; i++){
imax = get_imax(arr, size - i);
if (imax != size - 1 - i){
temp = arr[imax];
arr[imax] = arr[size - 1 -i];
arr[size - 1 - i] = temp;
}
}
}