【问题标题】:quick sort c++ code error segmentation fault 11?快速排序C++代码错误分段错误11?
【发布时间】:2021-09-17 02:46:49
【问题描述】:

代码:


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

当我的某些输入数字具有相同的值时,我会收到“分段错误:11”。

例如,“4 7 7 3 1”的输入将产生以下输出的无限行:“start is 1 end is 3”。

请问我应该如何改进我的代码?我知道当您尝试访问无效内存时会发生 Segfault 11,我认为这很可能是我在这里所做的,尽管我似乎无法识别错误。谢谢!

【问题讨论】:

  • 请不要标记未使用的语言。
  • 您是否尝试过通过调试器逐步运行您的代码?我的经验是,在这种情况下,人们经常会找到这些错误的原因。
  • -fsanitize=addr 非常擅长找出此类问题的原因
  • 为了测试和调试崩溃之类的东西,请学习minimal reproducible example的概念。删除除最低限度之外的所有代码以复制崩溃,并使用硬编码值而不是用户输入。
  • 在这种情况下另一个有用的工具是内存调试器,例如 valgrind。

标签: c++ algorithm sorting quicksort


【解决方案1】:

所以这就是这里发生的事情。想象一下,在某个partition中,第一个元素(pivot)也是最大的。

这意味着在while 循环的每次迭代中,A[j]&lt;=pivot 的值都会为真,i 的值会增加。

由于 ij 在每次迭代中都增加 1,因此值相同,因此在循环结束时它们都等于 end+1

然后你正在调用partition(A, start, i-1),这实际上与partition(A, start, end) 相同——这意味着你有一个无限递归,一直持续到你到达......好吧,堆栈溢出和程序因段错误而崩溃。


最小的解决方法是确保子分区严格小于原始分区,方法是从它们中排除枢轴。所以是这样的:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-06-20
    • 1970-01-01
    • 2017-11-04
    • 2012-05-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多