-
Notifications
You must be signed in to change notification settings - Fork 33
/
Mergesortedarray.cpp
84 lines (66 loc) · 1.48 KB
/
Mergesortedarray.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#include<bits/stdc++.h>
using namespace std;
void merge(int arr1[],int n,int arr2[],int m,int arr3[]){
int t=n+m;
int i=0,j=0,k=0;
while(i<n && j<m){
if(arr1[i]<arr2[j]){
arr3[k++]=arr1[i++];
// k++;
// i++;
}
else{
arr3[k++]=arr2[j++];
// k++;
// j++;
}
}
// agar ek array avi vi bacha ho to uska value arr3 mai copy kar do
while(i<n){
arr3[k++]=arr1[i++];
// k++;
// i++;
}
while(j<m){
arr3[k++]=arr2[j++];
// k++;
// j++;
}
sort(arr3,arr3+t);
}
void print(int ans[],int t){
cout<<"The sorted merged array : "<<endl;
for(int i=0;i<t;i++){
cout<<ans[i]<<" ";
}
cout<<endl;
}
int main()
{
int n,m;
cout<<"Enter the size of arr1 and arr2 : "<<endl;
cin>>n>>m;
int t=n+m;
int arr1[n],arr2[m];
cout<<"Enter arr1 and arr2 : \n";
for(int i=0; i<n; i++){
cin>>arr1[i];
}
for(int j=0;j<m;j++){
cin>>arr2[j];
}
// int m;
// cout<<"Enter the size of arr2 : "<<endl;
// cin>>m;
// int arr2[100];
// cout<<"Enter arr2 : \n";
// for(int i=0; i<m; i++){
// cin>>arr2[i];
// }
// int arr1[5]={1,3,5,7,9};
// int arr2[3]={2,4,6};
int arr3[t]={0};
merge(arr1,n,arr2,m,arr3);
print(arr3,t);
return 0;
}