-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathsorts.hpp
94 lines (85 loc) · 2.23 KB
/
sorts.hpp
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#pragma once
#include <algorithm>
#include <type_traits>
#include <utility>
/**
* TODO: Benchmark more
* Use less comparisons and swaps.
* I use more than necessary right now for simpler code.
*/
namespace ten {
template <typename Iterator, typename Compare>
inline void sort3(Iterator first, Iterator second, Iterator third,
Compare comp) {
using ::std::swap;
using ::std::forward;
if (comp(*second, *first)) {
swap(*first, *second);
}
if (comp(*third, *first)) {
swap(*first, *third);
}
if (comp(*third, *second)) {
swap(*second, *third);
}
}
template <typename Iterator, typename Compare>
inline void lower_median4(Iterator first, Iterator second, Iterator third,
Iterator fourth, Compare comp) {
using ::std::swap;
using ::std::forward;
if (comp(*third, *first)) {
swap(*first, *third);
}
if (comp(*fourth, *second)) {
swap(*second, *fourth);
}
if (comp(*second, *first)) {
swap(*first, *second);
if (comp(*fourth, *second)) {
swap(*second, *fourth);
}
} else if (comp(*third, *second)) {
swap(*second, *third);
}
}
template <typename Iterator, typename Compare>
inline void sort4(Iterator first, Iterator second, Iterator third,
Iterator fourth, Compare comp) {
using ::std::swap;
using ::std::forward;
if (comp(*fourth, *second)) {
swap(*second, *fourth);
}
if (comp(*third, *first)) {
swap(*first, *third);
}
if (comp(*fourth, *third)) {
swap(*third, *fourth);
if (comp(*third, *first)) {
swap(*first, *third);
}
} else if (comp(*third, *second)) {
swap(*second, *third);
}
if (comp(*second, *first)) {
swap(*first, *second);
}
}
template <typename RandomAccessIterator, typename Compare>
inline void selection_sort(RandomAccessIterator first,
RandomAccessIterator last, Compare comp) {
using ::std::min_element;
using ::std::add_lvalue_reference;
using ::std::swap;
auto lm1 = last;
for (--lm1; first != lm1; ++first) {
auto it = min_element<RandomAccessIterator,
typename add_lvalue_reference<Compare>::type>(
first, last, comp);
if (it != first) {
swap(*first, *it);
}
}
}
}