IBM JDK

  1. public static void sort(int[] a) {
  2. boolean sortWasSuccessfulOnGPU = tryIntSortOnGPU(a);
  3. if (sortWasSuccessfulOnGPU) {
  4. return;
  5. } else {
  6. DualPivotQuicksort.sort(a, 0, a.length - 1, null, 0, 0);
  7. }
  8. }
  9. public static void sort(int[] a, int fromIndex, int toIndex) {
  10. rangeCheck(a.length, fromIndex, toIndex);
  11. DualPivotQuicksort.sort(a, fromIndex, toIndex - 1, null, 0, 0);
  12. }
  13. public static void sort(long[] a, int fromIndex, int toIndex) {
  14. rangeCheck(a.length, fromIndex, toIndex);
  15. DualPivotQuicksort.sort(a, fromIndex, toIndex - 1, null, 0, 0);
  16. }
  17. public static void sort(short[] a) {
  18. DualPivotQuicksort.sort(a, 0, a.length - 1, null, 0, 0);
  19. }
  20. public static void sort(short[] a, int fromIndex, int toIndex) {
  21. rangeCheck(a.length, fromIndex, toIndex);
  22. DualPivotQuicksort.sort(a, fromIndex, toIndex - 1, null, 0, 0);
  23. }
  24. public static void sort(char[] a) {
  25. DualPivotQuicksort.sort(a, 0, a.length - 1, null, 0, 0);
  26. }
  27. public static void sort(char[] a, int fromIndex, int toIndex) {
  28. rangeCheck(a.length, fromIndex, toIndex);
  29. DualPivotQuicksort.sort(a, fromIndex, toIndex - 1, null, 0, 0);
  30. }
  31. public static void sort(byte[] a) {
  32. DualPivotQuicksort.sort(a, 0, a.length - 1);
  33. }
  34. public static void sort(byte[] a, int fromIndex, int toIndex) {
  35. rangeCheck(a.length, fromIndex, toIndex);
  36. DualPivotQuicksort.sort(a, fromIndex, toIndex - 1);
  37. }
  38. public static void sort(float[] a) {
  39. boolean sortWasSuccessfulOnGPU = tryFloatSortOnGPU(a);
  40. if (sortWasSuccessfulOnGPU) {
  41. return;
  42. } else {
  43. DualPivotQuicksort.sort(a, 0, a.length - 1, null, 0, 0);
  44. }
  45. }
  46. public static void sort(float[] a, int fromIndex, int toIndex) {
  47. rangeCheck(a.length, fromIndex, toIndex);
  48. DualPivotQuicksort.sort(a, fromIndex, toIndex - 1, null, 0, 0);
  49. }
  50. public static void sort(double[] a) {
  51. boolean sortWasSuccessfulOnGPU = tryDoubleSortOnGPU(a);
  52. if (sortWasSuccessfulOnGPU) {
  53. return;
  54. } else {
  55. DualPivotQuicksort.sort(a, 0, a.length - 1, null, 0, 0);
  56. }
  57. }
  58. public static void sort(double[] a, int fromIndex, int toIndex) {
  59. rangeCheck(a.length, fromIndex, toIndex);
  60. DualPivotQuicksort.sort(a, fromIndex, toIndex - 1, null, 0, 0);
  61. }
  62. public static void sort(Object[] a) {
  63. if (LegacyMergeSort.userRequested)
  64. legacyMergeSort(a);
  65. else
  66. ComparableTimSort.sort(a, 0, a.length, null, 0, 0);
  67. }
  68. public static void sort(Object[] a, int fromIndex, int toIndex) {
  69. rangeCheck(a.length, fromIndex, toIndex);
  70. if (LegacyMergeSort.userRequested)
  71. legacyMergeSort(a, fromIndex, toIndex);
  72. else
  73. ComparableTimSort.sort(a, fromIndex, toIndex, null, 0, 0);
  74. }

Oracle JDK

  1. private static void sort(int[] a, int left, int right, boolean leftmost) {
  2. int length = right - left + 1;
  3. // Use insertion sort on tiny arrays
  4. if (length < INSERTION_SORT_THRESHOLD) {
  5. if (leftmost) {
  6. /*
  7. * Traditional (without sentinel) insertion sort,
  8. * optimized for server VM, is used in case of
  9. * the leftmost part.
  10. */
  11. for (int i = left, j = i; i < right; j = ++i) {
  12. int ai = a[i + 1];
  13. while (ai < a[j]) {
  14. a[j + 1] = a[j];
  15. if (j-- == left) {
  16. break;
  17. }
  18. }
  19. a[j + 1] = ai;
  20. }
  21. } else {
  22. /*
  23. * Skip the longest ascending sequence.
  24. */
  25. do {
  26. if (left >= right) {
  27. return;
  28. }
  29. } while (a[++left] >= a[left - 1]);
  30. /*
  31. * Every element from adjoining part plays the role
  32. * of sentinel, therefore this allows us to avoid the
  33. * left range check on each iteration. Moreover, we use
  34. * the more optimized algorithm, so called pair insertion
  35. * sort, which is faster (in the context of Quicksort)
  36. * than traditional implementation of insertion sort.
  37. */
  38. for (int k = left; ++left <= right; k = ++left) {
  39. int a1 = a[k], a2 = a[left];
  40. if (a1 < a2) {
  41. a2 = a1; a1 = a[left];
  42. }
  43. while (a1 < a[--k]) {
  44. a[k + 2] = a[k];
  45. }
  46. a[++k + 1] = a1;
  47. while (a2 < a[--k]) {
  48. a[k + 1] = a[k];
  49. }
  50. a[k + 1] = a2;
  51. }
  52. int last = a[right];
  53. while (last < a[--right]) {
  54. a[right + 1] = a[right];
  55. }
  56. a[right + 1] = last;
  57. }
  58. return;
  59. }
  60. // Inexpensive approximation of length / 7
  61. int seventh = (length >> 3) + (length >> 6) + 1;
  62. /*
  63. * Sort five evenly spaced elements around (and including) the
  64. * center element in the range. These elements will be used for
  65. * pivot selection as described below. The choice for spacing
  66. * these elements was empirically determined to work well on
  67. * a wide variety of inputs.
  68. */
  69. int e3 = (left + right) >>> 1; // The midpoint
  70. int e2 = e3 - seventh;
  71. int e1 = e2 - seventh;
  72. int e4 = e3 + seventh;
  73. int e5 = e4 + seventh;
  74. // Sort these elements using insertion sort
  75. if (a[e2] < a[e1]) { int t = a[e2]; a[e2] = a[e1]; a[e1] = t; }
  76. if (a[e3] < a[e2]) { int t = a[e3]; a[e3] = a[e2]; a[e2] = t;
  77. if (t < a[e1]) { a[e2] = a[e1]; a[e1] = t; }
  78. }
  79. if (a[e4] < a[e3]) { int t = a[e4]; a[e4] = a[e3]; a[e3] = t;
  80. if (t < a[e2]) { a[e3] = a[e2]; a[e2] = t;
  81. if (t < a[e1]) { a[e2] = a[e1]; a[e1] = t; }
  82. }
  83. }
  84. if (a[e5] < a[e4]) { int t = a[e5]; a[e5] = a[e4]; a[e4] = t;
  85. if (t < a[e3]) { a[e4] = a[e3]; a[e3] = t;
  86. if (t < a[e2]) { a[e3] = a[e2]; a[e2] = t;
  87. if (t < a[e1]) { a[e2] = a[e1]; a[e1] = t; }
  88. }
  89. }
  90. }
  91. // Pointers
  92. int less = left; // The index of the first element of center part
  93. int great = right; // The index before the first element of right part
  94. if (a[e1] != a[e2] && a[e2] != a[e3] && a[e3] != a[e4] && a[e4] != a[e5]) {
  95. /*
  96. * Use the second and fourth of the five sorted elements as pivots.
  97. * These values are inexpensive approximations of the first and
  98. * second terciles of the array. Note that pivot1 <= pivot2.
  99. */
  100. int pivot1 = a[e2];
  101. int pivot2 = a[e4];
  102. /*
  103. * The first and the last elements to be sorted are moved to the
  104. * locations formerly occupied by the pivots. When partitioning
  105. * is complete, the pivots are swapped back into their final
  106. * positions, and excluded from subsequent sorting.
  107. */
  108. a[e2] = a[left];
  109. a[e4] = a[right];
  110. /*
  111. * Skip elements, which are less or greater than pivot values.
  112. */
  113. while (a[++less] < pivot1);
  114. while (a[--great] > pivot2);
  115. /*
  116. * Partitioning:
  117. *
  118. * left part center part right part
  119. * +--------------------------------------------------------------+
  120. * | < pivot1 | pivot1 <= && <= pivot2 | ? | > pivot2 |
  121. * +--------------------------------------------------------------+
  122. * ^ ^ ^
  123. * | | |
  124. * less k great
  125. *
  126. * Invariants:
  127. *
  128. * all in (left, less) < pivot1
  129. * pivot1 <= all in [less, k) <= pivot2
  130. * all in (great, right) > pivot2
  131. *
  132. * Pointer k is the first index of ?-part.
  133. */
  134. outer:
  135. for (int k = less - 1; ++k <= great; ) {
  136. int ak = a[k];
  137. if (ak < pivot1) { // Move a[k] to left part
  138. a[k] = a[less];
  139. /*
  140. * Here and below we use "a[i] = b; i++;" instead
  141. * of "a[i++] = b;" due to performance issue.
  142. */
  143. a[less] = ak;
  144. ++less;
  145. } else if (ak > pivot2) { // Move a[k] to right part
  146. while (a[great] > pivot2) {
  147. if (great-- == k) {
  148. break outer;
  149. }
  150. }
  151. if (a[great] < pivot1) { // a[great] <= pivot2
  152. a[k] = a[less];
  153. a[less] = a[great];
  154. ++less;
  155. } else { // pivot1 <= a[great] <= pivot2
  156. a[k] = a[great];
  157. }
  158. /*
  159. * Here and below we use "a[i] = b; i--;" instead
  160. * of "a[i--] = b;" due to performance issue.
  161. */
  162. a[great] = ak;
  163. --great;
  164. }
  165. }
  166. // Swap pivots into their final positions
  167. a[left] = a[less - 1]; a[less - 1] = pivot1;
  168. a[right] = a[great + 1]; a[great + 1] = pivot2;
  169. // Sort left and right parts recursively, excluding known pivots
  170. sort(a, left, less - 2, leftmost);
  171. sort(a, great + 2, right, false);
  172. /*
  173. * If center part is too large (comprises > 4/7 of the array),
  174. * swap internal pivot values to ends.
  175. */
  176. if (less < e1 && e5 < great) {
  177. /*
  178. * Skip elements, which are equal to pivot values.
  179. */
  180. while (a[less] == pivot1) {
  181. ++less;
  182. }
  183. while (a[great] == pivot2) {
  184. --great;
  185. }
  186. /*
  187. * Partitioning:
  188. *
  189. * left part center part right part
  190. * +----------------------------------------------------------+
  191. * | == pivot1 | pivot1 < && < pivot2 | ? | == pivot2 |
  192. * +----------------------------------------------------------+
  193. * ^ ^ ^
  194. * | | |
  195. * less k great
  196. *
  197. * Invariants:
  198. *
  199. * all in (*, less) == pivot1
  200. * pivot1 < all in [less, k) < pivot2
  201. * all in (great, *) == pivot2
  202. *
  203. * Pointer k is the first index of ?-part.
  204. */
  205. outer:
  206. for (int k = less - 1; ++k <= great; ) {
  207. int ak = a[k];
  208. if (ak == pivot1) { // Move a[k] to left part
  209. a[k] = a[less];
  210. a[less] = ak;
  211. ++less;
  212. } else if (ak == pivot2) { // Move a[k] to right part
  213. while (a[great] == pivot2) {
  214. if (great-- == k) {
  215. break outer;
  216. }
  217. }
  218. if (a[great] == pivot1) { // a[great] < pivot2
  219. a[k] = a[less];
  220. /*
  221. * Even though a[great] equals to pivot1, the
  222. * assignment a[less] = pivot1 may be incorrect,
  223. * if a[great] and pivot1 are floating-point zeros
  224. * of different signs. Therefore in float and
  225. * double sorting methods we have to use more
  226. * accurate assignment a[less] = a[great].
  227. */
  228. a[less] = pivot1;
  229. ++less;
  230. } else { // pivot1 < a[great] < pivot2
  231. a[k] = a[great];
  232. }
  233. a[great] = ak;
  234. --great;
  235. }
  236. }
  237. }
  238. // Sort center part recursively
  239. sort(a, less, great, false);
  240. } else { // Partitioning with one pivot
  241. /*
  242. * Use the third of the five sorted elements as pivot.
  243. * This value is inexpensive approximation of the median.
  244. */
  245. int pivot = a[e3];
  246. /*
  247. * Partitioning degenerates to the traditional 3-way
  248. * (or "Dutch National Flag") schema:
  249. *
  250. * left part center part right part
  251. * +-------------------------------------------------+
  252. * | < pivot | == pivot | ? | > pivot |
  253. * +-------------------------------------------------+
  254. * ^ ^ ^
  255. * | | |
  256. * less k great
  257. *
  258. * Invariants:
  259. *
  260. * all in (left, less) < pivot
  261. * all in [less, k) == pivot
  262. * all in (great, right) > pivot
  263. *
  264. * Pointer k is the first index of ?-part.
  265. */
  266. for (int k = less; k <= great; ++k) {
  267. if (a[k] == pivot) {
  268. continue;
  269. }
  270. int ak = a[k];
  271. if (ak < pivot) { // Move a[k] to left part
  272. a[k] = a[less];
  273. a[less] = ak;
  274. ++less;
  275. } else { // a[k] > pivot - Move a[k] to right part
  276. while (a[great] > pivot) {
  277. --great;
  278. }
  279. if (a[great] < pivot) { // a[great] <= pivot
  280. a[k] = a[less];
  281. a[less] = a[great];
  282. ++less;
  283. } else { // a[great] == pivot
  284. /*
  285. * Even though a[great] equals to pivot, the
  286. * assignment a[k] = pivot may be incorrect,
  287. * if a[great] and pivot are floating-point
  288. * zeros of different signs. Therefore in float
  289. * and double sorting methods we have to use
  290. * more accurate assignment a[k] = a[great].
  291. */
  292. a[k] = pivot;
  293. }
  294. a[great] = ak;
  295. --great;
  296. }
  297. }
  298. /*
  299. * Sort left and right parts recursively.
  300. * All elements from center part are equal
  301. * and, therefore, already sorted.
  302. */
  303. sort(a, left, less - 1, leftmost);
  304. sort(a, great + 1, right, false);
  305. }
  306. }