繁体   English   中英

以升序合并两个数组(两个数组的大小相同)

[英]Merging two arrays in ascending order (both arrays are same size)

我和我的朋友正在努力合并功能。 它要求两个数组都具有相同的大小,因此合并后的数组是该数组的两倍。 到目前为止,这是我们所拥有的:

void mergeTwoSortedArrays(const int a1[], const int a2[], int mergedArray[], int n) 
{
    int i = 0;
    int j = 0;
    int k = 0;

    while (i <= n && j <= n) 
    {
        if (a1[i] == a2[j]) 
        {
            mergedArray[k] = a1[i];
            mergedArray[k] = a2[j];
            i++;
            j++;
        }
        k++;
    }
}

但是,它不起作用。 有小费吗?

这是用于合并排序还是其他? 通常的方法是进行合并合并,就像您已经完成的一样,然后进行复制。 这是第一部分。 你有点困惑。 通读以下内容,您会发现它很有意义:

while (i < n && j < n) {
    if (a1[i] <= a2[j]) {
        mergedArray[k++] = a1[i++];
    } else {
        mergedArray[k++] = a2[j++];
    }
}

然后处理其余元素。 显然,只有一个数组到达末尾时,循环结束。 因此,现在您需要两个简单得多的循环。 只有一个将执行-无需复杂的测试:

while (i < n) mergedArray[k++] = a1[i++];
while (j < n) mergedArray[k++] = a2[j++];

我将您的测试转换为<而不是<= ,因为数组是从零开始的。

这就是我想出的-

void mergeTwoSortedArrays(const int a1[], const int a2[], int mergedArray[], int n) {

    int i = 0;
    int j = 0;
    int k = 0;

    // Iterate and compare two input arrays until at least one of them reaches to boundary. 
    while (i < n && j < n) {
        if (a1[i] < a2[j]) {
            mergedArray[k++] = a1[i++];
        }
        else if (a1[i] > a2[j]) {
            mergedArray[k++] = a2[j++];
        }
        else if (a1[i] == a2[j]) {
            mergedArray[k++] = a1[i++];
            mergedArray[k++] = a2[j++];
        }
    }

    // Copy the remaining items from the other array, without comparison until, boundary.
    while (i < n) mergedArray[k++] = a1[i++];
    while (j < n) mergedArray[k++] = a2[j++];

}

我不知道您是要像插入它们那样合并它们,还是只是连接两个不同的数组。 如果大步向前:

#include <iostream>

void mergeTwoArraysIntoOne(const int* lhs, const int* rhs, int* dst, size_t numElements) {
    const int* const endLhs = lhs + numElements;
    const int* const endRhs = rhs + numElements;
    for ( ; lhs < endLhs ; ) {
        while (rhs < endRhs && *rhs < *lhs)
            *(dst++) = *(rhs++);
        *(dst++) = *(lhs++);
    }
    while (rhs < endRhs)
        *(dst++) = *(rhs++);
}

void dumpArray(int* array, size_t elements) {
    for (size_t i = 0; i < elements; ++i)
        std::cout << array[i] << " ";
    std::cout << std::endl;
}

int main() {
    int array1[] = { 1, 2, 3 };
    int array2[] = { 10, 20, 30 };
    int array3[] = { 1, 11, 31 };

    int result[6];

    mergeTwoArraysIntoOne(array1, array2, result, 3);
    dumpArray(result, 6);

    mergeTwoArraysIntoOne(array2, array1, result, 3);
    dumpArray(result, 6);

    mergeTwoArraysIntoOne(array1, array3, result, 3);
    dumpArray(result, 6);

    mergeTwoArraysIntoOne(array3, array1, result, 3);
    dumpArray(result, 6);

    mergeTwoArraysIntoOne(array2, array3, result, 3);
    dumpArray(result, 6);

    mergeTwoArraysIntoOne(array3, array2, result, 3);
    dumpArray(result, 6);

    return 0;
}

现场演示: http//ideone.com/7ODqWD

如果只是串联它们:

std::copy(&lhs[0], &lhs[numElements], &dst[0]);
std::copy(&rhs[0], &rhs[numElements], &dst[numElements]);

暂无
暂无

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

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