-
Notifications
You must be signed in to change notification settings - Fork 1
/
SortQuick.c
61 lines (56 loc) · 1.03 KB
/
SortQuick.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
61
/*********************************************************************
* 文件: SortQuick.c
* 作者: Ray1024
* 日期: 2019.1.30
* 简介: 排序算法——快速排序
**********************************************************************/
#include <stdio.h>
void SortQuick(int a[], int left, int right)
{
if (left < right)
{
int i = left;
int j = right;
int x = a[left];
while (i < j)
{
while (a[j] >= x && i < j)
{
j--;
}
if (i < j)
{
a[i++] = a[j];
}
while (a[i] <= x && i < j)
{
i++;
}
if (i < j)
{
a[j--] = a[i];
}
}
a[i] = x;
SortQuick(a, left, i - 1);
SortQuick(a, i + 1, right);
}
}
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");
SortQuick(a, 0, sizeof(a) / sizeof(int) - 1);
printf("排序后:");
for (int i = 0; i < sizeof(a) / sizeof(int); ++i)
{
printf("%d ", a[i]);
}
printf("\n");
return 0;
}