简体   繁体   English

使用for循环合并两个排序的数组

[英]Merging two sorted arrays with for loop

I have a function that merges two sorted arrays into one and returns a pointer to it. 我有一个函数,将两个排序数组合并为一个,并返回指向它的指针。 I want to use a for loop rather than a while. 我想使用一个for循环,而不是一会儿。 However in some test cases the last 1 or 2 elements of the merge array are not in their place. 但是,在某些测试用例中,合并数组的最后1个或2个元素不在原处。 I would appreciate if someone can help solve this problem keeping the for loop. 如果有人可以帮助解决此问题并保持for循环,我将不胜感激。

int * mergeSort(int arr1[], int arr2[],int len)
{

  /* len is the combined length of the two arrays */

    static int sorted[100];

    int pos1=0, pos2=0;

    for (int i=0; i<len; i++)
    {
        if (arr1[pos1]<=arr2[pos2])
        {
            sorted[i]=arr1[pos1];
            pos1++;
        }
        else
        {
            sorted[i]=arr2[pos2];
            pos2++;
        }
    }

    return sorted;
}

Your problem is that you don't seem to handle going past the end of the input arrays. 您的问题是您似乎无法处理超出输入数组末尾的情况。 If there is uninitialized memory - you get undefined behaviour. 如果存在未初始化的内存-您将获得未定义的行为。

You can avoid this by terminating your arrays with a sentinel value, for example INT_MAX , which should always be bigger than all possible values in the arrays: 您可以通过用定点值终止数组来避免这种情况,例如INT_MAX ,该值应始终大于数组中所有可能的值:

int a[] = { 1, 2, 104, INT_MAX};
int b[] = { 101, 102, 105, INT_MAX};

int* ptr = mergeSort(a,b,6);

for(int i = 0; i < 6; i++){
    cout << i << " " << ptr[i] << endl;
}

live demo 现场演示

Or you can pass the actual lengths of both arrays and handle them correctly: 或者,您可以传递两个数组的实际长度并正确处理它们:

int * mergeSort(int arr1[], int len1, int arr2[],int len2)
{

  /* len is the combined length of the two arrays */

    static int sorted[100];

    int pos1=0, pos2=0;

    for (int i=0; i< len1 + len2; i++)
    {
        if ((pos2 == len2) || (arr1[pos1] <= arr2[pos2] && (pos1 < len1)))
        {
            sorted[i]=arr1[pos1];
            pos1++;
        }
        else
        {
            sorted[i]=arr2[pos2];
            pos2++;
        }
    }

    return sorted;
}

live demo 现场演示

This doesn't answer the question of what's wrong with your code, but to answer the question of how to merge two sorted ranges I would suggest std::merge : 这不会回答您的代码有什么问题,但是为了回答如何合并两个排序范围的问题,我建议使用std::merge

    int * mergeSort(int arr1[], int arr2[], int len1, int len2)
    {
        //I am not condoning the use of a static buffer here,
        //I would probably use a std::vector or std::array,
        //possibly a boost::static_vector if really necessary
        static int sorted[100];
        std::merge(arr1, arr1 + len1, arr2, arr2 + len2, sorted);
        return sorted;
    }

I also changed int len to int len1, int len2 because you need to know the lengths of the individual arrays, not just their combined length, to avoid reading past the end of the input arrays. 我还将int len更改为int len1, int len2因为您需要知道各个数组的长度,而不仅仅是它们的组合长度,以避免读取输入数组的末尾。

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

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