-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmergeSorted.cpp
43 lines (41 loc) · 867 Bytes
/
mergeSorted.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
#include<iostream>
#include<bits/stdc++.h>
using namespace std;
void mergeArr(int arr[],int n,int arr1[],int m,int ans[]){
int index1=0,index2 =0;
int k = 0;
while(index1<n && index2<m){
if(arr[index1]<arr1[index2]){
ans[k] = arr[index1];
k++;
index1++;
}else if(arr[index1]>arr1[index2]){
ans[k]=arr1[index2];
index2++;
k++;
}
}
while(index1<n){
ans[k] = arr[index1];
index1++;
k++;
}
while(index2<m){
ans[k] = arr1[index2];
index2++;
k++;
}
}
int print(int ans[]){
for(int i = 0;i<9;i++){
cout<<ans[i]<<" ";
}cout<<endl;
}
int main(){
int arr[5] = {1,3,5,7,9};
int arr1[4]={2,4,6,8};
int ans[9];
mergeArr(arr,5,arr1,4,ans);
print(ans);
return 0;
}