#include<stack>
int Partition_2(int array[], int low, int high){
int begin = low;//从基准值位置开始
int end = high;
int pivot = array[low];// begin是坑的下标
//(begin,end)的区间是没有被比较过的数据
while (begin<end){
//重点:如果基准值在左边,需要从右边开始走
while (begin<end && array[end] >= pivot){
end--;
}
if (begin == end){
break;
}
//array[end]比基准值小了
//坑的下标是begin
array[begin] = array[end];
//end是坑的下标
while (begin<end && array[begin] <= pivot){
begin++;
}
//array[begin]比基准值大了
//坑的下标是end
array[end] = array[begin];
//begin变成坑的下标
}
//low基准值
//[low+1,begin]比基准值小
//[begin+1,high]比基准值大
//把基准值和比它小的最后一个数交换
array[begin] = pivot;
return begin;
}
void QuickSortNoR(int array[], int size){
std::stack<int> stack;
stack.push(0); //low
stack.push(size-1); //high
while (!stack.empty()){
//取左右边境
int high=stack.top();
stack.pop();
int low = stack.top();
stack.pop();
if (low >= high){
continue;
}
//第一步,先找基准值
int pivotIdx = Partition_2(array, low, high);
//[low,pivotIdx-1],先处理左边
stack.push(low);
stack.push(pivotIdx - 1);
//[pivotIdx+1,high],再处理右边
stack.push(pivotIdx + 1);
stack.push(high);
}
}