📜  合并 3 个有序数组的 C++ 程序

📅  最后修改于: 2022-05-13 01:55:51.095000             🧑  作者: Mango

合并 3 个有序数组的 C++ 程序

给定 3 个按升序排序的数组(A、B、C),我们需要将它们按升序合并在一起并输出数组 D。

例子:

Input : A = [1, 2, 3, 4, 5] 
        B = [2, 3, 4]
        C = [4, 5, 6, 7]
Output : D = [1, 2, 2, 3, 3, 4, 4, 4, 5, 5, 6, 7]

Input : A = [1, 2, 3, 5]
        B = [6, 7, 8, 9 ]
        C = [10, 11, 12]
Output: D = [1, 2, 3, 5, 6, 7, 8, 9. 10, 11, 12]

方法1(一次两个数组)
我们在 Merging 2 Sorted arrays 中讨论过。所以我们可以先合并两个数组,然后将结果与第三个数组合并。合并两个数组 O(m+n) 的时间复杂度。因此对于合并第三个数组,时间复杂度将变为 O(m+n+o)。请注意,这确实是该问题可以实现的最佳时间复杂度。
空间复杂性:由于我们一次合并两个数组,我们需要另一个数组来存储第一次合并的结果。这将空间复杂度提高到 O(m+n)。请注意,在计算复杂度时,会忽略保存 3 个数组的结果所需的空间。

算法

function merge(A, B)
    Let m and n be the sizes of A and B
    Let D be the array to store result
   
    // Merge by taking smaller element from A and B
    while i < m and j < n
        if A[i] <= B[j]
            Add A[i] to D and increment i by 1
        else Add B[j] to D and increment j by 1

    // If array A has exhausted, put elements from B
    while j < n
        Add B[j] to D and increment j by 1
   
    // If array B has exhausted, put elements from A
    while i < n
        Add A[j] to D and increment i by 1
   
    Return D

function merge_three(A, B, C)
    T = merge(A, B)
    return merge(T, C)

下面给出了实现

C++
// C++ program to merge three sorted arrays
// by merging two at a time.
#include 
#include 
using namespace std;
  
using Vector = vector;
  
void printVector(const Vector& a)
{
    cout << "[";
    for (auto e : a)
        cout << e << " ";
    cout << "]" << endl;
}
  
Vector mergeTwo(Vector& A, Vector& B)
{
    // Get sizes of vectors
    int m = A.size();
    int n = B.size();
  
    // Vector for storing Result
    Vector D;
    D.reserve(m + n);
  
    int i = 0, j = 0;
    while (i < m && j < n) {
  
        if (A[i] <= B[j])
            D.push_back(A[i++]);
        else
            D.push_back(B[j++]);
    }
  
    // B has exhausted
    while (i < m)
        D.push_back(A[i++]);
  
    // A has exhausted
    while (j < n)
        D.push_back(B[j++]);
  
    return D;
}
  
// Driver Code
int main()
{
    Vector A = { 1, 2, 3, 5 };
    Vector B = { 6, 7, 8, 9 };
    Vector C = { 10, 11, 12 };
  
    // First Merge A and B
    Vector T = mergeTwo(A, B);
  
    // Print Result after merging T with C
    printVector(mergeTwo(T, C));
    return 0;
}


C++
// C++ program to merger three sorted arrays
// by merging three simultaneously.
#include 
#include 
using namespace std;
  
using Vector = vector;
  
void printVector(const Vector& a)
{
    cout << "[";
    for (auto e : a) {
        cout << e << " ";
    }
    cout << "]" << endl;
}
  
Vector mergeThree(Vector& A, Vector& B,
                  Vector& C)
{
    int m, n, o, i, j, k;
    // Get Sizes of three vectors
    m = A.size();
    n = B.size();
    o = C.size();
  
    // Vector for storing output
    Vector D;
    D.reserve(m + n + o);
  
    i = j = k = 0;
  
    while (i < m && j < n && k < o) {
  
        // Get minimum of a, b, c
        int m = min(min(A[i], B[j]), C[k]);
  
        // Put m in D
        D.push_back(m);
  
        // Increment i, j, k
        if (m == A[i])
            i++;
        else if (m == B[j])
            j++;
        else
            k++;
    }
  
    // C has exhausted
    while (i < m && j < n) {
        if (A[i] <= B[j]) {
            D.push_back(A[i]);
            i++;
        }
        else {
            D.push_back(B[j]);
            j++;
        }
    }
  
    // B has exhausted
    while (i < m && k < o) {
        if (A[i] <= C[k]) {
            D.push_back(A[i]);
            i++;
        }
        else {
            D.push_back(C[k]);
            k++;
        }
    }
  
    // A has exhausted
    while (j < n && k < o) {
        if (B[j] <= C[k]) {
            D.push_back(B[j]);
            j++;
        }
        else {
            D.push_back(C[k]);
            k++;
        }
    }
  
    // A and B have exhausted
    while (k < o)
        D.push_back(C[k++]);
  
    // B and C have exhausted
    while (i < m)
        D.push_back(A[i++]);
  
    // A and C have exhausted
    while (j < n)
        D.push_back(B[j++]);
  
    return D;
}
  
// Driver Code
int main()
{
    Vector A = { 1, 2, 41, 52, 84 };
    Vector B = { 1, 2, 41, 52, 67 };
    Vector C = { 1, 2, 41, 52, 67, 85 };
  
    // Print Result
    printVector(mergeThree(A, B, C));
    return 0;
}


输出:

[1, 2, 3, 5, 6, 7, 8, 9, 10, 11, 12]

方法2(一次三个数组)
方法1的空间复杂度可以提高,我们将三个数组合并在一起。

function merge-three(A, B, C)
    Let m, n, o be size of A, B, and C
    Let D be the array to store the result
    
    // Merge three arrays at the same time
    while i < m and j < n and k < o
        Get minimum of A[i], B[j], C[i]
        if the minimum is from A, add it to 
           D and advance i
        else if the minimum is from B add it 
                to D and advance j
        else if the minimum is from C add it 
                to D and advance k
    
   // After above step at least 1 array has 
   // exhausted. Only C has exhausted
   while i < m and j < n
       put minimum of A[i] and B[j] into D
       Advance i if minimum is from A else advance j 
   
   // Only B has exhausted
   while i < m and k < o
       Put minimum of A[i] and C[k] into D
       Advance i if minimum is from A else advance k
 
   // Only A has exhausted
   while j < n and k < o
       Put minimum of B[j] and C[k] into D
       Advance j if minimum is from B else advance k

   // After above steps at least 2 arrays have 
   // exhausted
   if A and B have exhausted take elements from C
   if B and C have exhausted take elements from A
   if A and C have exhausted take elements from B
   
   return D

复杂度:时间复杂度为 O(m+n+o),因为我们处理了三个数组中的每个元素一次。我们只需要一个数组来存储合并的结果,所以忽略这个数组,空间复杂度是 O(1)。

该算法的实现如下:

C++

// C++ program to merger three sorted arrays
// by merging three simultaneously.
#include 
#include 
using namespace std;
  
using Vector = vector;
  
void printVector(const Vector& a)
{
    cout << "[";
    for (auto e : a) {
        cout << e << " ";
    }
    cout << "]" << endl;
}
  
Vector mergeThree(Vector& A, Vector& B,
                  Vector& C)
{
    int m, n, o, i, j, k;
    // Get Sizes of three vectors
    m = A.size();
    n = B.size();
    o = C.size();
  
    // Vector for storing output
    Vector D;
    D.reserve(m + n + o);
  
    i = j = k = 0;
  
    while (i < m && j < n && k < o) {
  
        // Get minimum of a, b, c
        int m = min(min(A[i], B[j]), C[k]);
  
        // Put m in D
        D.push_back(m);
  
        // Increment i, j, k
        if (m == A[i])
            i++;
        else if (m == B[j])
            j++;
        else
            k++;
    }
  
    // C has exhausted
    while (i < m && j < n) {
        if (A[i] <= B[j]) {
            D.push_back(A[i]);
            i++;
        }
        else {
            D.push_back(B[j]);
            j++;
        }
    }
  
    // B has exhausted
    while (i < m && k < o) {
        if (A[i] <= C[k]) {
            D.push_back(A[i]);
            i++;
        }
        else {
            D.push_back(C[k]);
            k++;
        }
    }
  
    // A has exhausted
    while (j < n && k < o) {
        if (B[j] <= C[k]) {
            D.push_back(B[j]);
            j++;
        }
        else {
            D.push_back(C[k]);
            k++;
        }
    }
  
    // A and B have exhausted
    while (k < o)
        D.push_back(C[k++]);
  
    // B and C have exhausted
    while (i < m)
        D.push_back(A[i++]);
  
    // A and C have exhausted
    while (j < n)
        D.push_back(B[j++]);
  
    return D;
}
  
// Driver Code
int main()
{
    Vector A = { 1, 2, 41, 52, 84 };
    Vector B = { 1, 2, 41, 52, 67 };
    Vector C = { 1, 2, 41, 52, 67, 85 };
  
    // Print Result
    printVector(mergeThree(A, B, C));
    return 0;
}
输出
[1 1 1 2 2 2 41 41 41 52 52 52 67 67 84 85 ]

注意:虽然实现合并两个或三个数组的直接过程相对容易,但如果我们要合并 4 个或更多数组,该过程会变得很麻烦。在这种情况下,我们应该遵循 Merge K Sorted Arrays 中显示的过程。

有关更多详细信息,请参阅有关 Merge 3 Sorted Arrays 的完整文章!