C++ Program to Merge 3 Sorted Arrays
Given 3 arrays (A, B, C) which are sorted in ascending order, we are required to merge them together in ascending order and output the array 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]
Method 1 (Two Arrays at a time)
We have discussed at Merging 2 Sorted arrays . So we can first merge two arrays and then merge the resultant with the third array. Time Complexity for merging two arrays O(m+n). So for merging the third array, the time complexity will become O(m+n+o). Note that this is indeed the best time complexity that can be achieved for this problem.
Space Complexity: Since we merge two arrays at a time, we need another array to store the result of the first merge. This raises the space complexity to O(m+n). Note that space required to hold the result of 3 arrays is ignored while calculating complexity.
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)
The Implementations are given below
[1, 2, 3, 5, 6, 7, 8, 9, 10, 11, 12]
Method 2 (Three arrays at a time)
The Space complexity of method 1 can be improved we merge the three arrays together.
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
Complexity: The Time Complexity is O(m+n+o) since we process each element from the three arrays once. We only need one array to store the result of merging and so ignoring this array, the space complexity is O(1).
Implementation of the algorithm is given below:
[1 1 1 2 2 2 41 41 41 52 52 52 67 67 84 85 ]
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 .
Another Approach (Without caring about the exhausting array):
The code written above can be shortened by the below code. Here we do not need to write code if any array gets exhausted.
Below is the implementation of the above approach:
1 1 1 2 2 2 41 41 41 52 52 52 67 67 84 85
Time Complexity: O(m+n+o) where m, n, o are the lengths of the 1st, 2nd, and 3rd arrays.
Please refer complete article on Merge 3 Sorted Arrays for more details!