-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmerge2sortedarray.cpp
More file actions
61 lines (51 loc) · 947 Bytes
/
Copy pathmerge2sortedarray.cpp
File metadata and controls
61 lines (51 loc) · 947 Bytes
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
#include<iostream>
using namespace std;
void merge(int arr1[],int arr2[],int arr3[],int m,int n){
int i=0;
int j=0;
int k=0;
while(i<m && j<n)
{
if(arr1[i]<arr2[j])
{
arr3[k++]=arr1[i++];
}
else{
arr3[k++]=arr2[j++];
}
}
while(i<m)
{
arr3[k++]=arr1[i++];
}
while(j<n)
{
arr3[k++]=arr2[j++];
}
}
int main(){
int m;
cout<<"enter size of array1";
cin>>m;
int arr1[m];
cout<<"enter array elements for array 1";
for(int i=0;i<m;i++)
{
cin>>arr1[i];
}
int n;
cout<<"enter size of array2";
cin>>n;
int arr2[n];
cout<<"enter array elements for array 2";
for(int i=0;i<n;i++)
{
cin>>arr2[i];
}
int arr3[m+n];
merge(arr1,arr2,arr3,m,n);
for(int i=0;i<m+n;i++)
{
cout<<arr3[i]<<" ";
}
}