Sort.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2012 Lasse Oorni
  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. namespace Urho3D
  27. {
  28. static const int QUICKSORT_THRESHOLD = 16;
  29. // Based on Comparison of several sorting algorithms by Juha Nieminen
  30. // http://warp.povusers.org/SortComparison/
  31. /// Perform insertion sort on an array.
  32. template <class T> void InsertionSort(RandomAccessIterator<T> begin, RandomAccessIterator<T> end)
  33. {
  34. for (RandomAccessIterator<T> i = begin + 1; i < end; ++i)
  35. {
  36. T temp = *i;
  37. RandomAccessIterator<T> j = i;
  38. while (j > begin && temp < *(j - 1))
  39. {
  40. *j = *(j - 1);
  41. --j;
  42. }
  43. *j = temp;
  44. }
  45. }
  46. /// Perform insertion sort on an array using a compare function.
  47. template <class T, class U> void InsertionSort(RandomAccessIterator<T> begin, RandomAccessIterator<T> end, U compare)
  48. {
  49. for (RandomAccessIterator<T> i = begin + 1; i < end; ++i)
  50. {
  51. T temp = *i;
  52. RandomAccessIterator<T> j = i;
  53. while (j > begin && compare(temp, *(j - 1)))
  54. {
  55. *j = *(j - 1);
  56. --j;
  57. }
  58. *j = temp;
  59. }
  60. }
  61. /// Perform quick sort initial pass on an array. Does not sort fully.
  62. template <class T> void InitialQuickSort(RandomAccessIterator<T> begin, RandomAccessIterator<T> end)
  63. {
  64. while (end - begin > QUICKSORT_THRESHOLD)
  65. {
  66. // Choose the pivot by median
  67. RandomAccessIterator<T> pivot = begin + ((end - begin) / 2);
  68. if (*begin < *pivot && *(end - 1) < *begin)
  69. pivot = begin;
  70. else if (*(end - 1) < *pivot && *begin < *(end - 1))
  71. pivot = end - 1;
  72. // Partition and sort recursively
  73. RandomAccessIterator<T> i = begin - 1;
  74. RandomAccessIterator<T> j = end;
  75. T pivotValue = *pivot;
  76. for (;;)
  77. {
  78. while (pivotValue < *(--j));
  79. while (*(++i) < pivotValue);
  80. if (i < j)
  81. Swap(*i, *j);
  82. else
  83. break;
  84. }
  85. InitialQuickSort(begin, j + 1);
  86. begin = j + 1;
  87. }
  88. }
  89. /// Perform quick sort initial pass on an array using a compare function. Does not sort fully.
  90. template <class T, class U> void InitialQuickSort(RandomAccessIterator<T> begin, RandomAccessIterator<T> end, U compare)
  91. {
  92. while (end - begin > QUICKSORT_THRESHOLD)
  93. {
  94. // Choose the pivot by median
  95. RandomAccessIterator<T> pivot = begin + ((end - begin) / 2);
  96. if (compare(*begin, *pivot) && compare(*(end - 1), *begin))
  97. pivot = begin;
  98. else if (compare(*(end - 1), *pivot) && compare(*begin, *(end - 1)))
  99. pivot = end - 1;
  100. // Partition and sort recursively
  101. RandomAccessIterator<T> i = begin - 1;
  102. RandomAccessIterator<T> j = end;
  103. T pivotValue = *pivot;
  104. for (;;)
  105. {
  106. while (compare(pivotValue, *(--j)));
  107. while (compare(*(++i), pivotValue));
  108. if (i < j)
  109. Swap(*i, *j);
  110. else
  111. break;
  112. }
  113. InitialQuickSort(begin, j + 1, compare);
  114. begin = j + 1;
  115. }
  116. }
  117. /// Sort in ascending order using quicksort for initial passes, then an insertion sort to finalize.
  118. template <class T> void Sort(RandomAccessIterator<T> begin, RandomAccessIterator<T> end)
  119. {
  120. InitialQuickSort(begin, end);
  121. InsertionSort(begin, end);
  122. }
  123. /// Sort in ascending order using quicksort for initial passes, then an insertion sort to finalize, using a compare function.
  124. template <class T, class U> void Sort(RandomAccessIterator<T> begin, RandomAccessIterator<T> end, U compare)
  125. {
  126. InitialQuickSort(begin, end, compare);
  127. InsertionSort(begin, end, compare);
  128. }
  129. }