-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmerge array.cpp
More file actions
46 lines (42 loc) · 936 Bytes
/
Copy pathmerge array.cpp
File metadata and controls
46 lines (42 loc) · 936 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
//merge two sorted arrays
#include<iostream>
using namespace std;
void mergesort(int a[15],int b[15],int n,int m, int &k);
int main()
{
int size1,size2, array1[15],array2[15];
int k=0;
cout<<"\nEnter the size of first array :";
cin>>size1;
cout<<"\nEnter elements of first array in asc order :"<<endl;
for(int i=0;i<size1;i++)
cin>>array1[i];
cout<<"\nEnter size of 2nd array : ";
cin>>size2;
cout<<"\nEnter elements of array 2 :"<<endl;
for(int j=0;j<size2;j++)
cin>>array2[j];
mergesort(array1,array2,size1,size2,k);
return 0;
}
void mergesort(int a[15],int b[15], int n , int m,int &k){
int sarr[25];
int i=0,j=0,l=0;
while(i<n && j<m){
if(a[i]<b[j])
sarr[k++]=a[i++];
else
sarr[k++]=b[j++];
}
while(i<n)
{
sarr[k++]=a[i++];
}
while(j<m)
{
sarr[k++]=b[j++];
}
cout<<"\nMerged array is:"<<endl;
for(int u=0;u<k;u++)
cout<<sarr[u]<<" ";
}