-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMergeTwoArrays.c++
More file actions
50 lines (45 loc) · 1.11 KB
/
MergeTwoArrays.c++
File metadata and controls
50 lines (45 loc) · 1.11 KB
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
//Lets make a program that merges two integral arrays and then sorts them in the ascending order.
#include <iostream>
#include <vector>
using namespace std ;
void ArrayPrinter(vector <int> a){
cout<<endl;
for(auto i : a){
cout<<" "<<i;
}
cout<<endl;
}
vector <int> ArrayMerge(vector <int> a1 , vector <int> a2){
vector <int> array;
for(auto i : a1){
array.push_back(i);
}
for(auto i : a2){
array.push_back(i);
}
for(int i = 1 ; i<array.size()-1 ; i++){
int key = array[i]; //Using the insertion sort algorithm
int j = i-1;
while(array[j] > key){
array[j+1] = array[j] ;
j--;
}
array[j+1] = key;
}
return array;
}
int main(){
vector <int> array1 ;
vector <int> array2 ;
array1.push_back(1);
array1.push_back(4);
array1.push_back(6);
array2.push_back(2);
array2.push_back(3);
array2.push_back(6);
ArrayPrinter(array1);
ArrayPrinter(array2);
vector <int> array = ArrayMerge(array1 , array2);
ArrayPrinter(array);
return 0;
}