#include<iostream>
using namespace std;
int partition(int a[], int first, int end)
{
int i = first;
int j = end;
while (i <j)
{
for (; j > i && a[j] >= a[i]; --j);
if (j > i)
{
int temp = a[j];
a[j] = a[i];
a[i] = temp;
++i;
}
for (; j > i && a[i] <= a[j]; ++i);
if (j > i)
{
int temp = a[j];
a[j] = a[i];
a[i] = temp;
--j;
}
}
return i;
}
void QuickSort(int a[], int first, int end)
{
if (end <= first)
return;
int pos = partition(a, first, end);
QuickSort(a, first, pos - 1);
QuickSort(a, pos + 1, end);
}
int main()
{
int a[10] = { 5,42,1,65,1,0,887,12,11,3 };
QuickSort(a, 0, 9);
for (int i = 0; i < 10; ++i)
cout << a[i]<<" ";
}
|