快速排序;细分错误,但找不到哪里?

时间:2019-12-07 15:15:58

标签: c++

我做了一个快速排序算法,作为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;
}

QuickSort.cpp

#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);

因为一旦我注释掉,我就不会收到错误;但是,我不知道为什么。

1 个答案:

答案 0 :(得分:2)

更改import pims--------------------------------------------------------------------------- AttributeError Traceback (most recent call last) <ipython-input-70-c035db68c7c1> in <module> ----> 1 import pims ~\Anaconda3\lib\site-packages\pims\__init__.py in <module> ----> 1 from pims.api import * 2 3 from ._version import get_versions 4 __version__ = get_versions()['version'] 5 del get_versions ~\Anaconda3\lib\site-packages\pims\api.py in <module> 34 try: 35 import pims.pyav_reader ---> 36 if pims.pyav_reader.available(): 37 PyAVReaderTimed = pims.pyav_reader.PyAVReaderTimed 38 PyAVReaderIndexed = pims.pyav_reader.PyAVReaderIndexed AttributeError: module 'pims' has no attribute 'pyav_reader'

quickSort(pivot, end);quickSort(pivot+1, end);

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