![]() |
VOOZH | about |
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]
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:
Below is the implementation of the approach:
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).
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:
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 Dfunction merge_three(A, B, C)
T = merge(A, B)
return merge(T, C)
Below is the implementation of the above approach:
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).
The idea is to merge three arrays at the same time just like two arrays.
Follow the steps below to solve the problem:
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:
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 .
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:
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