-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmerge_sort.h
63 lines (59 loc) · 1.98 KB
/
merge_sort.h
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
55
56
57
58
59
60
61
62
63
#include <iterator>
#include <algorithm>
template<typename RandomIt1, typename RandomIt2, typename Compare>
int merge(RandomIt1 first, RandomIt1 middle, RandomIt1 last, RandomIt2 buf,
Compare comp)
{
RandomIt1 left = first, right = middle;
int inversions = 0;
while (left != middle && right != last) {
if (comp(*right, *left)) {
inversions += middle - left;
*buf = *right;
++right;
} else {
*buf = *left;
++left;
}
++buf;
}
std::copy(left, middle, buf);
std::copy(right, last, buf);
return inversions;
}
template<typename RandomIt1, typename RandomIt2, typename Compare>
int merge_sort(RandomIt1 src_first, RandomIt1 src_last,
RandomIt2 dst_first, RandomIt2 dst_last, Compare comp)
{
int length = src_last - src_first;
if (length <= 1) return 0;
RandomIt1 src_middle = src_first + (length / 2);
RandomIt2 dst_middle = dst_first + (length / 2);
return merge_sort(dst_first, dst_middle, src_first, src_middle, comp)
+ merge_sort(dst_middle, dst_last, src_middle, src_last, comp)
+ merge(src_first, src_middle, src_last, dst_first, comp);
}
template<typename RandomIt>
int merge_sort(RandomIt first, RandomIt last)
{
int length = last - first;
if (length <= 1) return 0;
using T = typename std::iterator_traits<RandomIt>::value_type;
T* buf = new T[length];
std::copy(first, last, buf);
int inversions = merge_sort(buf, buf + length, first, last, std::less<T>());
delete[] buf;
return inversions;
}
template<typename RandomIt, typename Compare>
int merge_sort(RandomIt first, RandomIt last, Compare comp)
{
int length = last - first;
if (length <= 1) return 0;
using T = typename std::iterator_traits<RandomIt>::value_type;
T* buf = new T[length];
std::copy(first, last, buf);
int inversions = merge_sort(buf, buf + length, first, last, comp);
delete[] buf;
return inversions;
}