简体   繁体   English

快速排序 C++ 代码错误分段错误 11?

[英]quick sort c++ code error segmentation fault 11?

Code:代码:


#include <iostream>
#include <stdlib.h>
#include <time.h>
using namespace std;
void partition(int *A, int start, int end){ //end is the position of the last number
    cout<<"start is "<<start<<" end is "<<end<<endl;
    if(end - start+1 > 1){
        int i = start + 1, j = start + 1, pivot = A[start];
        while(j <= end){
            if(A[j] <= pivot){
                swap(A[j], A[i]);
                i++;
            }
            j++;
        }
        swap(A[start], A[i-1]);
        partition(A, start, i-1);
        partition(A, i, end);
    }
    
}

void QuickSort(int *A, int n){
    partition(A, 0, n-1);
}


int main() {
    int n, method;
    string sortMethod[ ] = {
            "Insertion sort", "Selection sort", "Bubble sort", "Merge sort", "Quick sort"
    };
    int m = sizeof(sortMethod)/sizeof(string);
    srand(time(0));
    
    while (true){
        cout << "Number of test marks: ";
        cin >> n;
        if ( n==0 ) break;
        int *A = new int[n];
        for (int i=0; i < n; i++) cin>>A[i];
        //A[i] = rand()%101;

        for (int i=0; i < n; i++)cout << A[i] << " ";
        cout<<endl<<endl;


        cout << "Sorting method: \n"; 
        for (int i=0; i < m; i++) cout << i+1 << ": " << sortMethod[i] << endl;
        // cin >> method;
        method = 5;
        cout<<endl;
        int t = time(0);
        
        QuickSort(A,n);

        if (method > m) continue;       
        cout << sortMethod[method-1] << endl;
        if (n <= 100) {
            for (int i=0; i < n; i++) {
                if (i> 0 && i%10==0) cout << endl;
                cout << A[i] << " ";
            }
            cout << endl;
        }
        else {
            cout << "Sorting completed in " <<  time(0) - t  << " sec.\n";
        }
        cout << endl;
    } 
    cout << "Program ends here."<<endl;
    return 0;
}

When some of my input numbers have the same value, I get a "Segmentation fault: 11".当我的某些输入数字具有相同的值时,我会收到“分段错误:11”。

For example, an input of "4 7 7 3 1" would produce infinite lines of the following output: "start is 1 end is 3".例如,输入“4 7 7 3 1”将产生以下输出的无限行:“start is 1 end is 3”。

May I ask how should I improve my code?请问我应该如何改进我的代码? I know that Segfault 11 happens when you try to access an invalid memory, and i think that's likely what I did here, although i cant seem to identify the error.我知道当您尝试访问无效内存时会发生 Segfault 11,我认为这可能是我在这里所做的,尽管我似乎无法识别错误。 Thanks!谢谢!

So this is what's happening here.所以这就是这里发生的事情。 Imagine that in a certain partition the first element (the pivot ) is also the largest.想象一下,在某个partition ,第一个元素( pivot )也是最大的。

This means that in every iteration of the while loop the value of A[j]<=pivot will be true and i will be increased.这意味着在while循环的每次迭代中, A[j]<=pivot将为真,并且i将增加。

Since both i and j are increased by 1 in every iteration there value is the same, so at the end of the loop they are both equal to end+1 .由于ij在每次迭代中都增加 1 ,因此值相同,因此在循环结束时它们都等于end+1

Then you are invoking partition(A, start, i-1) which is really the same as partition(A, start, end) - meaning you have an infinite recursion which continues until you reach... well, stack overflow and the program crush with segfault.然后你调用partition(A, start, i-1)这与partition(A, start, end)真的一样——这意味着你有一个无限递归,它会一直持续到你到达......好吧,堆栈溢出和程序用段错误粉碎。


The minimal fix will be to make sure that the sub partitions are strictly smaller than the original one by excluding the pivot from them.最小的修复是通过从子分区中排除枢轴来确保子分区严格小于原始分区。 So something like this:所以像这样:

        partition(A, start, i-2); // i-1 is the pivot, so we don't need to include it
        partition(A, i, end);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM