合并排序实现未按预期工作

时间:2017-06-02 20:37:45

标签: c++ sorting c++14 mergesort

我一直在努力实现合并排序;但实现方式不正确 - 输出包含的值不是原始数组的一部分。我已经尝试将它与其他人的实施(工作)进行比较,但似乎无法找到错误。

代码是: -

#include <iostream>

using namespace std;
void Merge (int A[], int lo, int hi, int mid){
    int i = lo;
    int k = 0;
    int j = mid+1;
    int R[hi-lo];

    while(i<=mid && j<=hi){
        if(A[i]<A[j]){
            R[k]=A[i];
            cout << A[i] << "; " << A[j] << "      " << i << "        " << j << endl;
            i++;
        }else {
            R[k]=A[j];
            cout << A[i] << "; " << A[j] << "      " << i << "        " << j << endl;
            j++;
        }k++;
    }
    while(i<=lo){
        R[k]=R[i];
        i++;k++;
    }
    while(j<=hi){
        R[k]=R[j];
        j++;k++;
    }
    int count =0;
    for(i=lo; i<hi; i++){
        A[i]=R[count];
        count++;
        cout << count << "    ;    " << i << "-/-/-/-/-/-/-" << flush;
    }
}
void mergeSort(int A[], int lo, int hi){
    //if(hi-lo+1<2)return;
    if (lo<hi){
    int mid = (lo+hi)/2;
    mergeSort(A, lo, mid);
    mergeSort(A, mid+1, hi);
    Merge (A, lo, hi, mid);}
}

int main(){
    int A[] = {1,5,3,4,8,9,150,7,51,65};
    int hi = sizeof(A)/sizeof(int);
    cout << hi << endl;
    mergeSort(A,0, hi);
    cout << endl << endl << endl << endl;
    for(int i=0; i<=hi; i++){
        cout<<A[i]<< "; " << flush;
    }
    return 0;
}

我试图搜索类似的Q(S),但找不到一个。

1 个答案:

答案 0 :(得分:1)

实现的问题是边界包含和错误数组的混合。

首先,看看合并中的最后2个while循环,当你的意思是R[k]=R[i];时,你做了R[k]=A[i];

在同一个函数上,你的for循环缺少迭代,循环保护应该是i<=hi而不是i<hi

hi的初始化也是错误的,因为你将它初始化为元素数而不是最后一个索引,你应该减去-1。

另外我不知道你的打印代码在做什么,所以我评论并添加了我自己的,我发现更清楚了。

我更正了您的代码如下:

   #include <iostream>

using namespace std;
void Merge (int A[], int lo, int hi, int mid){
    int i = lo;
    int k = 0;
    int j = mid+1;
    int R[hi-lo];

    cout << "input" << endl;
    for(int i=lo; i<=mid; i++)
        cout << A[i] << " ";
    cout << endl;
    for(int i=mid+1; i<=hi; i++)
        cout << A[i] << " ";

    cout << endl;

    while(i<=mid && j<=hi){
        if(A[i]<A[j]){
            R[k]=A[i];
            //cout << A[i] << "; " << A[j] << "      " << i << "        " << j << endl;
            i++;
        }else {
            R[k]=A[j];
            //cout << A[i] << "; " << A[j] << "      " << i << "        " << j << endl;
            j++;
        }k++;
    }
    while(i<=mid){
        R[k]=A[i];
        i++;k++;
    }
    while(j<=hi){
        R[k]=A[j];
        j++;k++;
    }
    int count =0;
    for(i=lo; i<=hi; i++){
        A[i]=R[count];
        count++;
        //cout << count << "    ;    " << i << "-/-/-/-/-/-/-" << flush;
    }

    cout << "output:" << endl;
    for(int i=lo; i<=hi; i++)
        cout << A[i] << " ";
    cout << endl;
}
void mergeSort(int A[], int lo, int hi){
    //if(hi-lo+1<2)return;
    if (lo<hi){
    int mid = (lo+hi)/2;
    mergeSort(A, lo, mid);
    mergeSort(A, mid+1, hi);
    Merge (A, lo, hi, mid);}
}

int main(){
    int A[] = {1,5,3,4,8,9,150,7,51,65};
    int hi = sizeof(A)/sizeof(int) -1;
    cout << hi << endl;
    mergeSort(A,0, hi);
    cout << endl << endl << endl << endl;
    for(int i=0; i<=hi; i++){
        cout<<A[i]<< "; " << flush;
    }
    return 0;
}