forked from Ray1024/algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SortChoose.c
50 lines (45 loc) · 901 Bytes
/
SortChoose.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
/*********************************************************************
* 文件: SortChoose.c
* 作者: Ray1024
* 日期: 2019.1.29
* 简介: 排序算法——选择排序
**********************************************************************/
#include <stdio.h>
int SortChoose(int a[], int len)
{
for (int i = 0; i < len - 1; ++i)
{
int min = i;
for (int j = i + 1; j < len; ++j)
{
if (a[j] < a[min])
{
min = j;
}
}
if (min != i)
{
int tmp = a[min];
a[min] = a[i];
a[i] = tmp;
}
}
}
int main()
{
int a[] = {30, 5, 3, 26, 0, 17, 15, 20, 2, 100, 4, 9, 87};
printf("排序前:");
for (int i = 0; i < sizeof(a) / sizeof(int); ++i)
{
printf("%d ", a[i]);
}
printf("\n");
SortChoose(a, sizeof(a) / sizeof(int));
printf("排序后:");
for (int i = 0; i < sizeof(a) / sizeof(int); ++i)
{
printf("%d ", a[i]);
}
printf("\n");
return 0;
}