C++ QuickSort没有排序

0 c++ sorting vector quicksort

void quickSort(vector<double> unsortedData, int leftBound, int rightBound) {

    if (leftBound < rightBound) {
        double i = partitionStep(unsortedData, leftBound, rightBound);
        quickSort(unsortedData, leftBound, i-1);
        quickSort(unsortedData, i + 1, rightBound);
    }
}

double partitionStep(vector<double> unsortedData, int leftBound, int rightBound) {

    double pivot = unsortedData[rightBound];

    while (leftBound <= rightBound) {

        while ((unsortedData[leftBound] < pivot)) {
            leftBound++;
        }
        while ((unsortedData[rightBound] > pivot)) {
            rightBound--;
        }

        if (unsortedData[leftBound] == unsortedData[rightBound]) {
            leftBound++;
        } else if (leftBound < rightBound) {
            double temp = unsortedData[leftBound];
            unsortedData[leftBound] = unsortedData[rightBound];
            unsortedData[rightBound] = temp;
        }
    }

    return rightBound;
}
Run Code Online (Sandbox Code Playgroud)

我需要对双打矢量进行排序.此代码运行但最后没有对向量进行排序.这可能是我忽视的东西.思考?

小智 5

quickSort日常工作的高级描述是:

  • 将原始矢量的副本和范围的端点作为输入
  • 做副本的东西
  • 丢弃副本

这不是特别有用.将输入参数更改为,vector<double>&以便您使用对原始向量的引用而不是副本进行操作.