C++ Program to Merge Two Sorted Arrays
Given two sorted arrays, the task is to merge them in a sorted manner.
Examples:
Input: arr1[] = { 1, 3, 4, 5}, arr2[] = {2, 4, 6, 8}
Output: arr3[] = {1, 2, 3, 4, 4, 5, 6, 8}
Input: arr1[] = { 5, 8, 9}, arr2[] = {4, 7, 8}
Output: arr3[] = {4, 5, 7, 8, 8, 9}
Naive Approach:
It is the brute force method to do the same. Take all the elements of arr1 and arr2 in arr3. Then simply sort the arr3.
The implementation of above approach is:
C++
#include<bits/stdc++.h>
using namespace std;
void mergeArrays( int a[], int b[], int n, int m)
{
map< int , int > mp;
for ( int i = 0; i < n; i++)mp[a[i]]++;
for ( int i = 0;i < m;i++)mp[b[i]]++;
for ( auto j: mp)
{
for ( int i=0; i<j.second;i++)cout<<j.first<< " " ;
}
}
int main()
{
int a[] = {1, 3, 5, 7}, b[] = {2, 4, 6, 8};
int size = sizeof (a)/ sizeof ( int );
int size1 = sizeof (b)/ sizeof ( int );
mergeArrays(a, b, size, size1);
return 0;
}
|
Output
Array after merging
1 2 3 4 5 6 7 8
Time Complexity : O((m+n) log(m+n)) , the whole size of arr3 is m+n
Auxiliary Space: O(1), No extra space is used
Method 2 (O(n1 * n2) Time and O(n1+n2) Extra Space)
- Create an array arr3[] of size n1 + n2.
- Copy all n1 elements of arr1[] to arr3[]
- Traverse arr2[] and one by one insert elements (like insertion sort) of arr3[] to arr1[]. This step take O(n1 * n2) time.
We have discussed implementation of above method in Merge two sorted arrays with O(1) extra space
Method 3 (O(n1 + n2) Time and O(n1 + n2) Extra Space)
The idea is to use Merge function of Merge sort.
- Create an array arr3[] of size n1 + n2.
- Simultaneously traverse arr1[] and arr2[].
- Pick smaller of current elements in arr1[] and arr2[], copy this smaller element to next position in arr3[] and move ahead in arr3[] and the array whose element is picked.
- If there are remaining elements in arr1[] or arr2[], copy them also in arr3[].
Below image is a dry run of the above approach:
Below is the implementation of the above approach:
Output
Array after merging
1 2 3 4 5 6 7 8
Output:
Array after merging
1 2 3 4 5 6 7 8
Time Complexity : O(n1 + n2)
Auxiliary Space : O(n1 + n2)
Method 4: Using Maps (O(nlog(n) + mlog(m)) Time and O(N) Extra Space)
- Insert elements of both arrays in a map as keys.
- Print the keys of the map.
Below is the implementation of above approach.
CPP
#include<bits/stdc++.h>
using namespace std;
void mergeArrays( int a[], int b[], int n, int m)
{
map< int , int > mp;
for ( int i = 0; i < n; i++)mp[a[i]]++;
for ( int i = 0;i < m;i++)mp[b[i]]++;
for ( auto j: mp)
{
for ( int i=0; i<j.second;i++)cout<<j.first<< " " ;
}
}
int main()
{
int a[] = {1, 3, 5, 7}, b[] = {2, 4, 6, 8};
int size = sizeof (a)/ sizeof ( int );
int size1 = sizeof (b)/ sizeof ( int );
mergeArrays(a, b, size, size1);
return 0;
}
|
Time Complexity: O( nlog(n) + mlog(m) )
Auxiliary Space: O(N)
Brocade,Goldman-Sachs,Juniper,Linkedin,Microsoft,Quikr,Snapdeal,Synopsys,Zoho
Related Articles :
Merge two sorted arrays with O(1) extra space
Merge k sorted arrays | Set 1
Last Updated :
19 Sep, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...