简体   繁体   English

使用 C++ 程序合并排序没有得到正确的输出

[英]Merge Sort using C++ Program not getting correct output

I dont know whats wrong with this code but ive spend way too much time to figure out the problem but still couldnt, I think there is some error with the copying of array because every other thing seems to be correct我不知道这段代码有什么问题,但我花了太多时间来找出问题,但仍然无法解决,我认为复制数组存在一些错误,因为其他所有事情似乎都是正确的

please check this code-请检查此代码-

#include<iostream>
using namespace std;


void MergeArray(int arr[],int lb,int mid,int ub){
    int i=lb;
    int j=mid+1;
    int k=0;
    int newarr[ub-lb+1];

//condition required for comparison between the split parts
    while(i<=mid && j<=ub){
        if(arr[i] < arr[j]){
            newarr[k]=arr[i];
            k++;
            i++;
        }
//basically a[j]<a[i] in this else condition
        else{
            newarr[k]=arr[j];
            k++;
            j++;
        }
    }
//all th left out elements in a[i] when a[j] is finished are added to newarr
    while(i<=mid){
        newarr[k]=arr[i];
        k++;
        i++;
    }
//all th left out elements in a[j] when a[i] is finished are added to newarr
    while(j<=ub){
        newarr[k]=arr[j];
        k++;
        j++;
    }

//copying all the elements of newarr to original arr
//i think this part has something messed up
   for(int i=lb;i<=ub;i=i+1){
        arr[i]= newarr[i];
    }

}


void MergeElements(int arr[], int lb,int ub){
    int mid;
    if(lb<ub){
        mid=(lb+ub)/2;
        //spliting into 2 arts**
        MergeElements(arr,lb,mid);
        MergeElements(arr,mid+1,ub);
        //merging in sorted order**
        MergeArray(arr,lb,mid,ub);
    }

}

int main(){
    int n;
    cout<<"enter the size of the array"<<endl;
    cin>>n;
    int arr[n];
    cout<<"please enter the elements of the array"<<endl;
    for(int i=0;i<n;i++){
        cout<<"enter the element no."<<i<<endl;
        cin>>arr[i];
    }

    MergeElements(arr,0,n-1);

    cout<<"\tSorted Array Elements"<<endl;
    for(int i=0;i<n;i++){
        cout<<arr[i]<<"\t";
    }
return 0;
}

i think i am not able to get the array correctly because everything else seems to be correct according to me please check我想我无法正确获取数组,因为我认为其他所有内容似乎都是正确的,请检查

Well, I'm going to bypass the issue you have with using a variable-length array (VLA) in C++ (for now - VLAs are not allowed in Standard C++ ) and, first, post the solution to your problem.好吧,我会绕过你有问题,使用可变长度阵列(VLA)在C++ (现在-沃拉斯是不是标准允许的C++ ),以及第一,发布解决您的问题。 This is (as you have correctly 'guessed' in your comments) in this loop:这是(正如您在评论中正确“猜测”的那样)在此循环中:

//i think this part has something messed up
   for(int i=lb;i<=ub;i=i+1){
        arr[i]= newarr[i];
    }

Here, although the i index (which starts at the given lower bound, lb ) is correct for the arr array, it is not correct for the newarr array!在这里,尽管i索引(从给定的下限lb )对于arr数组是正确的,但对于newarr数组却是正确的! This is created locally, with a size ub - lb + 1 (correct) but the indexes start at zero - so you need to remove the lb offset for newarr :这是在本地创建的,大小为ub - lb + 1 (正确),但索引从零开始- 因此您需要删除newarrlb偏移量:

   for (i = lb; i <= ub; i++) { // NOTE: You've already declared "int i" - a new one will give a 'hides previous declaration' warning
        arr[i] = newarr[i - lb]; // *** You need to remove the lower-bound offset!
    }

On the issue of VLAs in C++ : I believe GCC/g++ supports these but, if you want to comply with Standard C++ , you should use a std::vector.关于C++的 VLA 问题:我相信GCC/g++支持这些,但是,如果您想遵守标准C++ ,您应该使用 std::vector。 So, in place of:因此,代替:

int newarr[ub - lb + 1];

use:用:

std::vector<int> newarr(size_t(ub - lb + 1));

and similarly use std::vector<int> arr(n);并类似地使用std::vector<int> arr(n); in your main function.在您的main功能中。 For minimal changes to your code, you can still keep your void MergeElements(int arr[], int lb, int ub) signature but, to call it using the std::vector you need to give the address of the first element.为了对代码进行最小的更改,您仍然可以保留void MergeElements(int arr[], int lb, int ub)签名,但是,要使用std::vector调用它,您需要提供第一个元素的地址。 So, in main use this:所以, main使用这个:

MergeElements(&arr[0], 0, n - 1);

Feel free to ask for further clarification and/or explanation.随时要求进一步澄清和/或解释。

Write the loop that copies the array newarr into arr like编写将数组 newarr 复制到 arr 中的循环,例如

//copying all the elements of newarr to original arr
//i think this part has something messed up
   for(int i=lb, k = 0;i<=ub; i++, k++){
        arr[i]= newarr[k];
    }

Pay attention to that variable length arrays is not a standard C++ feature.请注意,可变长度数组不是标准的 C++ 特性。 Instead of the variable length array newarr you can use the standard container std::vector<int> .您可以使用标准容器std::vector<int>代替可变长度数组newarr Also using standard algorithms makes the implementation of the functionMergeArray more simpler.还使用标准算法使 functionMergeArray 的实现更简单。 Here you are这个给你

void MergeArray(int arr[],int lb,int mid,int ub){
    std::vector<int> newarr(ub-lb+1 );

    std::merge( arr + lb, arr + mid + 1, arr + mid + 1, arr + ub + 1, newarr.begin() );
    std::copy( newarr.begin(), newarr.end(), arr + lb );
}

The loop below this comment此评论下方的循环

// copying all the elements of newarr to original arr

uses the same index for the source and destination – and it should not.对源和目标使用相同的索引——它不应该。 The newarr[] content to be copied starts at index 0 , whilst its destination area in arr[] starts from index lb .要复制的newarr[]内容从索引0开始,而其在arr[]目标区域从索引lb开始。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM