-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuickSort.h
More file actions
188 lines (160 loc) · 3.94 KB
/
QuickSort.h
File metadata and controls
188 lines (160 loc) · 3.94 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
#ifndef __DS_QUICKSORT_H__
#define __DS_QUICKSORT_H__
#include "CommonTypeDefines.h"
namespace DataStructures
{
const int sort_cut_off = 10;
const int sort_max_level = 32;
template <class T>
struct pair
{
pair(const T& _first, const T& _second) : first(_first), second(_second) {}
T first;
T second;
};
template <class Iter, class Ty>
inline void interator_swap(Iter a, Iter b)
{
Ty save = *a;
*a = *b;
*b = save;
}
template <class Iter, class Ty>
inline void insertion_sort(Iter first, Iter last)
{
// insertion sort [first, last]
if (first != last)
{
Iter next = first;
do
{
Ty val = *++next;
Iter next1 = next;
for (; next1 != first && val < *(next1 - 1); next1--)
*next1 = *(next1 - 1);
*next1 = val;
}while (next != last);
}
}
template <class Iter, class Ty>
inline void shellsort(Iter first, Iter last)
{
// shell sort [first, last]
const int hmax = (int)((last - first + 1) / 9);
int h;
for(h = 1; h <= hmax; h = 3*h+1);
for(; h > 0; h /= 3)
{
for(Iter p = first + h; p < last; ++p)
{
Ty v = *p;
Iter p1 = p;
while( p1 - first >= h && v < *(p1 - h))
{
*p1 = *(p1 - h);
p1 -= h;
}
*p1 = v;
}
}
}
template <class Iter, class Ty>
inline void med3(Iter first, Iter mid, Iter last)
{
// sort median of three elements to middle
if (*mid < *first)
interator_swap<Iter, Ty>(mid, first);
if (*last < *mid)
interator_swap<Iter, Ty>(last, mid);
if (*mid < *first)
interator_swap<Iter, Ty>(mid, first);
}
template <class Iter, class Ty>
inline void median(Iter first, Iter mid, Iter last)
{
// sort median element to middle
if (40 < last - first)
{
// median of nine
int step = (int)((last - first + 1) / 8);
med3<Iter, Ty>(first, first + step, first + 2 * step);
med3<Iter, Ty>(mid - step, mid, mid + step);
med3<Iter, Ty>(last - 2 * step, last - step, last);
med3<Iter, Ty>(first + step, mid, last - step);
}
else
med3<Iter, Ty>(first, mid, last);
}
template <class Iter, class Ty>
inline pair<Iter> partition(Iter first, Iter last)
{
// partition [first, last]
Iter mid = first + (last - first + 1) / 2; // fort median to mid;
median<Iter, Ty>(first, mid, last);
Ty pivot = *mid;
Iter pfirst = first;
Iter plast = last - 1;
Iter gfirst = first;
Iter glast = plast;
for (;;)
{
while (*++pfirst < pivot);
while (pivot < *--plast)
{
if (plast == pfirst)
break;
}
if (pfirst >= plast)
break;
interator_swap<Iter, Ty>(pfirst, plast);
if (*pfirst == pivot)
interator_swap<Iter, Ty>(++gfirst, pfirst);
if (*plast == pivot)
interator_swap<Iter, Ty>(--glast, plast);
}
interator_swap<Iter, Ty>(pfirst, last - 1);
plast = pfirst++ - 1;
for (Iter p = first; p < gfirst; p++, plast--)
interator_swap<Iter, Ty>(p, plast);
for (Iter p = last - 1; p > glast; p--, gfirst++)
interator_swap<Iter, Ty>(p, pfirst);
return pair<Iter>(pfirst, plast);
}
template <class Iter, class Ty>
inline void quicksort_r(Iter first, Iter last, int ideal)
{
// order [first, last]
int count;
for(; sort_max_level < (count = (int)(last - first + 1)) && 0 < ideal; )
{
// divide and conquer by quicksort
pair<Iter> mid = partition<Iter, Ty>(first, last);
ideal /= 2, ideal += ideal / 2; // allow 1.5 log2(N) divisions
if (mid.first - first <= last - mid.second)
{
// loop on second half
quicksort_r<Iter, Ty>(first, mid.first, ideal);
first = mid.second;
}
else
{
// loop on first half
quicksort_r<Iter, Ty>(mid.second, last, ideal);
last = mid.first;
}
}
if (sort_max_level < count)
shellsort<Iter, Ty>(first, last);
else if (1 < count)
insertion_sort<Iter, Ty>(first, last);
}
template <class Iter, class Ty>
void QuickSort(Iter first, Iter last, int size)
{
if (size <= sort_cut_off)
insertion_sort<Iter, Ty>(first, last);
else
quicksort_r<Iter, Ty>(first, last, size);
}
}
#endif