C++ 快速排序不排序

C++ QuickSort not sorting

本文关键字:排序 快速排序 C++      更新时间:2023-10-16
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>&,以便您执行的操作引用原始向量而不是副本。