-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinPlaceMergeSort.cpp
More file actions
54 lines (47 loc) · 923 Bytes
/
inPlaceMergeSort.cpp
File metadata and controls
54 lines (47 loc) · 923 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
#include <iostream>
#include <vector>
void inPlaceMergeSort(std::vector<int>& x, std::vector<int>& y)
{
// Pick the larger array
size_t idx_x = 0;
while (idx_x < x.size())
{
if (x[idx_x] > y[0])
{
std::swap(x[idx_x], y[0]);
// Swap new element with first element
// and move it to the right position
size_t idx_y = 1;
while (idx_y < y.size())
{
if (y[0] > y[idx_y])
std::swap(y[0], y[idx_y]);
++idx_y;
}
}
++idx_x;
}
}
void print(const std::vector<int>& arr)
{
for (auto iter : arr)
{
std::cout << iter << " ";
}
std::cout << std::endl;
}
int main()
{
std::vector<int> x{1, 4, 7, 8, 10};
std::vector<int> y{ 2, 3, 9 };
std::cout << "x is: " << std::endl;
print(x);
std::cout << "y is: " << std::endl;
print(y);
inPlaceMergeSort(x, y);
std::cout << "x is: " << std::endl;
print(x);
std::cout << "y is: " << std::endl;
print(y);
return 0;
}