-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path20_MergeSortedArray.cpp
More file actions
58 lines (46 loc) · 873 Bytes
/
Copy path20_MergeSortedArray.cpp
File metadata and controls
58 lines (46 loc) · 873 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
#include <iostream>
using namespace std;
void printArray(int arr[], int n){
for(int i=0; i<n; i++){
cout<<arr[i]<<" ";
}
}
void mergeArray(int arr1[], int n, int arr2[], int m, int arr3[]){
int i=0; int j=0;
int k=0;
while (i<n && j<m)
{
if (arr1[i]<arr2[j])
{
arr3[k]=arr1[i];
i++;
k++;
}
else{
arr3[k]=arr2[j];
j++;
k++;
}
}
while (i<n)
{
arr3[k]=arr1[i];
k++;
i++;
}
while (j<m)
{
arr3[k]=arr2[j];
k++;
j++;
}
}
int main(){
int arr1[5]={1,3,5,7,9};
int arr2[3]={2,4,6};
int arr3[8]={0};
// printArray( arr1, 5);
mergeArray(arr1, 5, arr2, 3, arr3);
printArray(arr3, 8);
return 0;
}