VOOZH about

URL: https://www.geeksforgeeks.org/dsa/merge-3-sorted-arrays/

⇱ Merge 3 Sorted Arrays - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Merge 3 Sorted Arrays

Last Updated : 19 Jul, 2024

Given 3 arrays A[], B[], and C[] that are sorted in ascending order, the task is to merge them together in ascending order and output the array D[]. 

Examples: 

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]

Naive Approach:

The naive approach to merge three sorted arrays would be to first concatenate all three arrays into a single array and then sort it in ascending order. This approach has a time complexity of O((n+m+p) log(n+m+p)), where n, m, and p are the lengths of the three arrays.

Algorithm:

  1.    Take the size of the arrays A, B, and C as input from the user.
  2.    Create arrays A, B, and C of the input size.
  3.    Take the elements of arrays A, B, and C as input from the user.
  4.    Merge arrays A, B, and C into a single array D.
  5.    Sort array D in ascending order.
  6.    Print the elements of array D.

Below is the implementation of the approach:


Output
1 2 3 5 6 7 8 9 10 11 12 

Time Complexity: O( (n+m+p) * log(n+m+p) ), where n, m, and p are the lengths of the three arrays.
Space Complexity: O(n+m+p), where n, m, and p are the lengths of the three arrays. This is because vector D has been created of size (m+n+p). 

Merge 3 Sorted Arrays by merging Two Arrays at a time:

The idea is to first merge two arrays and then merge the resultant with the third array.

Follow the steps below to solve the problem:

  • First, merge the arr1 and arr2 using the idea mentioned in Merge Two Sorted Arrays
  • Now merge the resultant array (from arr1 and arr2) with arr3.
  • Print the answer array.

Pseudocode:

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)

Below is the implementation of the above approach:


Output
1 2 3 5 6 7 8 9 10 11 12 

Time Complexity: O(M+N+O) where m, n, o are the lengths of the 1st, 2nd, 3rd Array.
Auxiliary Space: O(M+N+O).

Merge 3 Sorted Arrays by merging Three arrays at a time:

The idea is to merge three arrays at the same time just like two arrays.

Follow the steps below to solve the problem:

  • Initialize three pointers i, j, and k which are associated with arr1, arr2, and arr3 respectively
  • Now check the smallest number from the i, j, and k indexes.
    • Put that minimum value in the output array 
    • Increment the pointer which has minimum value by 1
    • Perform these steps till any of the indexes reach the end of the array.
  • After that check which two arrays don't reach the end and perform the same with those two arrays.
  • At last, check which of the three arrays doesn't reach the end and put all the remaining elements of that array into the output array.

Pseudocode:

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

Below is the implementation of the above approach:


Output
1 2 3 5 6 7 8 9 10 11 12 

Time Complexity: O(M+N+O), Traversing over all the three arrays of size M, N, and O
Auxiliary Space: O(M+N+O), Space used for the output array

Note: While it is relatively easy to implement direct procedures to merge two or three arrays, the process becomes cumbersome if we want to merge 4 or more arrays. In such cases, we should follow the procedure shown in Merge K Sorted Arrays .

Optimization of the above approach:

The code written above can be shortened by the below code. Here we do not need to write code if any array gets exhausted. If any of the arrays get exhausted then store the large value(INT_MAX) in the variable which represents the value of that array. When all the pointers reach the end of their respective array return the resultant array.

Below is the implementation of the above approach:


Output
1 2 3 5 6 7 8 9 10 11 12 

Time Complexity: O(l1+l2+l3), Traversing over all the three arrays of size l1, l2,and l3
Auxiliary Space:  O(l1+l2+l3),  Space used for the output array

Comment
Article Tags: