/*
*/
#include <stdio.h>
#include <stdlib.h>
#include <
string.h>
#include <math.h>
int partition(
int *a,
int start,
int end,
int key)
{
int i = start, j =
end;
while (
1)
{
while (a[j] >=
key)
{
j--
;
if (j ==
start) {
return start;
}
}
while (a[i] <= key && i <=
end) {
i++
;
}
if (i <
j)
{
int tmp;
tmp =
a[i];
a[i] =
a[j];
a[j] =
tmp;
}
else
return j;
}
}
void quick_sort(
int *a,
int start,
int end)
{
if (start <
end)
{
int q =
partition(a, start, end, a[start]);
int tmp;
tmp =
a[start];
a[start] =
a[q];
a[q] =
tmp;
quick_sort(a, start, q -
1);
quick_sort(a, q +
1, end);
}
}
int main()
{
int a[] = {
9,
8,
10,
2,
1,
19,
10,
20};
int aa[] = {
19,
19,
19,
19};
quick_sort(a, 0,
sizeof(a) /
sizeof(
int) -
1);
for (
int i =
0; i <
sizeof(a) /
sizeof(
int); i++
) {
printf("%d ", a[i]);
}
printf("\n");
}
转载于:https://www.cnblogs.com/leiatpku/p/3348693.html
转载请注明原文地址: https://mac.8miu.com/read-13834.html