35#define SOPLEX_SHELLSORTMAX 25
38template <
class T,
class COMPARATOR >
39void SPxShellsort(T* keys,
int end, COMPARATOR& compare,
int start = 0)
41 static const int incs[3] = {1, 5, 19};
46 for(k = 2; k >= 0; --k)
49 int first = h + start;
52 for(i = first; i <= end; ++i)
59 while(j >= first && compare(tempkey, keys[j - h]) < 0)
61 keys[j] = keys[j - h];
81template <
class T,
class COMPARATOR >
82void SPxQuicksort(T* keys,
int end, COMPARATOR& compare,
int start = 0,
bool type =
true)
103 mid = start + (end - start) / 2;
106 pivotkey = keys[mid];
116 while(lo < end && compare(keys[lo], pivotkey) < 0)
119 while(hi > start && compare(keys[hi], pivotkey) >= 0)
124 while(lo < end && compare(keys[lo], pivotkey) <= 0)
127 while(hi > start && compare(keys[hi], pivotkey) > 0)
142 assert((hi == lo - 1) || (type && hi == start) || (!type && lo == end));
147 while(lo < end && compare(pivotkey, keys[lo]) >= 0)
154 assert(compare(keys[mid], pivotkey) == 0);
157 keys[lo] = keys[mid];
165 while(hi > start && compare(pivotkey, keys[hi]) <= 0)
172 assert(compare(keys[mid], pivotkey) == 0);
175 keys[hi] = keys[mid];
183 if(hi - start <= end - lo)
217 for(
int i = start; i < end; ++i)
218 assert(compare(keys[i], keys[i + 1]) <= 0);
245template <
class T,
class COMPARATOR >
246int SPxQuicksortPart(T* keys, COMPARATOR& compare,
int start,
int end,
int size,
int start2 = 0,
247 int end2 = 0,
bool type =
true)
254 else if(end == start + 1)
263 assert(start2 < end);
265 for(
int i = start; i < start2 - 1; ++i)
266 assert(compare(keys[i], keys[i + 1]) <= 0);
272 if(start2 + size >= end - 1)
288 mid = (start2 + end) / 2;
289 pivotkey = keys[mid];
299 while(lo < end && compare(keys[lo], pivotkey) < 0)
302 while(hi > start2 && compare(keys[hi], pivotkey) >= 0)
307 while(lo < end && compare(keys[lo], pivotkey) <= 0)
310 while(hi > start2 && compare(keys[hi], pivotkey) > 0)
325 assert((hi == lo - 1) || (type && hi == start2) || (!type && lo == end));
330 while(lo < end && compare(pivotkey, keys[lo]) >= 0)
337 assert(compare(keys[mid], pivotkey) == 0);
340 keys[lo] = keys[mid];
348 while(hi > start2 && compare(pivotkey, keys[hi]) <= 0)
355 assert(compare(keys[mid], pivotkey) == 0);
358 keys[hi] = keys[mid];
367 for(
int i = start2; i < lo; ++i)
368 assert(compare(keys[i], pivotkey) <= 0);
373 if(2 * size <= hi - start2)
375 return SPxQuicksortPart(keys, compare, start, hi + 1, size, start2, end2, !type);
378 else if(size <= lo - start2)
387 return SPxQuicksortPart(keys, compare, start, end + 1, size + start2 - lo, lo, end2, !type);
Everything should be within this namespace.
void SPxShellsort(T *keys, int end, COMPARATOR &compare, int start=0)
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.
#define SOPLEX_SHELLSORTMAX