递归:
void QuickSort(vector<int> &vec,int low,int high)
{
int i = low;
int j = high;
int tmp = vec[low];
while (i < j)
{
while (i < j&&vec[j] >= tmp)--j;
vec[i] = vec[j];
while (i < j&&vec[i] <= tmp)++i;
vec[j] = vec[i];
}
vec[i] = tmp;
if (i - low - 1 > 0)
{
QuickSort(vec, low, i - 1);
}
if (high - i - 1 > 0)
{
QuickSort(vec, i + 1, high);
}
}
非递归
void quickSort(vector<int> &vec, int low, int high)
{
stack<int> st;
st.push(low);
st.push(high);
while (!st.empty())
{
int j= st.top();
int savehigh = j;
st.pop();
int i = st.top();
int savelow = i;
st.pop();
int tmp = vec[i];
while (i < j)
{
while (i < j&&vec[j] >= tmp)--j;
vec[i] = vec[j];
while (i < j&&vec[i] <= tmp)++i;
vec[j] = vec[i];
}
vec[i] = tmp;
if (i - savelow - 1 > 0)
{
//QuickSort(vec, low, i - 1);
st.push(savelow);
st.push(i - 1);
}
if (savehigh - i - 1 > 0)
{
//QuickSort(vec, i + 1, high);
st.push(i + 1);
st.push(savehigh);
}
}
}