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

时间:2013-11-01 01:14:06

标签: c++ arrays merge

我和我的朋友正在努力解决合并问题。它要求两个数组具有相同的大小,因此合并的数组是数字的两倍。这是我们到目前为止所做的:

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++;
    }
}
然而,它不起作用。有什么提示吗?

3 个答案:

答案 0 :(得分:4)

这是合并排序还是其他什么?通常的方法是进行组合合并,就像您已经完成的那样,然后是副本。这是第一部分。你有点困惑。仔细阅读,你会发现它是有道理的:

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++];

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

答案 1 :(得分:0)

这就是我提出的 -

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++];

}

答案 2 :(得分:0)

我不知道你是否试图将它们合并为交错它们或者只是连接两个不同的数组。如果它正在迈进:

#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]);