-
Notifications
You must be signed in to change notification settings - Fork 0
/
merge_two_sorted_array.cpp
62 lines (58 loc) · 1003 Bytes
/
merge_two_sorted_array.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#include<iostream>
using namespace std;
void merge(int a[],int m,int b[],int n,int c[])
{
int i=0,j=0,k=0;
while(i<m+n && j<m && k<n)
{
if(a[j]<=b[k])
{
c[i]=a[j];
j++;
}
else
{
c[i]=b[k];
k++;
}
i++;
}
while(i<m+n && j<m)
{
c[i]=a[j];
j++;
i++;
}
while(i<m+n && k<n)
{
c[i]=b[k];
k++;
i++;
}
}
int main()
{
int a[100],b[100],c[100];
int m ,n;
cout <<"Enter The Length of 1st Sorted Array : ";
cin >> m ;
cout <<"Enter The Length of 2nd Sorted Array : ";
cin >>n;
cout <<"1st Array : ";
for(int i=0;i<m;i++)
{
cin >>a[i];
}
cout <<"2nd Array : ";
for(int i=0;i<n;i++)
{
cin >>b[i];
}
merge(a,m,b,n,c);
cout <<"The new Array after merging is : ";
for(int i=0;i<m+n;i++)
{
cout <<c[i] <<" ";
}
cout <<endl;
}