39void SPxShellsort(T* keys,
int end, COMPARATOR& compare,
int start = 0)
41 static const int incs[3] = {1, 5, 19};
46 for(
int k = 2; k >= 0; --k)
48 const int h = incs[k];
50 for(
int i = start + h; i <= end; ++i)
52 if(compare(keys[i], keys[i - h]) < 0)
54 const T tmp = keys[i];
59 keys[j + h] = keys[j];
62 while(j >= start && compare(tmp, keys[j]) < 0);
80void SPxQuicksort(T* keys,
int end, COMPARATOR& compare,
int start = 0,
bool type =
true)
95 const int mid = start + (end - start) / 2;
96 const T pivotkey = keys[mid];
104 while(compare(keys[lo], pivotkey) < 0)
107 while(hi > start && compare(keys[hi], pivotkey) >= 0)
112 while(lo < end && compare(pivotkey, keys[lo]) >= 0)
115 while(compare(pivotkey, keys[hi]) < 0)
122 const T tmp = keys[lo];
130 assert((hi == lo - 1) || (type && hi == start) || (!type && lo == end));
135 while(lo < end && compare(pivotkey, keys[lo]) >= 0)
142 assert(compare(keys[mid], pivotkey) == 0);
144 const T tmp = keys[lo];
145 keys[lo] = keys[mid];
153 while(hi > start && compare(keys[hi], pivotkey) >= 0)
160 assert(compare(keys[mid], pivotkey) == 0);
162 const T tmp = keys[hi];
163 keys[hi] = keys[mid];
171 if(hi - start <= end - lo)
198 for(
int i = start; i < end; ++i)
199 assert(compare(keys[i], keys[i + 1]) <= 0);
228int SPxQuicksortPart(T* keys, COMPARATOR& compare,
int start,
int end,
int size,
int start2 = 0,
229 int end2 = 0,
bool type =
true)
232 assert(end >= start);
233 assert(start2 <= end);
243 for(
int i = start; i < start2 - 1; ++i)
244 assert(compare(keys[i], keys[i + 1]) <= 0);
256 if(start >= end - size - 1)
266 const int mid = start + (end - start) / 2;
267 const T pivotkey = keys[mid];
275 while(compare(keys[lo], pivotkey) < 0)
278 while(hi > start && compare(keys[hi], pivotkey) >= 0)
283 while(lo < end && compare(pivotkey, keys[lo]) >= 0)
286 while(compare(pivotkey, keys[hi]) < 0)
293 const T tmp = keys[lo];
301 assert((hi == lo - 1) || (type && hi == start) || (!type && lo == end));
306 while(lo < end && compare(pivotkey, keys[lo]) >= 0)
313 assert(compare(keys[mid], pivotkey) == 0);
315 const T tmp = keys[lo];
316 keys[lo] = keys[mid];
324 while(hi > start && compare(keys[hi], pivotkey) >= 0)
331 assert(compare(keys[mid], pivotkey) == 0);
333 const T tmp = keys[hi];
334 keys[hi] = keys[mid];
343 for(
int i = start; i < lo; ++i)
344 assert(compare(keys[i], pivotkey) <= 0);
349 if(start <= hi - 2 * size)
355 else if(start <= lo - size)
int SPxQuicksortPart(T *keys, COMPARATOR &compare, int start, int end, int size, int start2=0, int end2=0, bool type=true)
Generic implementation of Partial QuickSort.
void SPxQuicksort(T *keys, int end, COMPARATOR &compare, int start=0, bool type=true)
Generic QuickSort implementation.