Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/262.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++ - Fatal编程技术网

C++ 快速排序;分段错误,但可以';我找不到哪里?

C++ 快速排序;分段错误,但可以';我找不到哪里?,c++,C++,我在main中做了一个快速排序算法作为普通函数,然后尝试将其转移到一个类中(根据我的导师的要求)。然而,我现在得到了一个分段错误,但我无法找出它发生在哪里。源代码如下;谢谢 main.cpp #include "MergeSort.h" #include "QuickSort.h" #include <iostream> using namespace std; const int SIZE = 10; int main() { cout << "This i

我在main中做了一个快速排序算法作为普通函数,然后尝试将其转移到一个类中(根据我的导师的要求)。然而,我现在得到了一个分段错误,但我无法找出它发生在哪里。源代码如下;谢谢

main.cpp

#include "MergeSort.h"
#include "QuickSort.h"
#include <iostream>
using namespace std;

const int SIZE = 10;

int main() {
    cout << "This is compiling.\n";
    int testArray[SIZE] = {5,3,9,2,1,3,8,1,7,9};
    for (int i = 0; i < SIZE; i++) {
        cout << testArray[i];
    }
    QuickSort test(testArray, SIZE);
    int * result = test.getArray();

    cout << endl;
    for (int i = 0; i < SIZE; i++) {
        cout << result[i];
    }
    return 0;
}
因为一旦我把它注释掉,我就不会得到错误;但是,我不明白为什么。

更改
快速排序(pivot,end)
quickSort(pivot+1,end)

if(start==end)
如果(开始>=结束)
,则转到


您的代码现在可以正常运行。

一个轴很可能是
start
索引,然后
quickSort(start,pivot-1)
将使用
start>end
调用
quickSort
。我在quickSort函数中将
if(start==end)
更改为
if(start>=end)
,但这并不能修复错误。你是否认为我还遗漏了什么,或者我误解了你的评论/我的逻辑无效?这可能是你练习/学习的好机会。如果您没有调试程序来帮助您逐步完成代码,我将从打印一些诊断消息开始。也许类似于std::cout
#include "QuickSort.h"
//constructor
QuickSort::QuickSort(const int anArray[], int aSize) {
    array_p = new int[aSize];
    size = aSize;

    for (int i = 0; i < size; i++)
        array_p[i] = anArray[i];

    quickSort(0, aSize - 1);
    return;
}
//destructor
QuickSort::~QuickSort() {
    delete [] array_p;
    return;
}
//accessor function for array
int * QuickSort::getArray() {
    return array_p;
}

//PRIV MEM FUNCTIONS
void QuickSort::quickSort(int start, int end)
{
    if (start == end)
        return;

    int pivot;
    pivot = partition(array_p, start, end);

    //quickSort everything before where pivot is now
    quickSort(start, pivot - 1);

    //quickSort everything after where pivot is now
    quickSort(pivot, end);

    return;
}
int QuickSort::partition(int a[], int start, int end)
{
    int first, last, pivot;
    pivot = end;
    first = start;
    last = end - 1; //minus one is because pivot is at last

    while (first < last) {
        if (a[first] > a[pivot] && a[last] < a[pivot]) {
            swap(a, first, last);
            first++;
            last--;
        }
        else {
            if (a[first] <= a[pivot])
                first++;
            if (a[last] >= a[pivot])
                last--;
        }
    }

    if (a[pivot] > a[first]) {
        swap(a, pivot, first + 1);
        return first + 1;
    }
    else {
        swap(a, pivot, first);
        return first;
    }

}
void QuickSort::swap(int a[], int indexOne, int indexTwo)
{
    int temp = a[indexOne];
    a[indexOne] = a[indexTwo];
    a[indexTwo] = temp;
    return;
}
quickSort(start, pivot - 1);