我很感激你们愿意提供的任何帮助或建议 .

我为编程类编写了一个程序,用于计算冒泡排序和选择排序算法的比较 . 这是作业说明:

修改冒泡排序(对于整数),以便计算比较次数并将其作为函数的值返回 . 选择排序也一样 . 编写一个主方法,声明两个100个整数的数组,生成100个随机整数(您不需要限制范围),将每个随机数存储在两个数组中,这样您最终会得到两个相同的数组 . 将其中一个传递给修改后的冒泡排序并打印出返回值,然后将另一个数组传递给修改后的选择排序并打印出返回值(当然标记输出 - 输出格式的细节由您决定只要他们很清楚) . 重复此操作,阵列大小为1,000,10,000和100,000 .

我的程序在Xcode中完美运行,但在编译时遇到分段错误 . 我学校的服务器运行一个pre-c 0x版本的编译器,我的计算机更现代的版本g也失败了 .

这是错误:

分段故障:11

我尝试在gdb中单步执行此操作,但我对此非常陌生,我不确定如何在填充数组时跳过这些长循环 .

我认为向量可能更容易调试(考虑到我对SO这个问题的研究),唉,教授要求我们使用数组 .

注意:如果您想运行该程序,则该程序对处理器来说相当密集 .

这是代码:

#include <iostream> /* cout */
#include <ctime> /* time for seeding RNG */
#include <cstdlib> /* srand() */

void randomFill(int array[], int size); //fills an array with random numbers
void copyArray(int selectionArray[], int bubbleArray[], int size); //copies from selection to bubble arrays

//ascending sorts
int bubbleSort(int IntsBubble[], int size);
int selectionSort(int IntsSelection[], int size);

int main() {
    srand(static_cast<int>(time(NULL))); //seed random number generator

    /* 'Twin' arrays declared, filled with random numbers and sorted */

    /* 100 Integers */
    int hundredIntsSelection[100];
    randomFill(hundredIntsSelection, 100);
    int hundredSelectionComparisons = selectionSort(hundredIntsSelection, 100);

    int hundredIntsBubble[100];
    copyArray(hundredIntsSelection, hundredIntsBubble, 100);
    int hundredBubbleComparisons = bubbleSort(hundredIntsBubble, 100);

    /* 1000 Integers */
    int thousandIntsSelection[1000];
    randomFill(thousandIntsSelection, 1000);
    int thousandSelectionComparisons = selectionSort(thousandIntsSelection, 1000);

    int thousandIntsBubble[1000];
    copyArray(thousandIntsSelection, thousandIntsBubble, 1000);
    int thousandBubbleComparisons = bubbleSort(thousandIntsBubble, 1000);

    /* 10,000 Integers */
    int tenThousandIntsSelection[10000];
    randomFill(tenThousandIntsSelection, 10000);
    int tenThousandSelectionComparisons = selectionSort(thousandIntsSelection, 10000);

    int tenThousandIntsBubble[10000];
    copyArray(tenThousandIntsSelection, tenThousandIntsBubble, 10000);
    int tenThousandBubbleComparisons = bubbleSort(tenThousandIntsSelection, 10000);

    /* 100,000 Integers */
    int hundredThousandIntsSelection[100000];
    randomFill(hundredThousandIntsSelection, 100000);
    int hundredThousandSelectionComparisons = selectionSort(hundredIntsSelection, 100000);

    int hundredThousandIntsBubble[100000];
    for (int counter = 0; counter < 100000; counter++) { /* The program encounters runtime errors if I pass an array this large to copyArray */
        hundredThousandIntsBubble[counter] = hundredThousandIntsSelection[counter];
    }
    int hundredThousandBubbleComparisons = bubbleSort(hundredThousandIntsBubble, 100000);

    /* Here we're outputting our results to the user */
    std::cout << "This program sorts twin arrays of 100, 1,000, 10,000 and 100,000 random integers and records the number of comparisons, displayed below.";
    std::cout << std::endl;
    std::cout << std::endl << "Results of random selection sort (100 integers): " << hundredSelectionComparisons << " comparisons.";
    std::cout << std::endl << "Versus bubble sort of the same: " << hundredBubbleComparisons << " comparisons.";
    std::cout << std::endl;
    std::cout << std::endl << "Results of random selection sort (1,000 integers): " << thousandSelectionComparisons << " comparisons.";
    std::cout << std::endl << "Versus bubble sort of the same: " << thousandBubbleComparisons << " comparisons.";
    std::cout << std::endl;
    std::cout << std::endl << "Results of random selection sort (10,000 integers): " << tenThousandSelectionComparisons << " comparisons.";
    std::cout << std::endl << "Versus bubble sort of the same: " << tenThousandBubbleComparisons << " comparisons.";
    std::cout << std::endl;
    std::cout << std::endl << "Results of random selection sort (100,000 integers): " << hundredThousandSelectionComparisons << " comparisons.";
    std::cout << std::endl << "Versus bubble sort of the same: " << hundredThousandBubbleComparisons << " comparisons.";
    std::cout << std::endl << std::endl;
    /* Bubble sort makes far more swaps than Selection sort, even though they make the same number of comparisons. */
    return 0;
}
//fills arrays with random ints for sorting
void randomFill(int array[], int size) {
    for (int counter = 0; counter < size; counter++) {
        array[counter] = rand();
    }
}

void copyArray(int selectionArray[], int bubbleArray[], int size) {
    for (int counter = 0; counter < size; counter++) {
        bubbleArray[counter] = selectionArray[counter];
    }
}

int bubbleSort(int IntsBubble[], int size) {
    int temp;
    int counter;
    bool swap;

    do {
        swap = false;
        for (counter = 0; counter < size; counter++) {
            if (IntsBubble[counter] > IntsBubble[counter + 1]) {
                temp = IntsBubble[counter];
                IntsBubble[counter] = IntsBubble[counter + 1];
                IntsBubble[counter + 1] = temp;
                swap = true;
            }
        }
    } while (swap);
    return counter;
}

int selectionSort(int IntsSelection[], int size) {
    int startScan, minIndex, minValue, index = 0;

    for (startScan = 0; startScan < (size - 1); startScan++) {
        minIndex = startScan;
        minValue = IntsSelection[startScan];
        for (index = startScan + 1; index < size; index++) {
            if (IntsSelection[index] < minValue) {
                minValue = IntsSelection[index];
                minIndex = index;
            }
        }
    }
    return index;
}

如果我可以添加任何内容以帮助我更轻松,请告诉我 . 我也很感激任何一般的调试建议或资源 . 到目前为止,我们还没有涵盖这个课程 . 谢谢!

编辑:让它通过gdb运行得到此错误 . 我将继续搜索,但快速网络搜索没有发现任何有用的东西:

warning: Could not open OSO archive file "/BinaryCache/corecrypto/corecrypto-233.1.2~26/Symbols/BuiltProducts/libcorecrypto_static.a"
warning: `/BinaryCache/coreTLS/coreTLS-35.1.2~2/Objects/coretls.build/coretls.build/Objects-normal/x86_64/system_coretls_vers.o': can't open to read symbols: No such file or directory.
warning: Could not open OSO archive file "/BinaryCache/coreTLS/coreTLS-35.1.2~2/Symbols/BuiltProducts/libcoretls_ciphersuites.a"
warning: Could not open OSO archive file "/BinaryCache/coreTLS/coreTLS-35.1.2~2/Symbols/BuiltProducts/libcoretls_handshake.a"
warning: Could not open OSO archive file "/BinaryCache/coreTLS/coreTLS-35.1.2~2/Symbols/BuiltProducts/libcoretls_record.a"
warning: Could not open OSO archive file "/BinaryCache/coreTLS/coreTLS-35.1.2~2/Symbols/BuiltProducts/libcoretls_stream_parser.a"

TLS甚至与C有什么关系?在学校机器上运行它说它缺少debuginfos但是我必须是root才能安装它 .

Edit2:I followed this suggestion为我的大型数组制作指针并手动取消分配它们 . 这似乎没有改变任何东西 .