1 | function quickSort |
感觉更为简洁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
void swap(int *a, int *b)
{
int t = *a;
*a = *b;
*b = t;
}
void quickSort(int a[], int aStart, int aEnd)
{
if (aStart >= aEnd)
{
return;
}
int midvalue = a[aEnd];
int i = aStart - 1;
for (int j = aStart; j <= aEnd-1; j++)
{
if (a[j] <= midvalue)
{
i++;
swap(&a[j],&a[i]);
}
}
swap(&a[i+1], &a[aEnd]);
quickSort(a, aStart, i + 1 - 1);
quickSort(a, i + 1 + 1, aEnd);
}
void printArray(int a[], int n)
{
for (int i = 0; i < n; i++)
{
printf("%d\t", a[i]);
}
printf("\n");
}
int main()
{
int a[] = { 1,4,3,2 };
int n = sizeof(a) / sizeof(a[0]);
printArray(a, n);
quickSort(a, 0, n - 1);
printArray(a, n);
}