在做算法导论第七章思考题7-2b时,自己所改的代码如下:
const vector<int>QUCKYSORT::partition1(vector<int>& A, int L, int R)
{
// TODO: 在此处插入 return 语句
int x=A[R];
int i=L-1;
vector<int> result;
int temp;//交换i,j的值
for (int j = L;j < R;++j)
{
if (A[j] < x)
{
++i;
temp=A[i];
A[i]=A[j];
A[j]=temp;
}
}
temp = A[i+1];
A[i+1] = x;
A[R] = temp;
int l1=i+1;
for (int p = l1+1;p <= R;++p)
{
if (A[p] == x)
{
++l1; //寻找t
temp = A[l1];
A[l1] = A[p];
A[p] = temp;
}
}
result.push_back(i+1);
result.push_back(l1);
return result;
}
程序经过验证