163 void IntroSort(
int low,
int high,
int max_depth, T data[]);
164 void IntroSort_Index(
int low,
int high,
int max_depth,
int indexlist[],
167 void QuickSort(
int low,
int high, T data[]);
168 void QuickSort_Index(
int low,
int high,
int indexlist[],
const T data[]);
170 void HeapSort(
int low,
int high, T data[]);
171 void HeapSort_Index(
int low,
int high,
int indexlist[],
const T data[]);
173 void InsertSort(
int low,
int high, T data[]);
174 void InsertSort_Index(
int low,
int high,
int indexlist[],
const T data[]);
190 s.sort(0, data.size() - 1, data);
203 ivec sort_index(
const Vec<T> &data,
SORTING_METHOD method = INTROSORT)
206 return s.sort_index(0, data.size() - 1, data);
222 it_assert((low >= 0) && (high > low) && (high < N),
"Sort::sort(): "
223 "low or high out of bounds");
225 switch (sort_method) {
230 QuickSort(low, high, data.
_data());
233 HeapSort(low, high, data.
_data());
236 InsertSort(low, high, data.
_data());
239 it_error(
"Sort<T>::sort(): Unknown sorting method");
254 it_assert((low >= 0) && (high > low) && (high < N),
"Sort::sort(): "
255 "low or high out of bounds");
258 for (
int i = 0; i < N; ++i) {
262 switch (sort_method) {
264 IntroSort_Index(low, high,
levels2bits(N), indexlist._data(),
268 QuickSort_Index(low, high, indexlist._data(), data.
_data());
271 HeapSort_Index(low, high, indexlist._data(), data.
_data());
274 InsertSort_Index(low, high, indexlist._data(), data.
_data());
277 it_error(
"Sort<T>::sort_index(): Unknown sorting method");
288 it_assert((low >= 0) && (high > low) && (high < data.
size()),
289 "Sort::sort(): low or high out of bounds");
290 IntroSort(low, high, max_depth, data.
_data());
299 it_assert((low >= 0) && (high > low) && (high < N),
300 "Sort::sort(): low or high out of bounds");
303 for (
int i = 0; i < N; ++i) {
307 IntroSort_Index(low, high, max_depth, indexlist._data(), data.
_data());
320 if (high - low > 16) {
322 if (max_depth == 0) {
323 HeapSort(low, high, data);
331 T test = data[phigh];
332 while (plow < phigh) {
345 IntroSort(low, plow - 1, max_depth, data);
346 IntroSort(plow + 1, high, max_depth, data);
351 InsertSort(low, high, data);
357 void Sort<T>::IntroSort_Index(
int low,
int high,
int max_depth,
358 int indexlist[],
const T data[])
360 if (high - low > 16) {
362 if (max_depth == 0) {
363 HeapSort_Index(low, high, indexlist, data);
368 int aindex = indexlist[low];
372 int testindex = indexlist[phigh];
373 T test = data[testindex];
374 while (plow < phigh) {
376 indexlist[plow] = testindex;
378 testindex = indexlist[plow];
379 test = data[testindex];
382 indexlist[phigh] = testindex;
384 testindex = indexlist[phigh];
385 test = data[testindex];
388 indexlist[plow] = aindex;
389 IntroSort_Index(low, plow - 1, max_depth, indexlist, data);
390 IntroSort_Index(plow + 1, high, max_depth, indexlist, data);
394 InsertSort_Index(low, high, indexlist, data);
400 void Sort<T>::QuickSort(
int low,
int high, T data[])
406 T test = data[phigh];
407 while (plow < phigh) {
420 QuickSort(low, plow - 1, data);
421 QuickSort(plow + 1, high, data);
426 void Sort<T>::QuickSort_Index(
int low,
int high,
int indexlist[],
430 int aindex = indexlist[low];
434 int testindex = indexlist[phigh];
435 T test = data[testindex];
436 while (plow < phigh) {
438 indexlist[plow] = testindex;
440 testindex = indexlist[plow];
441 test = data[testindex];
444 indexlist[phigh] = testindex;
446 testindex = indexlist[phigh];
447 test = data[testindex];
450 indexlist[plow] = aindex;
451 QuickSort_Index(low, plow - 1, indexlist, data);
452 QuickSort_Index(plow + 1, high, indexlist, data);
457 void Sort<T>::HeapSort(
int low,
int high, T data[])
459 int size = (high + 1) - low;
464 temp = data[--i + low];
468 temp = data[size + low];
469 data[size + low] = data[low];
473 int child = i * 2 + 1;
475 while (child < size) {
476 if (child + 1 < size && data[child + 1 + low] > data[child + low])
478 if (data[child + low] > temp) {
479 data[parent + low] = data[child + low];
481 child = parent * 2 + 1;
486 data[parent + low] = temp;
491 void Sort<T>::HeapSort_Index(
int low,
int high,
int indexlist[],
494 int size = (high + 1) - low;
502 tempValue = data[indexlist[i + low]];
503 tempIndex = indexlist[i + low];
508 tempValue = data[indexlist[size + low]];
509 tempIndex = indexlist[size + low];
510 indexlist[size+low] = indexlist[low];
514 int child = i * 2 + 1;
516 while (child < size) {
517 if ((child + 1 < size)
518 && data[indexlist[child + 1 + low]] > data[indexlist[child + low]])
520 if (data[indexlist[child + low]] > tempValue) {
521 indexlist[parent + low] = indexlist[child + low];
523 child = parent * 2 + 1;
528 indexlist[parent + low] = tempIndex;
533 void Sort<T>::InsertSort(
int low,
int high, T data[])
535 for (
int i = low + 1; i <= high; i++) {
538 for (j = i - 1; j >= low && data[j] > value; j--) {
539 data[j + 1] = data[j];
546 void Sort<T>::InsertSort_Index(
int low,
int high,
int indexlist[],
549 for (
int i = low + 1; i <= high; i++) {
550 T value = data[indexlist[i]];
551 int tempIndex = indexlist[i];
553 for (j = i - 1; j >= low && data[indexlist[j]] > value; j--) {
554 indexlist[j + 1] = indexlist[j];
556 indexlist[j + 1] = tempIndex;
563 #endif // #ifndef SORT_H