-3

下面给出的是我能想到的快速排序技术的代码。我不确定它是否正确,但按照我的逻辑,我猜它应该可以正常工作。但是,我认为我做得太过火了,因为当我尝试在 DevC++ 上运行此代码时,它会崩溃并关闭程序。并非每个程序都会发生这种情况,因此显然只有此代码存在一些问题。

#include<iostream.h>
#include<conio.h>

int quick(int, int);

int split(int beg, int end);

int a[7] = { 43, 6, 235, 76, 23, 65, 29 };

int main() {
    quick(0, 6);
    getch();
    return 1;
}

int quick(int beg, int end) {
    //trial for self coding

    int loc = split(beg, end);
    quick(beg, loc - 1);//first half
    quick(loc + 1, end);//second half

    //end of coding

    cout << "\nThe sorted array is :\t";
    for (int i = 0; i < 7; i++)
        cout << a[i] << "\t";
    return 0;
}

//SPLIT FUNC STARTS
int split(int beg, int end) {
    int temp, loc, left, right, count = 1;
    left = beg;
    right = end;
    loc = beg;
    while (left != right) {
        if ((count % 2) != 0) {
            while (a[loc] <= a[right]) {
                right--;
            }
            if (loc == right)
                return loc;
            if (a[loc] > a[right]) {
                temp = a[loc];
                a[loc] = a[right];
                a[right] = temp;
                loc = right;
                left++;
                count++;
                continue;
            }
        }// end of count%2 if
        else {
            while (a[left] <= a[loc])
                left++;
            if (loc == left)
                return loc;
            if (a[loc] < a[left]) {
                temp = a[loc];
                a[loc] = a[right];
                a[right] = temp;
                loc = left;
                right--;
                count++;
                continue;
            }
        }//end of else
    }// end of while
    return loc;
}
4

2 回答 2

3

使用调试标志编译程序并在调试器中运行它。大多数 IDE 都提供“调试”按钮。GCC 将允许您使用 -g 选项编译调试标志,并且您可以使用 gdb。

在这种情况下,我用g++ -g quicksort.cpp && gdb a.out. 曾经在旁边,我用过run. 这立即给了我一个“无法访问内存”。错误以行号完成。 print <variable>将打印变量并quit退出。

出于教育目的,我特别没有提供实际的错误位置信息。

于 2012-09-14T18:35:37.247 回答
3

编译错误

我把它放在一个文件中并编译:

g++ -g yourcode.cpp

第一个问题:

yourcode.cpp:1:21: fatal error: iostream.h: No such file or directory
compilation terminated.

其次是:

yourcode.cpp: In function ‘int quick(int, int)’:
yourcode.cpp:23:5: error: ‘cout’ was not declared in this scope
yourcode.cpp:23:5: note: suggested alternative:
/usr/include/c++/4.6/iostream:62:18: note:   ‘std::cout’

而不是#include <iostream.h>,你应该有#include <iostream>。您还需要using namespace std;您的编译器可能已经过时了。大多数编译器都是免费的,因此我强烈建议您购买一个不会让您在学习时犯此类错误的编译器。我有一段时间没用过 Windows,但大概Eclipse在它上面的工作原理是一样的,而且Visual Studio Express也是免费的。我认为Qt Creator也适用于 Windows。选择其中任何一个,停止使用你现在正在使用的那个

我还删除了您为暂停程序而调用的奇怪的 DOS 输入功能。std::cin如果您想输入,请使用。

分段故障

现在,如果我运行它,我会得到:

Segmentation fault (core dumped)

所以这很有趣,gdb 说什么?

Starting program: /home/brendan/a.out 

Program received signal SIGSEGV, Segmentation fault.
0x00000000004008b7 in split (beg=0, end=6) at yourcode.cpp:55
55              while (a[left] <= a[loc])

这表明在某些时候,在第 55 行,a[left]或者a[loc]不是内存中的有效位置。您可能需要添加某种代码以确保leftloc保持在数组的范围内。

如果你迫切需要知道是什么导致你的程序崩溃,那么自己运行这个过程会比让别人为你做这个过程要快得多。这个过程很简单,因此很少有人愿意为您运行它。我只是作为一个例子做的,因为我意识到当你第一次开始编程时这并不明显。

注意:我使用命令行程序来检查它,因为它速度很快,但在 IDE 中使用调试器可能会更舒服。如果你运行 Eclipse,你应该能够只点击“调试”按钮(通常是一个绿色的 bug),它会告诉你这个确切的信息。

于 2012-09-14T18:47:15.260 回答