Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/152.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 数字列表中的最后两项未交换_C++_Sorting_Quicksort - Fatal编程技术网

C++ 数字列表中的最后两项未交换

C++ 数字列表中的最后两项未交换,c++,sorting,quicksort,C++,Sorting,Quicksort,所以我现在的问题是,我已经编写了一个快速排序算法,它确实有效。它始终将所有数字从最小到最大进行排序。然而,在最后总是有两个项目需要交换,我不确定在哪里实现交换。提前谢谢 #include <iostream> #include <iomanip> using namespace std; void swap(double* a, double* b) { double temp = *a; *a = *b; *b = temp; } int

所以我现在的问题是,我已经编写了一个快速排序算法,它确实有效。它始终将所有数字从最小到最大进行排序。然而,在最后总是有两个项目需要交换,我不确定在哪里实现交换。提前谢谢

#include <iostream>
#include <iomanip>

using namespace std;

void swap(double* a, double* b) {
    double temp = *a;
    *a = *b;
    *b = temp;
}

int partition(double arr[], int start, int end) {
    double pivot = arr[end];
    int pivotIndex = start;
    int index;

    for (index = 0; index < end; index++) {
        if (arr[index] < pivot) {
            swap(arr[index], arr[pivotIndex]);
            pivotIndex++;
        }
    }
    swap(arr[pivotIndex], arr[index]);
    return index;
}

void quickSort(double arr[], int start, int end) {
    if (start < end) {
        int part = partition(arr, start, end);
        quickSort(arr, start, part - 1);
        quickSort(arr, part + 1, end);
    }
}


void main() {
    double numList[10];

    for (int i = 0; i < size(numList); i++) {
        numList[i] = rand() % 100;
        cout << setw(2) << left << numList[i] << "  ";
    }

    cout << endl;
    quickSort(numList, 0, size(numList) - 1);

    for (int i = 0; i < size(numList); i++) {
        cout << setw(2) << left << numList[i] << "  ";
    }
}
#包括
#包括
使用名称空间std;
无效掉期(双*a,双*b){
双温=*a;
*a=*b;
*b=温度;
}
整数分区(双arr[],整数开始,整数结束){
双枢轴=arr[端];
int pivotIndex=开始;
整数指数;
对于(索引=0;索引cout您正在实现Lomuto分区方案,但是您没有正确地转录partition()

int partition(double arr[], int start, int end) {
    double pivot = arr[end];
    int pivotIndex = start;
    int index;

 // Mistake 1: Start at the first element of the partition, not 0! 
 // for (index = 0; index < end; index++) {
    for (index = start; index < end; index++) {
        if (arr[index] < pivot) {
            swap(arr[index], arr[pivotIndex]);
            pivotIndex++;
        }
    }

 // Mistake 2: Swap the last element of the partition.
 // swap(arr[pivotIndex], arr[index]);
    swap(arr[pivotIndex], arr[end]);

//  Mistake 3: return the pivot index.
//  return index;
    return pivotIndex;
}
int分区(双arr[],int开始,int结束){
双枢轴=arr[端];
int pivotIndex=开始;
整数指数;
//错误1:从分区的第一个元素开始,而不是从0开始!
//对于(索引=0;索引
只需使用
std::sort
即可
std::swap
存在。重命名函数
myswap
,然后重试。为什么
分区
要查看从
0
开始的元素,而不是从
开始的元素?我很确定可以让
数据透视索引
从数组的末尾运行,并触发未定义的行为通过禁止进入的方式。