-
Notifications
You must be signed in to change notification settings - Fork 2
/
dualpivotquick.h
65 lines (50 loc) · 1.35 KB
/
dualpivotquick.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
64
65
#ifndef _DUALPIVOTQUICK_H__
#define _DUALPIVOTQUICK_H__
#include <algorithm>
namespace SortBenchDual{
const int cutoff = 32;
template <typename _RanIt>
void dp_qsort(_RanIt first, _RanIt last,int div){
typedef iterator_traits<_RanIt>::value_type value_t;
intptr_t len = last - first;
if( len < cutoff )
return;
intptr_t third = len / div;
//medians
_RanIt m1 = first + third;
_RanIt m2 = last - third;
if (*m1 < *m2){
std::iter_swap(m1,first);
std::iter_swap(m2,last-1);
}else{
std::iter_swap(m1,last-1);
std::iter_swap(m2,first);
}
value_t pivot1 = *first;
value_t pivot2 = *(last-1);
_RanIt lessI = first + 1;
_RanIt greatI = last - 2;
//sorting
for (_RanIt k = lessI; k <= greatI; ++k){
if (*k < pivot1)
std::iter_swap(k,lessI++);
else if (pivot2 < *k){
while ( k < greatI && pivot2 < *greatI)
--greatI;
std::iter_swap(k,greatI--);
if (*k < pivot1)
std::iter_swap(k,lessI++);
}
}
std::iter_swap(lessI-1, first);
std::iter_swap(greatI+1,last-1);
if(greatI - lessI < 13)
div++;
// subarrays
dp_qsort(first, lessI - 1, div);
dp_qsort(greatI + 2, last, div);
if(pivot1 < pivot2)
dp_qsort(lessI,greatI+1,div);
}
}
#endif // _DUALPIVOTQUICK_H__