-
Notifications
You must be signed in to change notification settings - Fork 0
/
mergetwosortedarry.cpp
64 lines (60 loc) · 1.34 KB
/
mergetwosortedarry.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
63
64
//This program contains qno 14:-
#include<iostream>
using namespace std;
void merge(int a[],int b[],int size1,int size2)
{
int newarr[size1+size2];
int i=0,j=0,k=0;
while(i<size1&&j<size2)
{
if(a[i]<b[j])
{
newarr[k]=a[i];
k++;
i++;
}
else
{
newarr[k]=b[j];
k++;
j++;
}
}
while(i<size1)
{
newarr[k]=a[i];
i++;
k++;
}
while(j<size2)
{
newarr[k]=b[j];
j++;
k++;
}
cout<<"New array is"<<endl;
for(int i=0;i<(size1+size2);i++)
{
cout<<newarr[i]<<" ";
}
}
int main()
{
int a[100];
int b[100],size1,size2;
cout<<"enter the size of first array "<<endl;
cin>>size1;
cout<<"enter the elements of first array in sorted order"<<endl;
for(int i=0;i<size1;i++)
{
cin>>a[i];
}
cout<<"enter the size of second array "<<endl;
cin>>size2;
cout<<"enter the elements of second array in sorted order"<<endl;
for(int i=0;i<size2;i++)
{
cin>>b[i];
}
merge(a,b,size1,size2);
}