-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmerge2arr.cpp
More file actions
52 lines (49 loc) · 916 Bytes
/
Copy pathmerge2arr.cpp
File metadata and controls
52 lines (49 loc) · 916 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
#include <iostream>
using namespace std;
void MergeSort(int *arr1, int *arr2, int *arr3, int m, int n)
{
int i = 0, j = 0, k = 0;
while (i < m && j < n)
{
if (arr1[i] < arr2[j])
{
arr3[k] = arr1[i];
i++;
}
else
{
arr3[k] = arr2[j];
j++;
}
k++;
}
while (k < m + n)
{
if(i < m){
arr3[k] = arr1[i];
i++;
}
else{
arr3[k] = arr2[j];
j++;
}
k++;
}
}
void display(int *arr, int size)
{
for (size_t i = 0; i < size; i++)
{
cout << arr[i] << " ";
}
}
int main(int argc, char const *argv[])
{
int arr1[5] = {2, 8,11, 15, 18};
int m = 5, n = 4;
int arr2[4] = {5, 9, 12, 17};
int arr3[9];
MergeSort(arr1, arr2, arr3, m, n);
display(arr3,9);
return 0;
}