1:用递归;(left<right)为进入条件
2:两边逼近:(low<high)-》先右边找到不满足的放到左边的(无意义位)空位置中,通过这个循环后就可以逼近low=high,这时把目标key付给v[low],即最后一个空位也填满了
#include <iostream>
2 #include <vector>
3 using namespace std;
4 void quicksort(int *v, int left, int right){
5 if(left < right){
6 int key = v[left];
7 int low = left;
8 int high = right;
9 while(low < high){
10 while(low < high && v[high] >= key){
11 high--;
12 }
13 v[low] = v[high];
14 while(low < high && v[low] <= key){
15 low++;
16 }
17 v[high] = v[low];
18 }
19 v[low] = key;
20 quicksort(v,left,low-1);
21 quicksort(v,low+1,right);
22 }
23 }
24 int main(){
25 int ivec[5];
26 int ic;
27 for(int ix=0;ix!=5;++ix)
28 {
29 cin>>ic;
30 ivec[ix]=ic;
31 }
32 for(int ix=0;ix!=5;ix++)
33 cout<<ivec[ix]<<" ";
34 cout<<"\n";
35 quicksort(ivec,0,4);
36 for(int ix=0;ix!=5;ix++)
37 cout<<ivec[ix]<<" ";
38 return 0;
39 }