-
Notifications
You must be signed in to change notification settings - Fork 0
/
sorter.h
151 lines (136 loc) · 3.16 KB
/
sorter.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
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
template <typename T> class sorter_t
{
public:
// 快速排序
int qsort(T* begin, T* end);
private:
const static int sthreshold = 1;
private:
// 插入排序 (假定序列的最小元素在[begin, begin + threshold)之间)
void _insertion_sort(T* begin, T* end, int threshold);
// [first, last)之间元素有序,本函数将last插入到[first, last)中使得[first, last]有序
inline void _linear_insert(T* first, T* last);
// 划分元素使得序列被分成两部分,且左边元素 <= pivot <= 右边元素
inline T* _partition(T* begin, T* end, T pivot);
// median_of_three: 交换元素使得first < middle < last
inline const T& _median_of_three(T& first, T& middle, T& last)
{
if (middle < first)
{
_swap(first, middle);
}
if (last < middle)
{
_swap(middle, last);
if (middle < first)
{
_swap(first, middle);
}
}
return middle;
}
inline void _swap(T& a, T& b)
{
T tmp = a;
a = b;
b = tmp;
}
// Qsort 排序算法
void _qsort_loop(T* begin, T* end);
};
template <typename T> void sorter_t<T>::_insertion_sort(T* begin, T* end, int threshold)
{
T* p = NULL;
if (begin < end)
{
T* min = begin;
for (p = begin + 1; p < begin + threshold && p < end; ++p)
{
if (*p < *min)
{
min = p;
}
}
_swap(*begin, *min);
for (p = begin + 1; p < end; ++p)
{
_linear_insert(begin, p);
}
}
}
template <typename T> void sorter_t<T>::_linear_insert(T* first, T* last)
{
T value = *last;
T* p = NULL;
if (value < *first)
{
for (p = last; p > first; --p)
{
*p = *(p-1);
}
*last = value;
} else
{
p = last - 1;
while (value < *p)
{
*(p+1) = *p;
--p;
}
*(p+1) = value;
}
}
template <typename T> T* sorter_t<T>::_partition(T* begin, T* end, T pivot)
{
while (true)
{
while (*begin < pivot)
{
++begin;
}
--end;
while (pivot < *end)
{
--end;
}
if (!(begin < end))
{
break;
}
_swap(*begin, *end);
++begin;
}
return begin;
}
template <typename T> int sorter_t<T>::qsort(T* begin, T* end)
{
int ret = 0;
if (NULL == begin || NULL == end)
{
fprintf(stderr, "begin or end is NULL");
ret = -1;
}
else
{
_qsort_loop(begin, end);
_insertion_sort(begin, end, sorter_t<T>::sthreshold);
}
return ret;
}
template <typename T> void sorter_t<T>::_qsort_loop(T* begin, T* end)
{
while (end - begin > sorter_t<T>::sthreshold)
{
T median_of_three = _median_of_three(*begin, *(begin + (end - begin)/2), *(end - 1));
T* cut = _partition(begin, end, median_of_three);
if (cut - begin >= end - cut)
{
_qsort_loop(cut, end);
end = cut;
} else
{
_qsort_loop(begin, cut);
begin = cut;
}
}
}