核心思想:每次都是将选定值,比如是p[low],将他给丢过来丢过去,最终到合适的位置
#include<bits/stdc++.h>
using namespace std;
void PrintArr(int *p, int len)
{
for (int i = 0; i < len; i++)
cout << p[i] << " ";
cout << endl;
}
int Position(int *p, int low, int high)
{
int key = p[low];
while (low < high)
{
while (low < high && p[high] >= key)
high--;
p[low] = p[high];
while (low < high && p[low] <= key)
low++;
p[high] = p[low];
}
p[low] = key;
return low;
}
void QuickSort(int *p,int low,int high)
{
int pos;
if (low >= high)
{
}
else
{
pos = Position(p, low, high);
QuickSort(p, low, pos-1);
QuickSort(p, pos + 1, high);
}
}
int main()
{
int a[] = { 50,10,90,30,70,40,80,60,20 };
int len = sizeof(a) / sizeof(int);
QuickSort(a,0,8);
PrintArr(a, len);
system("pause");
return 0;
}