-
Notifications
You must be signed in to change notification settings - Fork 1
/
select.c
40 lines (33 loc) · 1.01 KB
/
select.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
//
// Created by william on 2018/12/4.
//
#include "select.h"
#include "sort_check.h"
#include "sort_tool.h"
void extremum(ElementType const arr[], int lo, int hi, int *max, int *min) {
*max = *min = lo;
ElementType v = arr[hi];
while (lo <= hi) {
if (compare(v, arr[*min]) <= 0) *min = hi;
if (compare(arr[*max], v) <= 0) *max = hi;
v = arr[--hi];
}
}
void select_sort(ElementType arr[], unsigned int len) {
int j = len;
int max = 0, min = 0;
while (len / 2 < j--) {
int i = len - j - 1;
extremum(arr, i, j, &max, &min);
swap(arr[i], arr[min]);
//处理最大值为i的情况
if (max == i) max = min;
swap(arr[j], arr[max]);
}
}
int main() {
ElementType a[] = {1118, 7, 17, 9, 6, 1, 32, 3, 5, 6, 43, 2, 13, 23, 8, 1, 44, 55, 22, 43, 66, 123, 456, 42};
select_sort(a, sizeof(a) / sizeof(ElementType));
printf("%d\n", check_sorted(a, sizeof(a) / sizeof(ElementType)));
print_arr(a, sizeof(a) / sizeof(int));
}