Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/142.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 合并排序实现未按预期工作_C++_Sorting_C++14_Mergesort - Fatal编程技术网

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

C++ 合并排序实现未按预期工作,c++,sorting,c++14,mergesort,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];

我一直在尝试实现合并排序;但是这个实现在某种程度上是不正确的——输出包含的值不是原始数组的一部分。我试着将它与其他人的实现工作进行比较,但似乎没有发现错误

代码是:-

#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,但没有找到。

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

首先,看看merge中的最后两个while循环:R[k]=R[i];当你的意思是R[k]=A[i]


在同一个函数上,for循环缺少一个迭代,如果我已经尝试将它与其他人的implementationworking进行比较,那么循环保护应该是正确的-你应该调试你的版本,而不是试图逐行找出你有什么不同。第二,intr[hi-lo];不是有效的C++,因为C++中的数组只能用常量表达式来声明条目的数量。只是猜测,但是忽略其声明的非法性的R数组看起来可能太小了;如果hi是源数组的有效索引,则R的大小可能应该是hi-lo+1。正如目前所写,它比源数组少保存一个元素。@PeteBecker首先感谢您的回复。现在,即使在声明R legaldidn'tKnown the constfactor并更正了i lo+1的长度后,输出仍然保持不变。并且@PaulMcKenzie@vibster是时候使用调试器逐行检查代码了,在每个步骤中查看变量,看看它在哪里做了一些你没有预料到的事情,你修好了,我@Makogan谢谢!至于所有的输出代码,由于某种原因,我的调试器无法工作,我试图跟踪各种变量。再次感谢!
   #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;
}