-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmerge_sort_div3.cpp
36 lines (32 loc) · 937 Bytes
/
merge_sort_div3.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
#include <iostream>
#include <vector>
#include <algorithm>
#include <iterator>
using namespace std;
template<typename RandomIt>
void MergeSort(RandomIt range_begin, RandomIt range_end) {
if (range_end - range_begin < 2) {
return;
}
vector<typename RandomIt::value_type> elements(range_begin,
range_end);
auto t_begin = elements.begin();
auto t_end = elements.end();
auto t_third1 = t_begin + (t_end - t_begin) / 3;
auto t_third2 = t_begin + 2 * (t_end - t_begin) / 3;
MergeSort(t_begin, t_third1);
MergeSort(t_third1, t_third2);
MergeSort(t_third2, t_end);
vector<typename RandomIt::value_type> temp;
merge(t_begin, t_third1, t_third1, t_third2, back_inserter(temp));
merge(temp.begin(), temp.end(), t_third2, t_end, range_begin);
}
int main() {
vector<int> v = {6, 4, 7, 6, 4, 4, 0, 1};
MergeSort(begin(v), end(v));
for (int x : v) {
cout << x << " ";
}
cout << endl;
return 0;
}