#include <stdio.h>
void Swap(int *a, int*b)
{
int tmp = *a;
*a = *b;
*b = tmp;
}
int PartSort(int* a, int left, int right) //先走一趟的排序。
{
int keyi = left;
while (left < right)
{
//右边找小的。
while (a[right] >= a[keyi] &&right>left)
{
right--;
}
//左边找大;
while (a[left] <=a[keyi] && right > left)
{
left++;
}
Swap(&a[right], &a[left]);
}
Swap(&a[left], &a[keyi]);
return left; //返回中间比较节点。
}
void QuickSort(int* a, int begin, int end)
{
if (begin >= end)
return;
int keyi = PartSort(a, begin, end);
//区间:【begin,keyi-1】keyi【keyi+1,end】
QuickSort(a, begin, keyi - 1);
QuickSort(a, keyi + 1,end);
}
int main()
{
int a[] = { 4,7,1,9,3,6,5,8,3,2,0, };
QuickSort(a, 0, sizeof(a) / sizeof(int) - 1);
for (int i = 0;i < sizeof(a) / sizeof(int); i++)
{
printf("%d ", a[i]);
}
return 0;
}