10(0) |
5(1) |
6(2) |
84(3) |
55(4) |
1(5) |
5(6) |
22(7) |
48(8) |
20(9) |
利用第一个数作为基准值,temp = 10;high--;直到arr[6]=5;则此时arr[0]=5;
low++直到arr[3] = 84;则arr[6] = 84;high--直到arr[5] = 1,则arr[3] = 1;low++直到arr[4]=55,arr[5] = 55;
arr[4] = 10;
5 |
5 |
6 |
1 |
10 |
55 |
84 |
22 |
48 |
20 |
void partion(vector<int>& nums,int left,int right){
if(left<right){
int temp = nums[left];
int low = left;
int high = right;
while(low<high){
while(low<high&&nums[high]<temp){
high--;
}
nums[low] = nums[high];
while(low<high&&nums[low]<=temp){
low++;
}
nums[high] = nums[low];
}
nums[low] = temp;
partion(nums,left,low-1);
partion(nums,low+1,right);
}
}