26 #define SHELLSORTMAX 25 29 template <
class T,
class COMPARATOR >
30 void SPxShellsort(T* keys,
int end, COMPARATOR& compare,
int start = 0)
32 static const int incs[3] = {1, 5, 19};
37 for(k = 2; k >= 0; --k)
40 int first = h + start;
43 for(i = first; i <= end; ++i)
50 while(j >= first && compare(tempkey, keys[j - h]) < 0)
52 keys[j] = keys[j - h];
72 template <
class T,
class COMPARATOR >
73 void SPxQuicksort(T* keys,
int end, COMPARATOR& compare,
int start = 0,
bool type =
true)
94 mid = start + (end - start) / 2;
107 while(lo < end && compare(keys[lo], pivotkey) < 0)
110 while(hi > start && compare(keys[hi], pivotkey) >= 0)
115 while(lo < end && compare(keys[lo], pivotkey) <= 0)
118 while(hi > start && compare(keys[hi], pivotkey) > 0)
133 assert((hi == lo - 1) || (type && hi == start) || (!type && lo == end));
138 while(lo < end && compare(pivotkey, keys[lo]) >= 0)
145 assert(compare(keys[mid], pivotkey) == 0);
148 keys[lo] = keys[mid];
156 while(hi > start && compare(pivotkey, keys[hi]) <= 0)
163 assert(compare(keys[mid], pivotkey) == 0);
166 keys[hi] = keys[mid];
174 if(hi - start <= end - lo)
208 for(
int i = start; i < end; ++i)
209 assert(compare(keys[i], keys[i + 1]) <= 0);
227 template <
class T,
class COMPARATOR >
244 else if(end == start + 1)
253 assert(start2 < end);
255 for(
int i = start; i < start2 - 1; ++i)
256 assert(compare(keys[i], keys[i + 1]) <= 0);
262 if(start2 + size >= end - 1)
278 mid = (start2 + end) / 2;
279 pivotkey = keys[mid];
289 while(lo < end && compare(keys[lo], pivotkey) < 0)
292 while(hi > start2 && compare(keys[hi], pivotkey) >= 0)
297 while(lo < end && compare(keys[lo], pivotkey) <= 0)
300 while(hi > start2 && compare(keys[hi], pivotkey) > 0)
315 assert((hi == lo - 1) || (type && hi == start2) || (!type && lo == end));
320 while(lo < end && compare(pivotkey, keys[lo]) >= 0)
327 assert(compare(keys[mid], pivotkey) == 0);
330 keys[lo] = keys[mid];
338 while(hi > start2 && compare(pivotkey, keys[hi]) <= 0)
345 assert(compare(keys[mid], pivotkey) == 0);
348 keys[hi] = keys[mid];
357 for(
int i = start2; i < lo; ++i)
358 assert(compare(keys[i], pivotkey) <= 0);
363 if(2 * size <= hi - start2)
365 return SPxQuicksortPart(keys, compare, start, hi + 1, size, start2, end2, !type);
368 else if(size <= lo - start2)
377 return SPxQuicksortPart(keys, compare, start, end + 1, size + start2 - lo, lo, end2, !type);
void SPxQuicksort(T *keys, int end, COMPARATOR &compare, int start=0, bool type=true)
Generic QuickSort implementation.
void SPxShellsort(T *keys, int end, COMPARATOR &compare, int start=0)
Everything should be within this namespace.
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.