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;
}


我需要对双精度向量进行排序。该代码运行,但矢量未在末尾排序。这可能是我忽略的事情。有什么想法吗?

最佳答案

您的quickSort例程的高级描述是:


输入原始向量和范围端点的副本作为输入
用副本做事
丢弃副本


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

07-25 20:34