-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathMergeTwoSortedArrays.cpp
47 lines (42 loc) · 985 Bytes
/
MergeTwoSortedArrays.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
#include <iostream>
#include <vector>
using namespace std;
vector <int> MergeTwoSortedArrays(vector<int> Array1, vector<int> Array2){
vector <int> MergedSortedArray;
int i=0;
int j=0;
//Check Input
if (Array1.size()==0){
return Array2;
}
if (Array2.size()==0){
return Array1;
}
while (i<Array1.size() && j<Array2.size()){
if (Array1[i] < Array2[j]){
MergedSortedArray.push_back(Array1[i]);
i++;
}
else{
MergedSortedArray.push_back(Array2[j]);
j++;
}
}
while (i<Array1.size()){
MergedSortedArray.push_back(Array1[i]);
i++;
}
while (j<Array2.size()){
MergedSortedArray.push_back(Array2[j]);
j++;
}
return MergedSortedArray;
}
int main() {
vector<int> Array1 = {1,3,7,9,15,92,100};
vector<int> Array2 = {2,4,6,8,10,11};
vector<int> MergedSortedArray = MergeTwoSortedArrays(Array1,Array2);
for (int i=0;i<MergedSortedArray.size();i++){
cout<<MergedSortedArray.at(i)<<",";
}
}