Sort.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2012 Lasse Öörni
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to deal
  7. // in the Software without restriction, including without limitation the rights
  8. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. // THE SOFTWARE.
  22. //
  23. #pragma once
  24. #include "Swap.h"
  25. #include "VectorBase.h"
  26. static const int QUICKSORT_THRESHOLD = 16;
  27. // Based on Comparison of several sorting algorithms by Juha Nieminen
  28. // http://warp.povusers.org/SortComparison/
  29. /// Perform insertion sort on an array.
  30. template <class T> void InsertionSort(RandomAccessIterator<T> begin, RandomAccessIterator<T> end)
  31. {
  32. for (RandomAccessIterator<T> i = begin + 1; i < end; ++i)
  33. {
  34. T temp = *i;
  35. RandomAccessIterator<T> j = i;
  36. while (j > begin && temp < *(j - 1))
  37. {
  38. *j = *(j - 1);
  39. --j;
  40. }
  41. *j = temp;
  42. }
  43. }
  44. /// Perform insertion sort on an array using a compare function.
  45. template <class T, class U> void InsertionSort(RandomAccessIterator<T> begin, RandomAccessIterator<T> end, U compare)
  46. {
  47. for (RandomAccessIterator<T> i = begin + 1; i < end; ++i)
  48. {
  49. T temp = *i;
  50. RandomAccessIterator<T> j = i;
  51. while (j > begin && compare(temp, *(j - 1)))
  52. {
  53. *j = *(j - 1);
  54. --j;
  55. }
  56. *j = temp;
  57. }
  58. }
  59. /// Perform quick sort initial pass on an array. Does not sort fully.
  60. template <class T> void InitialQuickSort(RandomAccessIterator<T> begin, RandomAccessIterator<T> end)
  61. {
  62. while (end - begin > QUICKSORT_THRESHOLD)
  63. {
  64. // Choose the pivot by median
  65. RandomAccessIterator<T> pivot = begin + ((end - begin) / 2);
  66. if (*begin < *pivot && *(end - 1) < *begin)
  67. pivot = begin;
  68. else if (*(end - 1) < *pivot && *begin < *(end - 1))
  69. pivot = end - 1;
  70. // Partition and sort recursively
  71. RandomAccessIterator<T> i = begin - 1;
  72. RandomAccessIterator<T> j = end;
  73. T pivotValue = *pivot;
  74. for (;;)
  75. {
  76. while (pivotValue < *(--j));
  77. while (*(++i) < pivotValue);
  78. if (i < j)
  79. Swap(*i, *j);
  80. else
  81. break;
  82. }
  83. InitialQuickSort(begin, j + 1);
  84. begin = j + 1;
  85. }
  86. }
  87. /// Perform quick sort initial pass on an array using a compare function. Does not sort fully.
  88. template <class T, class U> void InitialQuickSort(RandomAccessIterator<T> begin, RandomAccessIterator<T> end, U compare)
  89. {
  90. while (end - begin > QUICKSORT_THRESHOLD)
  91. {
  92. // Choose the pivot by median
  93. RandomAccessIterator<T> pivot = begin + ((end - begin) / 2);
  94. if (compare(*begin, *pivot) && compare(*(end - 1), *begin))
  95. pivot = begin;
  96. else if (compare(*(end - 1), *pivot) && compare(*begin, *(end - 1)))
  97. pivot = end - 1;
  98. // Partition and sort recursively
  99. RandomAccessIterator<T> i = begin - 1;
  100. RandomAccessIterator<T> j = end;
  101. T pivotValue = *pivot;
  102. for (;;)
  103. {
  104. while (compare(pivotValue, *(--j)));
  105. while (compare(*(++i), pivotValue));
  106. if (i < j)
  107. Swap(*i, *j);
  108. else
  109. break;
  110. }
  111. InitialQuickSort(begin, j + 1, compare);
  112. begin = j + 1;
  113. }
  114. }
  115. /// Sort in ascending order using quicksort for initial passes, then an insertion sort to finalize.
  116. template <class T> void Sort(RandomAccessIterator<T> begin, RandomAccessIterator<T> end)
  117. {
  118. InitialQuickSort(begin, end);
  119. InsertionSort(begin, end);
  120. }
  121. /// Sort in ascending order using quicksort for initial passes, then an insertion sort to finalize, using a compare function.
  122. template <class T, class U> void Sort(RandomAccessIterator<T> begin, RandomAccessIterator<T> end, U compare)
  123. {
  124. InitialQuickSort(begin, end, compare);
  125. InsertionSort(begin, end, compare);
  126. }