0

我已经用 Python 编程一年多了,我刚回到 C++,学习一些基本算法。我从合并排序开始,但它给了我一个分段错误。帮助将不胜感激。我想我调试了它发生的地方,但我无法理解为什么。

#include <iostream>
using namespace std;
void* merge(int array1[], int array2[], int low, int mid, int high){
    int i, j, k;
    for (i = low,j = mid, k = low; i < mid, j < high; k++){
        if (array1[i] < array1[j]){
            array2[k] = array1[i];
            i++;
            }
        else{
            array2[k] = array1[j];
            j++;
        }
    }
    if (i == mid){
        for (; j < high; j++, k++){
            array2[k] = array1[j];
            }
    }
else{
    for (; i < mid; i++, k++){
        array2[k] = array1[i];
    }
}
void* merge_sort(int array1[], int copy[], int low, int high){
    int mid;
    int range = high - low;
    if (range == 1)
        copy[low] = array1[low];
    else{
        mid = low + range/2;
        merge_sort(array1, copy, low, mid);
        // Segmentation error seems to be here.
        merge_sort(array1, copy, mid, high);
     }
     merge(array1, copy, low, mid, high);
}

int main()
{
    int n, temp;
    cout << "How many numbers do you want to enter?" << endl;
    cin >> n;
    int numarray[n];
    for (int i = 0; i < n; i++){
        cin >> numarray[i];
    }
    int dumarray[n];  // Used for filling elements from merge sort
    merge_sort(numarray, dumarray, 0, n);
    cout << "Merge Sort" << endl;
    for (int i = 0; i < n; i++){
        cout << dumarray[i] << endl;
    } 
    return 0;
}

编辑:

#include <iostream>
using namespace std;

void merge_sort(int [], int [], int, int);

void merge(int array1[], int array2[], int low, int mid, int high){
    int i, j, k;
    for (i = low,j = mid, k = low; i < mid, j < high; k++){
        if (array1[i] < array1[j]){
            array2[k] = array1[i];
            i++;
            }
        else{
            array2[k] = array1[j];
            j++;
        }
    }
    if (i == mid){
        for (; j < high; j++, k++){
            array2[k] = array1[j];
            }
    }
    else{
        for (; i < mid; i++, k++){
            array2[k] = array1[i];
            }
    }
}

void merge_sort(int array1[], int copy[], int low, int high){
    int mid;
    int range = high - low;
    mid = low + range/2;
    if (range == 1)
        copy[low] = array1[low];
    else{
        mid = low + range/2;
        merge_sort(array1, copy, low, mid);
        merge_sort(array1, copy, mid, high);
    merge(array1, copy, low, mid, high);
    }
}
int main(){
    int n, temp;
cout << "How many numbers do you want to enter?" << endl;
cin >> n;
int numarray[n];
for (int i = 0; i < n; i++){
    cin >> numarray[i];
}
    int dumarray[n];
    merge_sort(numarray, dumarray ,0, n);
    for (int i = 0; i < n; i++){
        cout << dumarray[i] << endl;
    }

}
4

1 回答 1

1

中间值的计算似乎是错误的。尝试计算 mid 作为

mid = low + (high-low)/2

编辑:merge(array1, copy, low, mid, high);在 else 括号内移动。

else
{
    mid = low + range/2;
    merge_sort(array1, copy, low, mid);
    merge_sort(array1, copy, mid, high);
    merge(array1, copy, low, mid, high);
}

merge功能也有很多错误。初始 for 循环中的条件检查 i < mid, j < high由逗号分隔,这意味着它仅检查j < high和丢弃 i 的条件检查。

于 2013-08-23T05:52:55.933 回答