27 #define SHELLSORTMAX 25 30 template <
class T,
class COMPARATOR >
31 void SPxShellsort(T* keys,
int end, COMPARATOR& compare,
int start = 0)
33 static const int incs[3] = {1, 5, 19};
38 for(k = 2; k >= 0; --k)
41 int first = h + start;
44 for(i = first; i <= end; ++i)
51 while(j >= first && compare(tempkey, keys[j - h]) < 0)
53 keys[j] = keys[j - h];
73 template <
class T,
class COMPARATOR >
74 void SPxQuicksort(T* keys,
int end, COMPARATOR& compare,
int start = 0,
bool type =
true)
95 mid = (start + end) / 2;
106 while(lo < end && compare(keys[lo], pivotkey) < 0)
109 while(hi > start && compare(keys[hi], pivotkey) >= 0)
114 while(lo < end && compare(keys[lo], pivotkey) <= 0)
117 while(hi > start && compare(keys[hi], pivotkey) > 0)
132 assert((hi == lo - 1) || (type && hi == start) || (!type && lo == end));
137 while(lo < end && compare(pivotkey, keys[lo]) >= 0)
144 assert(compare(keys[mid], pivotkey) == 0);
147 keys[lo] = keys[mid];
155 while(hi > start && compare(pivotkey, keys[hi]) <= 0)
162 assert(compare(keys[mid], pivotkey) == 0);
165 keys[hi] = keys[mid];
173 if(hi - start <= end - lo)
207 for(
int i = start; i < end; ++i)
208 assert(compare(keys[i], keys[i + 1]) <= 0);
226 template <
class T,
class COMPARATOR >
248 else if(end == start + 1)
257 assert(start2 < end);
259 for(
int i = start; i < start2 - 1; ++i)
260 assert(compare(keys[i], keys[i + 1]) <= 0);
266 if(start2 + size >= end - 1)
282 mid = (start2 + end) / 2;
283 pivotkey = keys[mid];
293 while(lo < end && compare(keys[lo], pivotkey) < 0)
296 while(hi > start2 && compare(keys[hi], pivotkey) >= 0)
301 while(lo < end && compare(keys[lo], pivotkey) <= 0)
304 while(hi > start2 && compare(keys[hi], pivotkey) > 0)
319 assert((hi == lo - 1) || (type && hi == start2) || (!type && lo == end));
324 while(lo < end && compare(pivotkey, keys[lo]) >= 0)
331 assert(compare(keys[mid], pivotkey) == 0);
334 keys[lo] = keys[mid];
342 while(hi > start2 && compare(pivotkey, keys[hi]) <= 0)
349 assert(compare(keys[mid], pivotkey) == 0);
352 keys[hi] = keys[mid];
361 for(
int i = start2; i < lo; ++i)
362 assert(compare(keys[i], pivotkey) <= 0);
367 if(2 * size <= hi - start2)
369 return SPxQuicksortPart(keys, compare, start, hi + 1, size, start2, end2, !type);
372 else if(size <= lo - start2)
381 return SPxQuicksortPart(keys, compare, start, end, 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.