-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMHA.h
More file actions
198 lines (153 loc) · 6.11 KB
/
MHA.h
File metadata and controls
198 lines (153 loc) · 6.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
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
// ======================================================================
//
//Copyright (C) 2018 Mehedi Hasan, MSc. Student @ OVGU Magdeburg,
// email: md.hasan@st.ovgu.de
// This software and related files are part of an Algorithm Engineering
// implementation project on adaptive sorting.
//This program is free software: you can redistribute it and/or modify
//it under the terms of the GNU General Public License as published by
//the Free Software Foundation, either version 3 of the License, or
//(at your option) any later version.
//This program is distributed in the hope that it will be useful,
//but WITHOUT ANY WARRANTY; without even the implied warranty of
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
//GNU General Public License for more details.
//You should have received a copy of the GNU General Public License
//along with this program. If not, see <http://www.gnu.org/licenses/>.
//
// ======================================================================
#include <algorithm>
#include <iterator>
#include <list>
#include <cmath>
namespace MHA {
template<class RandomAccessIterator, class Less = std::less<int>>
void a_merge_sort(RandomAccessIterator first, RandomAccessIterator beyond, Less less = {})
{
const auto n = std::distance(first, beyond);
if (!std::is_sorted(first, beyond, less)){
if (n > 1) {
const auto middle = std::next(first, n / 2);
a_merge_sort(first, middle, less);
a_merge_sort(middle, beyond, less);
std::inplace_merge(first, middle, beyond, less);
}
}
}
template<class RandomAccessIterator, class Less=std::less<int>>
void hybrid_sort(RandomAccessIterator first, RandomAccessIterator beyond, Less less = {})
{
auto const n = std::distance(first, beyond);
if (n < 128) {
a_merge_sort(std::move(first), std::move(beyond), std::move(less));
return;
}
auto const limit = n / floor(std::log2(n));
std::list<RandomAccessIterator> runs;
auto begin = beyond;
auto current = first;
auto next = std::next(first);
while (true) {
auto begin_range = current;
if (std::distance(next, beyond) <= limit) {
if (begin == beyond) {
begin = begin_range;
}
break;
}
std::advance(current, limit);
std::advance(next, limit);
auto current2 = current;
auto next2 = next;
if (less(*next, *current)) {
do {
--current;
--next;
if (less(*current, *next)) break;
} while (current != begin_range);
if (less(*current, *next)) ++current;
++current2;
++next2;
while (next2 != beyond) {
if (less(*current2, *next2)) break;
++current2;
++next2;
}
if (std::distance(current, next2) >= limit) {
std::reverse(current, next2);
if (std::distance(begin_range, current) && begin == beyond) {
begin = begin_range;
}
if (begin != beyond) {
std::sort(begin, current, less);
runs.push_back(current);
begin = beyond;
}
runs.push_back(next2);
} else {
if (begin == beyond) {
begin = begin_range;
}
}
} else {
do {
--current;
--next;
if (less(*next, *current)) break;
} while (current != begin_range);
if (less(*next, *current)) ++current;
++current2;
++next2;
while (next2 != beyond) {
if (less(*next2, *current2)) break;
++current2;
++next2;
}
if (std::distance(current, next2) >= limit) {
if (std::distance(begin_range, current) && begin == beyond) {
begin = begin_range;
}
if (begin != beyond) {
std::sort(begin, current, less);
runs.push_back(current);
begin = beyond;
}
runs.push_back(next2);
} else {
if (begin == beyond) {
begin = begin_range;
}
}
}
if (next2 == beyond) break;
current = std::next(current2);
next = std::next(next2);
}
if (begin != beyond) {
runs.push_back(beyond);
std::sort(begin, beyond, less);
}
if (runs.size() < 2) return;
do {
auto again_begin = first;
for (auto it = runs.begin(); it != runs.end() && it != std::prev(runs.end()); ++it) {
std::inplace_merge(again_begin, *it, *std::next(it), less);
it = runs.erase(it);
again_begin = *it;
}
} while (runs.size() > 1);
}
template<class InputIterator, class OutputIterator, class Less>
OutputIterator sort(
InputIterator first,
InputIterator beyond,
OutputIterator result,
Less less)
{
typedef typename std::iterator_traits<InputIterator>::value_type type;
std::vector<type> V(first, beyond);
hybrid_sort( V.begin(), V.end(), less);
std::copy( V.begin(), V.end(), result);
return result;
}
}