#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
void quicksort(int Arr[], int p, int q);
int partition(int Arr[], int p, int q);
int main()
{
int arr[10] = { 10,9,4,7,8,5,6,3,2,1 };
for (int i = 0; i < 10; i++)
cout << arr[i] << endl;
quicksort(arr, 0, 9);
cout << "排序后:" << endl;
for (int i = 0; i < 10; i++)
cout << arr[i] << endl;
return 0;
}
void quicksort(int Arr[], int p, int q)
{
if (p < q)
{
int r = partition(Arr, p, q);
quicksort(Arr, p, r-1);
quicksort(Arr, r+1, q);
}
}
int partition(int Arr[], int p, int q)
{
int x = Arr[p];
int i = p;
for (int j = p+1; j <= q; j++)
{
if (Arr[j] <=x)
{
i++;
swap(Arr[i],Arr[j]);
}
}
swap(Arr[p], Arr[i]);//不能把Arr[p]写成x;我也不知道为啥
return i;
}