DualPivotQuicksort源码

x33g5p2x  于2021-12-30 转载在 其他  
字(60.4k)|赞(0)|评价(0)|浏览(419)
  1. package java.util;
  2. /** * This class implements the Dual-Pivot Quicksort algorithm by * Vladimir Yaroslavskiy, Jon Bentley, and Josh Bloch. The algorithm * offers O(n log(n)) performance on many data sets that cause other * quicksorts to degrade to quadratic performance, and is typically * faster than traditional (one-pivot) Quicksort implementations. * * All exposed methods are package-private, designed to be invoked * from public methods (in class Arrays) after performing any * necessary array bounds checks and expanding parameters into the * required forms. * * @author Vladimir Yaroslavskiy * @author Jon Bentley * @author Josh Bloch * * @version 2011.02.11 m765.827.12i:5\7pm * @since 1.7 */
  3. final class DualPivotQuicksort {
  4. /** * Prevents instantiation. */
  5. private DualPivotQuicksort() {}
  6. /* * Tuning parameters. */
  7. /** * The maximum number of runs in merge sort. */
  8. private static final int MAX_RUN_COUNT = 67;
  9. /** * The maximum length of run in merge sort. */
  10. private static final int MAX_RUN_LENGTH = 33;
  11. /** * If the length of an array to be sorted is less than this * constant, Quicksort is used in preference to merge sort. */
  12. private static final int QUICKSORT_THRESHOLD = 286;
  13. /** * If the length of an array to be sorted is less than this * constant, insertion sort is used in preference to Quicksort. */
  14. private static final int INSERTION_SORT_THRESHOLD = 47;
  15. /** * If the length of a byte array to be sorted is greater than this * constant, counting sort is used in preference to insertion sort. */
  16. private static final int COUNTING_SORT_THRESHOLD_FOR_BYTE = 29;
  17. /** * If the length of a short or char array to be sorted is greater * than this constant, counting sort is used in preference to Quicksort. */
  18. private static final int COUNTING_SORT_THRESHOLD_FOR_SHORT_OR_CHAR = 3200;
  19. /* * Sorting methods for seven primitive types. */
  20. /** * Sorts the specified range of the array using the given * workspace array slice if possible for merging * * @param a the array to be sorted * @param left the index of the first element, inclusive, to be sorted * @param right the index of the last element, inclusive, to be sorted * @param work a workspace array (slice) * @param workBase origin of usable space in work array * @param workLen usable size of work array */
  21. static void sort(int[] a, int left, int right,
  22. int[] work, int workBase, int workLen) {
  23. // Use Quicksort on small arrays
  24. if (right - left < QUICKSORT_THRESHOLD) {
  25. sort(a, left, right, true);
  26. return;
  27. }
  28. /* * Index run[i] is the start of i-th run * (ascending or descending sequence). */
  29. int[] run = new int[MAX_RUN_COUNT + 1];
  30. int count = 0; run[0] = left;
  31. // Check if the array is nearly sorted
  32. for (int k = left; k < right; run[count] = k) {
  33. if (a[k] < a[k + 1]) { // ascending
  34. while (++k <= right && a[k - 1] <= a[k]);
  35. } else if (a[k] > a[k + 1]) { // descending
  36. while (++k <= right && a[k - 1] >= a[k]);
  37. for (int lo = run[count] - 1, hi = k; ++lo < --hi; ) {
  38. int t = a[lo]; a[lo] = a[hi]; a[hi] = t;
  39. }
  40. } else { // equal
  41. for (int m = MAX_RUN_LENGTH; ++k <= right && a[k - 1] == a[k]; ) {
  42. if (--m == 0) {
  43. sort(a, left, right, true);
  44. return;
  45. }
  46. }
  47. }
  48. /* * The array is not highly structured, * use Quicksort instead of merge sort. */
  49. if (++count == MAX_RUN_COUNT) {
  50. sort(a, left, right, true);
  51. return;
  52. }
  53. }
  54. // Check special cases
  55. // Implementation note: variable "right" is increased by 1.
  56. if (run[count] == right++) { // The last run contains one element
  57. run[++count] = right;
  58. } else if (count == 1) { // The array is already sorted
  59. return;
  60. }
  61. // Determine alternation base for merge
  62. byte odd = 0;
  63. for (int n = 1; (n <<= 1) < count; odd ^= 1);
  64. // Use or create temporary array b for merging
  65. int[] b; // temp array; alternates with a
  66. int ao, bo; // array offsets from 'left'
  67. int blen = right - left; // space needed for b
  68. if (work == null || workLen < blen || workBase + blen > work.length) {
  69. work = new int[blen];
  70. workBase = 0;
  71. }
  72. if (odd == 0) {
  73. System.arraycopy(a, left, work, workBase, blen);
  74. b = a;
  75. bo = 0;
  76. a = work;
  77. ao = workBase - left;
  78. } else {
  79. b = work;
  80. ao = 0;
  81. bo = workBase - left;
  82. }
  83. // Merging
  84. for (int last; count > 1; count = last) {
  85. for (int k = (last = 0) + 2; k <= count; k += 2) {
  86. int hi = run[k], mi = run[k - 1];
  87. for (int i = run[k - 2], p = i, q = mi; i < hi; ++i) {
  88. if (q >= hi || p < mi && a[p + ao] <= a[q + ao]) {
  89. b[i + bo] = a[p++ + ao];
  90. } else {
  91. b[i + bo] = a[q++ + ao];
  92. }
  93. }
  94. run[++last] = hi;
  95. }
  96. if ((count & 1) != 0) {
  97. for (int i = right, lo = run[count - 1]; --i >= lo;
  98. b[i + bo] = a[i + ao]
  99. );
  100. run[++last] = right;
  101. }
  102. int[] t = a; a = b; b = t;
  103. int o = ao; ao = bo; bo = o;
  104. }
  105. }
  106. /** * Sorts the specified range of the array by Dual-Pivot Quicksort. * * @param a the array to be sorted * @param left the index of the first element, inclusive, to be sorted * @param right the index of the last element, inclusive, to be sorted * @param leftmost indicates if this part is the leftmost in the range */
  107. private static void sort(int[] a, int left, int right, boolean leftmost) {
  108. int length = right - left + 1;
  109. // Use insertion sort on tiny arrays
  110. if (length < INSERTION_SORT_THRESHOLD) {
  111. if (leftmost) {
  112. /* * Traditional (without sentinel) insertion sort, * optimized for server VM, is used in case of * the leftmost part. */
  113. for (int i = left, j = i; i < right; j = ++i) {
  114. int ai = a[i + 1];
  115. while (ai < a[j]) {
  116. a[j + 1] = a[j];
  117. if (j-- == left) {
  118. break;
  119. }
  120. }
  121. a[j + 1] = ai;
  122. }
  123. } else {
  124. /* * Skip the longest ascending sequence. */
  125. do {
  126. if (left >= right) {
  127. return;
  128. }
  129. } while (a[++left] >= a[left - 1]);
  130. /* * Every element from adjoining part plays the role * of sentinel, therefore this allows us to avoid the * left range check on each iteration. Moreover, we use * the more optimized algorithm, so called pair insertion * sort, which is faster (in the context of Quicksort) * than traditional implementation of insertion sort. */
  131. for (int k = left; ++left <= right; k = ++left) {
  132. int a1 = a[k], a2 = a[left];
  133. if (a1 < a2) {
  134. a2 = a1; a1 = a[left];
  135. }
  136. while (a1 < a[--k]) {
  137. a[k + 2] = a[k];
  138. }
  139. a[++k + 1] = a1;
  140. while (a2 < a[--k]) {
  141. a[k + 1] = a[k];
  142. }
  143. a[k + 1] = a2;
  144. }
  145. int last = a[right];
  146. while (last < a[--right]) {
  147. a[right + 1] = a[right];
  148. }
  149. a[right + 1] = last;
  150. }
  151. return;
  152. }
  153. // Inexpensive approximation of length / 7
  154. int seventh = (length >> 3) + (length >> 6) + 1;
  155. /* * Sort five evenly spaced elements around (and including) the * center element in the range. These elements will be used for * pivot selection as described below. The choice for spacing * these elements was empirically determined to work well on * a wide variety of inputs. */
  156. int e3 = (left + right) >>> 1; // The midpoint
  157. int e2 = e3 - seventh;
  158. int e1 = e2 - seventh;
  159. int e4 = e3 + seventh;
  160. int e5 = e4 + seventh;
  161. // Sort these elements using insertion sort
  162. if (a[e2] < a[e1]) { int t = a[e2]; a[e2] = a[e1]; a[e1] = t; }
  163. if (a[e3] < a[e2]) { int t = a[e3]; a[e3] = a[e2]; a[e2] = t;
  164. if (t < a[e1]) { a[e2] = a[e1]; a[e1] = t; }
  165. }
  166. if (a[e4] < a[e3]) { int t = a[e4]; a[e4] = a[e3]; a[e3] = t;
  167. if (t < a[e2]) { a[e3] = a[e2]; a[e2] = t;
  168. if (t < a[e1]) { a[e2] = a[e1]; a[e1] = t; }
  169. }
  170. }
  171. if (a[e5] < a[e4]) { int t = a[e5]; a[e5] = a[e4]; a[e4] = t;
  172. if (t < a[e3]) { a[e4] = a[e3]; a[e3] = t;
  173. if (t < a[e2]) { a[e3] = a[e2]; a[e2] = t;
  174. if (t < a[e1]) { a[e2] = a[e1]; a[e1] = t; }
  175. }
  176. }
  177. }
  178. // Pointers
  179. int less = left; // The index of the first element of center part
  180. int great = right; // The index before the first element of right part
  181. if (a[e1] != a[e2] && a[e2] != a[e3] && a[e3] != a[e4] && a[e4] != a[e5]) {
  182. /* * Use the second and fourth of the five sorted elements as pivots. * These values are inexpensive approximations of the first and * second terciles of the array. Note that pivot1 <= pivot2. */
  183. int pivot1 = a[e2];
  184. int pivot2 = a[e4];
  185. /* * The first and the last elements to be sorted are moved to the * locations formerly occupied by the pivots. When partitioning * is complete, the pivots are swapped back into their final * positions, and excluded from subsequent sorting. */
  186. a[e2] = a[left];
  187. a[e4] = a[right];
  188. /* * Skip elements, which are less or greater than pivot values. */
  189. while (a[++less] < pivot1);
  190. while (a[--great] > pivot2);
  191. /* * Partitioning: * * left part center part right part * +--------------------------------------------------------------+ * | < pivot1 | pivot1 <= && <= pivot2 | ? | > pivot2 | * +--------------------------------------------------------------+ * ^ ^ ^ * | | | * less k great * * Invariants: * * all in (left, less) < pivot1 * pivot1 <= all in [less, k) <= pivot2 * all in (great, right) > pivot2 * * Pointer k is the first index of ?-part. */
  192. outer:
  193. for (int k = less - 1; ++k <= great; ) {
  194. int ak = a[k];
  195. if (ak < pivot1) { // Move a[k] to left part
  196. a[k] = a[less];
  197. /* * Here and below we use "a[i] = b; i++;" instead * of "a[i++] = b;" due to performance issue. */
  198. a[less] = ak;
  199. ++less;
  200. } else if (ak > pivot2) { // Move a[k] to right part
  201. while (a[great] > pivot2) {
  202. if (great-- == k) {
  203. break outer;
  204. }
  205. }
  206. if (a[great] < pivot1) { // a[great] <= pivot2
  207. a[k] = a[less];
  208. a[less] = a[great];
  209. ++less;
  210. } else { // pivot1 <= a[great] <= pivot2
  211. a[k] = a[great];
  212. }
  213. /* * Here and below we use "a[i] = b; i--;" instead * of "a[i--] = b;" due to performance issue. */
  214. a[great] = ak;
  215. --great;
  216. }
  217. }
  218. // Swap pivots into their final positions
  219. a[left] = a[less - 1]; a[less - 1] = pivot1;
  220. a[right] = a[great + 1]; a[great + 1] = pivot2;
  221. // Sort left and right parts recursively, excluding known pivots
  222. sort(a, left, less - 2, leftmost);
  223. sort(a, great + 2, right, false);
  224. /* * If center part is too large (comprises > 4/7 of the array), * swap internal pivot values to ends. */
  225. if (less < e1 && e5 < great) {
  226. /* * Skip elements, which are equal to pivot values. */
  227. while (a[less] == pivot1) {
  228. ++less;
  229. }
  230. while (a[great] == pivot2) {
  231. --great;
  232. }
  233. /* * Partitioning: * * left part center part right part * +----------------------------------------------------------+ * | == pivot1 | pivot1 < && < pivot2 | ? | == pivot2 | * +----------------------------------------------------------+ * ^ ^ ^ * | | | * less k great * * Invariants: * * all in (*, less) == pivot1 * pivot1 < all in [less, k) < pivot2 * all in (great, *) == pivot2 * * Pointer k is the first index of ?-part. */
  234. outer:
  235. for (int k = less - 1; ++k <= great; ) {
  236. int ak = a[k];
  237. if (ak == pivot1) { // Move a[k] to left part
  238. a[k] = a[less];
  239. a[less] = ak;
  240. ++less;
  241. } else if (ak == pivot2) { // Move a[k] to right part
  242. while (a[great] == pivot2) {
  243. if (great-- == k) {
  244. break outer;
  245. }
  246. }
  247. if (a[great] == pivot1) { // a[great] < pivot2
  248. a[k] = a[less];
  249. /* * Even though a[great] equals to pivot1, the * assignment a[less] = pivot1 may be incorrect, * if a[great] and pivot1 are floating-point zeros * of different signs. Therefore in float and * double sorting methods we have to use more * accurate assignment a[less] = a[great]. */
  250. a[less] = pivot1;
  251. ++less;
  252. } else { // pivot1 < a[great] < pivot2
  253. a[k] = a[great];
  254. }
  255. a[great] = ak;
  256. --great;
  257. }
  258. }
  259. }
  260. // Sort center part recursively
  261. sort(a, less, great, false);
  262. } else { // Partitioning with one pivot
  263. /* * Use the third of the five sorted elements as pivot. * This value is inexpensive approximation of the median. */
  264. int pivot = a[e3];
  265. /* * Partitioning degenerates to the traditional 3-way * (or "Dutch National Flag") schema: * * left part center part right part * +-------------------------------------------------+ * | < pivot | == pivot | ? | > pivot | * +-------------------------------------------------+ * ^ ^ ^ * | | | * less k great * * Invariants: * * all in (left, less) < pivot * all in [less, k) == pivot * all in (great, right) > pivot * * Pointer k is the first index of ?-part. */
  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. /* * Even though a[great] equals to pivot, the * assignment a[k] = pivot may be incorrect, * if a[great] and pivot are floating-point * zeros of different signs. Therefore in float * and double sorting methods we have to use * more accurate assignment a[k] = a[great]. */
  285. a[k] = pivot;
  286. }
  287. a[great] = ak;
  288. --great;
  289. }
  290. }
  291. /* * Sort left and right parts recursively. * All elements from center part are equal * and, therefore, already sorted. */
  292. sort(a, left, less - 1, leftmost);
  293. sort(a, great + 1, right, false);
  294. }
  295. }
  296. /** * Sorts the specified range of the array using the given * workspace array slice if possible for merging * * @param a the array to be sorted * @param left the index of the first element, inclusive, to be sorted * @param right the index of the last element, inclusive, to be sorted * @param work a workspace array (slice) * @param workBase origin of usable space in work array * @param workLen usable size of work array */
  297. static void sort(long[] a, int left, int right,
  298. long[] work, int workBase, int workLen) {
  299. // Use Quicksort on small arrays
  300. if (right - left < QUICKSORT_THRESHOLD) {
  301. sort(a, left, right, true);
  302. return;
  303. }
  304. /* * Index run[i] is the start of i-th run * (ascending or descending sequence). */
  305. int[] run = new int[MAX_RUN_COUNT + 1];
  306. int count = 0; run[0] = left;
  307. // Check if the array is nearly sorted
  308. for (int k = left; k < right; run[count] = k) {
  309. if (a[k] < a[k + 1]) { // ascending
  310. while (++k <= right && a[k - 1] <= a[k]);
  311. } else if (a[k] > a[k + 1]) { // descending
  312. while (++k <= right && a[k - 1] >= a[k]);
  313. for (int lo = run[count] - 1, hi = k; ++lo < --hi; ) {
  314. long t = a[lo]; a[lo] = a[hi]; a[hi] = t;
  315. }
  316. } else { // equal
  317. for (int m = MAX_RUN_LENGTH; ++k <= right && a[k - 1] == a[k]; ) {
  318. if (--m == 0) {
  319. sort(a, left, right, true);
  320. return;
  321. }
  322. }
  323. }
  324. /* * The array is not highly structured, * use Quicksort instead of merge sort. */
  325. if (++count == MAX_RUN_COUNT) {
  326. sort(a, left, right, true);
  327. return;
  328. }
  329. }
  330. // Check special cases
  331. // Implementation note: variable "right" is increased by 1.
  332. if (run[count] == right++) { // The last run contains one element
  333. run[++count] = right;
  334. } else if (count == 1) { // The array is already sorted
  335. return;
  336. }
  337. // Determine alternation base for merge
  338. byte odd = 0;
  339. for (int n = 1; (n <<= 1) < count; odd ^= 1);
  340. // Use or create temporary array b for merging
  341. long[] b; // temp array; alternates with a
  342. int ao, bo; // array offsets from 'left'
  343. int blen = right - left; // space needed for b
  344. if (work == null || workLen < blen || workBase + blen > work.length) {
  345. work = new long[blen];
  346. workBase = 0;
  347. }
  348. if (odd == 0) {
  349. System.arraycopy(a, left, work, workBase, blen);
  350. b = a;
  351. bo = 0;
  352. a = work;
  353. ao = workBase - left;
  354. } else {
  355. b = work;
  356. ao = 0;
  357. bo = workBase - left;
  358. }
  359. // Merging
  360. for (int last; count > 1; count = last) {
  361. for (int k = (last = 0) + 2; k <= count; k += 2) {
  362. int hi = run[k], mi = run[k - 1];
  363. for (int i = run[k - 2], p = i, q = mi; i < hi; ++i) {
  364. if (q >= hi || p < mi && a[p + ao] <= a[q + ao]) {
  365. b[i + bo] = a[p++ + ao];
  366. } else {
  367. b[i + bo] = a[q++ + ao];
  368. }
  369. }
  370. run[++last] = hi;
  371. }
  372. if ((count & 1) != 0) {
  373. for (int i = right, lo = run[count - 1]; --i >= lo;
  374. b[i + bo] = a[i + ao]
  375. );
  376. run[++last] = right;
  377. }
  378. long[] t = a; a = b; b = t;
  379. int o = ao; ao = bo; bo = o;
  380. }
  381. }
  382. /** * Sorts the specified range of the array by Dual-Pivot Quicksort. * * @param a the array to be sorted * @param left the index of the first element, inclusive, to be sorted * @param right the index of the last element, inclusive, to be sorted * @param leftmost indicates if this part is the leftmost in the range */
  383. private static void sort(long[] a, int left, int right, boolean leftmost) {
  384. int length = right - left + 1;
  385. // Use insertion sort on tiny arrays
  386. if (length < INSERTION_SORT_THRESHOLD) {
  387. if (leftmost) {
  388. /* * Traditional (without sentinel) insertion sort, * optimized for server VM, is used in case of * the leftmost part. */
  389. for (int i = left, j = i; i < right; j = ++i) {
  390. long ai = a[i + 1];
  391. while (ai < a[j]) {
  392. a[j + 1] = a[j];
  393. if (j-- == left) {
  394. break;
  395. }
  396. }
  397. a[j + 1] = ai;
  398. }
  399. } else {
  400. /* * Skip the longest ascending sequence. */
  401. do {
  402. if (left >= right) {
  403. return;
  404. }
  405. } while (a[++left] >= a[left - 1]);
  406. /* * Every element from adjoining part plays the role * of sentinel, therefore this allows us to avoid the * left range check on each iteration. Moreover, we use * the more optimized algorithm, so called pair insertion * sort, which is faster (in the context of Quicksort) * than traditional implementation of insertion sort. */
  407. for (int k = left; ++left <= right; k = ++left) {
  408. long a1 = a[k], a2 = a[left];
  409. if (a1 < a2) {
  410. a2 = a1; a1 = a[left];
  411. }
  412. while (a1 < a[--k]) {
  413. a[k + 2] = a[k];
  414. }
  415. a[++k + 1] = a1;
  416. while (a2 < a[--k]) {
  417. a[k + 1] = a[k];
  418. }
  419. a[k + 1] = a2;
  420. }
  421. long last = a[right];
  422. while (last < a[--right]) {
  423. a[right + 1] = a[right];
  424. }
  425. a[right + 1] = last;
  426. }
  427. return;
  428. }
  429. // Inexpensive approximation of length / 7
  430. int seventh = (length >> 3) + (length >> 6) + 1;
  431. /* * Sort five evenly spaced elements around (and including) the * center element in the range. These elements will be used for * pivot selection as described below. The choice for spacing * these elements was empirically determined to work well on * a wide variety of inputs. */
  432. int e3 = (left + right) >>> 1; // The midpoint
  433. int e2 = e3 - seventh;
  434. int e1 = e2 - seventh;
  435. int e4 = e3 + seventh;
  436. int e5 = e4 + seventh;
  437. // Sort these elements using insertion sort
  438. if (a[e2] < a[e1]) { long t = a[e2]; a[e2] = a[e1]; a[e1] = t; }
  439. if (a[e3] < a[e2]) { long t = a[e3]; a[e3] = a[e2]; a[e2] = t;
  440. if (t < a[e1]) { a[e2] = a[e1]; a[e1] = t; }
  441. }
  442. if (a[e4] < a[e3]) { long t = a[e4]; a[e4] = a[e3]; a[e3] = t;
  443. if (t < a[e2]) { a[e3] = a[e2]; a[e2] = t;
  444. if (t < a[e1]) { a[e2] = a[e1]; a[e1] = t; }
  445. }
  446. }
  447. if (a[e5] < a[e4]) { long t = a[e5]; a[e5] = a[e4]; a[e4] = t;
  448. if (t < a[e3]) { a[e4] = a[e3]; a[e3] = t;
  449. if (t < a[e2]) { a[e3] = a[e2]; a[e2] = t;
  450. if (t < a[e1]) { a[e2] = a[e1]; a[e1] = t; }
  451. }
  452. }
  453. }
  454. // Pointers
  455. int less = left; // The index of the first element of center part
  456. int great = right; // The index before the first element of right part
  457. if (a[e1] != a[e2] && a[e2] != a[e3] && a[e3] != a[e4] && a[e4] != a[e5]) {
  458. /* * Use the second and fourth of the five sorted elements as pivots. * These values are inexpensive approximations of the first and * second terciles of the array. Note that pivot1 <= pivot2. */
  459. long pivot1 = a[e2];
  460. long pivot2 = a[e4];
  461. /* * The first and the last elements to be sorted are moved to the * locations formerly occupied by the pivots. When partitioning * is complete, the pivots are swapped back into their final * positions, and excluded from subsequent sorting. */
  462. a[e2] = a[left];
  463. a[e4] = a[right];
  464. /* * Skip elements, which are less or greater than pivot values. */
  465. while (a[++less] < pivot1);
  466. while (a[--great] > pivot2);
  467. /* * Partitioning: * * left part center part right part * +--------------------------------------------------------------+ * | < pivot1 | pivot1 <= && <= pivot2 | ? | > pivot2 | * +--------------------------------------------------------------+ * ^ ^ ^ * | | | * less k great * * Invariants: * * all in (left, less) < pivot1 * pivot1 <= all in [less, k) <= pivot2 * all in (great, right) > pivot2 * * Pointer k is the first index of ?-part. */
  468. outer:
  469. for (int k = less - 1; ++k <= great; ) {
  470. long ak = a[k];
  471. if (ak < pivot1) { // Move a[k] to left part
  472. a[k] = a[less];
  473. /* * Here and below we use "a[i] = b; i++;" instead * of "a[i++] = b;" due to performance issue. */
  474. a[less] = ak;
  475. ++less;
  476. } else if (ak > pivot2) { // Move a[k] to right part
  477. while (a[great] > pivot2) {
  478. if (great-- == k) {
  479. break outer;
  480. }
  481. }
  482. if (a[great] < pivot1) { // a[great] <= pivot2
  483. a[k] = a[less];
  484. a[less] = a[great];
  485. ++less;
  486. } else { // pivot1 <= a[great] <= pivot2
  487. a[k] = a[great];
  488. }
  489. /* * Here and below we use "a[i] = b; i--;" instead * of "a[i--] = b;" due to performance issue. */
  490. a[great] = ak;
  491. --great;
  492. }
  493. }
  494. // Swap pivots into their final positions
  495. a[left] = a[less - 1]; a[less - 1] = pivot1;
  496. a[right] = a[great + 1]; a[great + 1] = pivot2;
  497. // Sort left and right parts recursively, excluding known pivots
  498. sort(a, left, less - 2, leftmost);
  499. sort(a, great + 2, right, false);
  500. /* * If center part is too large (comprises > 4/7 of the array), * swap internal pivot values to ends. */
  501. if (less < e1 && e5 < great) {
  502. /* * Skip elements, which are equal to pivot values. */
  503. while (a[less] == pivot1) {
  504. ++less;
  505. }
  506. while (a[great] == pivot2) {
  507. --great;
  508. }
  509. /* * Partitioning: * * left part center part right part * +----------------------------------------------------------+ * | == pivot1 | pivot1 < && < pivot2 | ? | == pivot2 | * +----------------------------------------------------------+ * ^ ^ ^ * | | | * less k great * * Invariants: * * all in (*, less) == pivot1 * pivot1 < all in [less, k) < pivot2 * all in (great, *) == pivot2 * * Pointer k is the first index of ?-part. */
  510. outer:
  511. for (int k = less - 1; ++k <= great; ) {
  512. long ak = a[k];
  513. if (ak == pivot1) { // Move a[k] to left part
  514. a[k] = a[less];
  515. a[less] = ak;
  516. ++less;
  517. } else if (ak == pivot2) { // Move a[k] to right part
  518. while (a[great] == pivot2) {
  519. if (great-- == k) {
  520. break outer;
  521. }
  522. }
  523. if (a[great] == pivot1) { // a[great] < pivot2
  524. a[k] = a[less];
  525. /* * Even though a[great] equals to pivot1, the * assignment a[less] = pivot1 may be incorrect, * if a[great] and pivot1 are floating-point zeros * of different signs. Therefore in float and * double sorting methods we have to use more * accurate assignment a[less] = a[great]. */
  526. a[less] = pivot1;
  527. ++less;
  528. } else { // pivot1 < a[great] < pivot2
  529. a[k] = a[great];
  530. }
  531. a[great] = ak;
  532. --great;
  533. }
  534. }
  535. }
  536. // Sort center part recursively
  537. sort(a, less, great, false);
  538. } else { // Partitioning with one pivot
  539. /* * Use the third of the five sorted elements as pivot. * This value is inexpensive approximation of the median. */
  540. long pivot = a[e3];
  541. /* * Partitioning degenerates to the traditional 3-way * (or "Dutch National Flag") schema: * * left part center part right part * +-------------------------------------------------+ * | < pivot | == pivot | ? | > pivot | * +-------------------------------------------------+ * ^ ^ ^ * | | | * less k great * * Invariants: * * all in (left, less) < pivot * all in [less, k) == pivot * all in (great, right) > pivot * * Pointer k is the first index of ?-part. */
  542. for (int k = less; k <= great; ++k) {
  543. if (a[k] == pivot) {
  544. continue;
  545. }
  546. long ak = a[k];
  547. if (ak < pivot) { // Move a[k] to left part
  548. a[k] = a[less];
  549. a[less] = ak;
  550. ++less;
  551. } else { // a[k] > pivot - Move a[k] to right part
  552. while (a[great] > pivot) {
  553. --great;
  554. }
  555. if (a[great] < pivot) { // a[great] <= pivot
  556. a[k] = a[less];
  557. a[less] = a[great];
  558. ++less;
  559. } else { // a[great] == pivot
  560. /* * Even though a[great] equals to pivot, the * assignment a[k] = pivot may be incorrect, * if a[great] and pivot are floating-point * zeros of different signs. Therefore in float * and double sorting methods we have to use * more accurate assignment a[k] = a[great]. */
  561. a[k] = pivot;
  562. }
  563. a[great] = ak;
  564. --great;
  565. }
  566. }
  567. /* * Sort left and right parts recursively. * All elements from center part are equal * and, therefore, already sorted. */
  568. sort(a, left, less - 1, leftmost);
  569. sort(a, great + 1, right, false);
  570. }
  571. }
  572. /** * Sorts the specified range of the array using the given * workspace array slice if possible for merging * * @param a the array to be sorted * @param left the index of the first element, inclusive, to be sorted * @param right the index of the last element, inclusive, to be sorted * @param work a workspace array (slice) * @param workBase origin of usable space in work array * @param workLen usable size of work array */
  573. static void sort(short[] a, int left, int right,
  574. short[] work, int workBase, int workLen) {
  575. // Use counting sort on large arrays
  576. if (right - left > COUNTING_SORT_THRESHOLD_FOR_SHORT_OR_CHAR) {
  577. int[] count = new int[NUM_SHORT_VALUES];
  578. for (int i = left - 1; ++i <= right;
  579. count[a[i] - Short.MIN_VALUE]++
  580. );
  581. for (int i = NUM_SHORT_VALUES, k = right + 1; k > left; ) {
  582. while (count[--i] == 0);
  583. short value = (short) (i + Short.MIN_VALUE);
  584. int s = count[i];
  585. do {
  586. a[--k] = value;
  587. } while (--s > 0);
  588. }
  589. } else { // Use Dual-Pivot Quicksort on small arrays
  590. doSort(a, left, right, work, workBase, workLen);
  591. }
  592. }
  593. /** The number of distinct short values. */
  594. private static final int NUM_SHORT_VALUES = 1 << 16;
  595. /** * Sorts the specified range of the array. * * @param a the array to be sorted * @param left the index of the first element, inclusive, to be sorted * @param right the index of the last element, inclusive, to be sorted * @param work a workspace array (slice) * @param workBase origin of usable space in work array * @param workLen usable size of work array */
  596. private static void doSort(short[] a, int left, int right,
  597. short[] work, int workBase, int workLen) {
  598. // Use Quicksort on small arrays
  599. if (right - left < QUICKSORT_THRESHOLD) {
  600. sort(a, left, right, true);
  601. return;
  602. }
  603. /* * Index run[i] is the start of i-th run * (ascending or descending sequence). */
  604. int[] run = new int[MAX_RUN_COUNT + 1];
  605. int count = 0; run[0] = left;
  606. // Check if the array is nearly sorted
  607. for (int k = left; k < right; run[count] = k) {
  608. if (a[k] < a[k + 1]) { // ascending
  609. while (++k <= right && a[k - 1] <= a[k]);
  610. } else if (a[k] > a[k + 1]) { // descending
  611. while (++k <= right && a[k - 1] >= a[k]);
  612. for (int lo = run[count] - 1, hi = k; ++lo < --hi; ) {
  613. short t = a[lo]; a[lo] = a[hi]; a[hi] = t;
  614. }
  615. } else { // equal
  616. for (int m = MAX_RUN_LENGTH; ++k <= right && a[k - 1] == a[k]; ) {
  617. if (--m == 0) {
  618. sort(a, left, right, true);
  619. return;
  620. }
  621. }
  622. }
  623. /* * The array is not highly structured, * use Quicksort instead of merge sort. */
  624. if (++count == MAX_RUN_COUNT) {
  625. sort(a, left, right, true);
  626. return;
  627. }
  628. }
  629. // Check special cases
  630. // Implementation note: variable "right" is increased by 1.
  631. if (run[count] == right++) { // The last run contains one element
  632. run[++count] = right;
  633. } else if (count == 1) { // The array is already sorted
  634. return;
  635. }
  636. // Determine alternation base for merge
  637. byte odd = 0;
  638. for (int n = 1; (n <<= 1) < count; odd ^= 1);
  639. // Use or create temporary array b for merging
  640. short[] b; // temp array; alternates with a
  641. int ao, bo; // array offsets from 'left'
  642. int blen = right - left; // space needed for b
  643. if (work == null || workLen < blen || workBase + blen > work.length) {
  644. work = new short[blen];
  645. workBase = 0;
  646. }
  647. if (odd == 0) {
  648. System.arraycopy(a, left, work, workBase, blen);
  649. b = a;
  650. bo = 0;
  651. a = work;
  652. ao = workBase - left;
  653. } else {
  654. b = work;
  655. ao = 0;
  656. bo = workBase - left;
  657. }
  658. // Merging
  659. for (int last; count > 1; count = last) {
  660. for (int k = (last = 0) + 2; k <= count; k += 2) {
  661. int hi = run[k], mi = run[k - 1];
  662. for (int i = run[k - 2], p = i, q = mi; i < hi; ++i) {
  663. if (q >= hi || p < mi && a[p + ao] <= a[q + ao]) {
  664. b[i + bo] = a[p++ + ao];
  665. } else {
  666. b[i + bo] = a[q++ + ao];
  667. }
  668. }
  669. run[++last] = hi;
  670. }
  671. if ((count & 1) != 0) {
  672. for (int i = right, lo = run[count - 1]; --i >= lo;
  673. b[i + bo] = a[i + ao]
  674. );
  675. run[++last] = right;
  676. }
  677. short[] t = a; a = b; b = t;
  678. int o = ao; ao = bo; bo = o;
  679. }
  680. }
  681. /** * Sorts the specified range of the array by Dual-Pivot Quicksort. * * @param a the array to be sorted * @param left the index of the first element, inclusive, to be sorted * @param right the index of the last element, inclusive, to be sorted * @param leftmost indicates if this part is the leftmost in the range */
  682. private static void sort(short[] a, int left, int right, boolean leftmost) {
  683. int length = right - left + 1;
  684. // Use insertion sort on tiny arrays
  685. if (length < INSERTION_SORT_THRESHOLD) {
  686. if (leftmost) {
  687. /* * Traditional (without sentinel) insertion sort, * optimized for server VM, is used in case of * the leftmost part. */
  688. for (int i = left, j = i; i < right; j = ++i) {
  689. short ai = a[i + 1];
  690. while (ai < a[j]) {
  691. a[j + 1] = a[j];
  692. if (j-- == left) {
  693. break;
  694. }
  695. }
  696. a[j + 1] = ai;
  697. }
  698. } else {
  699. /* * Skip the longest ascending sequence. */
  700. do {
  701. if (left >= right) {
  702. return;
  703. }
  704. } while (a[++left] >= a[left - 1]);
  705. /* * Every element from adjoining part plays the role * of sentinel, therefore this allows us to avoid the * left range check on each iteration. Moreover, we use * the more optimized algorithm, so called pair insertion * sort, which is faster (in the context of Quicksort) * than traditional implementation of insertion sort. */
  706. for (int k = left; ++left <= right; k = ++left) {
  707. short a1 = a[k], a2 = a[left];
  708. if (a1 < a2) {
  709. a2 = a1; a1 = a[left];
  710. }
  711. while (a1 < a[--k]) {
  712. a[k + 2] = a[k];
  713. }
  714. a[++k + 1] = a1;
  715. while (a2 < a[--k]) {
  716. a[k + 1] = a[k];
  717. }
  718. a[k + 1] = a2;
  719. }
  720. short last = a[right];
  721. while (last < a[--right]) {
  722. a[right + 1] = a[right];
  723. }
  724. a[right + 1] = last;
  725. }
  726. return;
  727. }
  728. // Inexpensive approximation of length / 7
  729. int seventh = (length >> 3) + (length >> 6) + 1;
  730. /* * Sort five evenly spaced elements around (and including) the * center element in the range. These elements will be used for * pivot selection as described below. The choice for spacing * these elements was empirically determined to work well on * a wide variety of inputs. */
  731. int e3 = (left + right) >>> 1; // The midpoint
  732. int e2 = e3 - seventh;
  733. int e1 = e2 - seventh;
  734. int e4 = e3 + seventh;
  735. int e5 = e4 + seventh;
  736. // Sort these elements using insertion sort
  737. if (a[e2] < a[e1]) { short t = a[e2]; a[e2] = a[e1]; a[e1] = t; }
  738. if (a[e3] < a[e2]) { short t = a[e3]; a[e3] = a[e2]; a[e2] = t;
  739. if (t < a[e1]) { a[e2] = a[e1]; a[e1] = t; }
  740. }
  741. if (a[e4] < a[e3]) { short t = a[e4]; a[e4] = a[e3]; a[e3] = t;
  742. if (t < a[e2]) { a[e3] = a[e2]; a[e2] = t;
  743. if (t < a[e1]) { a[e2] = a[e1]; a[e1] = t; }
  744. }
  745. }
  746. if (a[e5] < a[e4]) { short t = a[e5]; a[e5] = a[e4]; a[e4] = t;
  747. if (t < a[e3]) { a[e4] = a[e3]; a[e3] = t;
  748. if (t < a[e2]) { a[e3] = a[e2]; a[e2] = t;
  749. if (t < a[e1]) { a[e2] = a[e1]; a[e1] = t; }
  750. }
  751. }
  752. }
  753. // Pointers
  754. int less = left; // The index of the first element of center part
  755. int great = right; // The index before the first element of right part
  756. if (a[e1] != a[e2] && a[e2] != a[e3] && a[e3] != a[e4] && a[e4] != a[e5]) {
  757. /* * Use the second and fourth of the five sorted elements as pivots. * These values are inexpensive approximations of the first and * second terciles of the array. Note that pivot1 <= pivot2. */
  758. short pivot1 = a[e2];
  759. short pivot2 = a[e4];
  760. /* * The first and the last elements to be sorted are moved to the * locations formerly occupied by the pivots. When partitioning * is complete, the pivots are swapped back into their final * positions, and excluded from subsequent sorting. */
  761. a[e2] = a[left];
  762. a[e4] = a[right];
  763. /* * Skip elements, which are less or greater than pivot values. */
  764. while (a[++less] < pivot1);
  765. while (a[--great] > pivot2);
  766. /* * Partitioning: * * left part center part right part * +--------------------------------------------------------------+ * | < pivot1 | pivot1 <= && <= pivot2 | ? | > pivot2 | * +--------------------------------------------------------------+ * ^ ^ ^ * | | | * less k great * * Invariants: * * all in (left, less) < pivot1 * pivot1 <= all in [less, k) <= pivot2 * all in (great, right) > pivot2 * * Pointer k is the first index of ?-part. */
  767. outer:
  768. for (int k = less - 1; ++k <= great; ) {
  769. short ak = a[k];
  770. if (ak < pivot1) { // Move a[k] to left part
  771. a[k] = a[less];
  772. /* * Here and below we use "a[i] = b; i++;" instead * of "a[i++] = b;" due to performance issue. */
  773. a[less] = ak;
  774. ++less;
  775. } else if (ak > pivot2) { // Move a[k] to right part
  776. while (a[great] > pivot2) {
  777. if (great-- == k) {
  778. break outer;
  779. }
  780. }
  781. if (a[great] < pivot1) { // a[great] <= pivot2
  782. a[k] = a[less];
  783. a[less] = a[great];
  784. ++less;
  785. } else { // pivot1 <= a[great] <= pivot2
  786. a[k] = a[great];
  787. }
  788. /* * Here and below we use "a[i] = b; i--;" instead * of "a[i--] = b;" due to performance issue. */
  789. a[great] = ak;
  790. --great;
  791. }
  792. }
  793. // Swap pivots into their final positions
  794. a[left] = a[less - 1]; a[less - 1] = pivot1;
  795. a[right] = a[great + 1]; a[great + 1] = pivot2;
  796. // Sort left and right parts recursively, excluding known pivots
  797. sort(a, left, less - 2, leftmost);
  798. sort(a, great + 2, right, false);
  799. /* * If center part is too large (comprises > 4/7 of the array), * swap internal pivot values to ends. */
  800. if (less < e1 && e5 < great) {
  801. /* * Skip elements, which are equal to pivot values. */
  802. while (a[less] == pivot1) {
  803. ++less;
  804. }
  805. while (a[great] == pivot2) {
  806. --great;
  807. }
  808. /* * Partitioning: * * left part center part right part * +----------------------------------------------------------+ * | == pivot1 | pivot1 < && < pivot2 | ? | == pivot2 | * +----------------------------------------------------------+ * ^ ^ ^ * | | | * less k great * * Invariants: * * all in (*, less) == pivot1 * pivot1 < all in [less, k) < pivot2 * all in (great, *) == pivot2 * * Pointer k is the first index of ?-part. */
  809. outer:
  810. for (int k = less - 1; ++k <= great; ) {
  811. short ak = a[k];
  812. if (ak == pivot1) { // Move a[k] to left part
  813. a[k] = a[less];
  814. a[less] = ak;
  815. ++less;
  816. } else if (ak == pivot2) { // Move a[k] to right part
  817. while (a[great] == pivot2) {
  818. if (great-- == k) {
  819. break outer;
  820. }
  821. }
  822. if (a[great] == pivot1) { // a[great] < pivot2
  823. a[k] = a[less];
  824. /* * Even though a[great] equals to pivot1, the * assignment a[less] = pivot1 may be incorrect, * if a[great] and pivot1 are floating-point zeros * of different signs. Therefore in float and * double sorting methods we have to use more * accurate assignment a[less] = a[great]. */
  825. a[less] = pivot1;
  826. ++less;
  827. } else { // pivot1 < a[great] < pivot2
  828. a[k] = a[great];
  829. }
  830. a[great] = ak;
  831. --great;
  832. }
  833. }
  834. }
  835. // Sort center part recursively
  836. sort(a, less, great, false);
  837. } else { // Partitioning with one pivot
  838. /* * Use the third of the five sorted elements as pivot. * This value is inexpensive approximation of the median. */
  839. short pivot = a[e3];
  840. /* * Partitioning degenerates to the traditional 3-way * (or "Dutch National Flag") schema: * * left part center part right part * +-------------------------------------------------+ * | < pivot | == pivot | ? | > pivot | * +-------------------------------------------------+ * ^ ^ ^ * | | | * less k great * * Invariants: * * all in (left, less) < pivot * all in [less, k) == pivot * all in (great, right) > pivot * * Pointer k is the first index of ?-part. */
  841. for (int k = less; k <= great; ++k) {
  842. if (a[k] == pivot) {
  843. continue;
  844. }
  845. short ak = a[k];
  846. if (ak < pivot) { // Move a[k] to left part
  847. a[k] = a[less];
  848. a[less] = ak;
  849. ++less;
  850. } else { // a[k] > pivot - Move a[k] to right part
  851. while (a[great] > pivot) {
  852. --great;
  853. }
  854. if (a[great] < pivot) { // a[great] <= pivot
  855. a[k] = a[less];
  856. a[less] = a[great];
  857. ++less;
  858. } else { // a[great] == pivot
  859. /* * Even though a[great] equals to pivot, the * assignment a[k] = pivot may be incorrect, * if a[great] and pivot are floating-point * zeros of different signs. Therefore in float * and double sorting methods we have to use * more accurate assignment a[k] = a[great]. */
  860. a[k] = pivot;
  861. }
  862. a[great] = ak;
  863. --great;
  864. }
  865. }
  866. /* * Sort left and right parts recursively. * All elements from center part are equal * and, therefore, already sorted. */
  867. sort(a, left, less - 1, leftmost);
  868. sort(a, great + 1, right, false);
  869. }
  870. }
  871. /** * Sorts the specified range of the array using the given * workspace array slice if possible for merging * * @param a the array to be sorted * @param left the index of the first element, inclusive, to be sorted * @param right the index of the last element, inclusive, to be sorted * @param work a workspace array (slice) * @param workBase origin of usable space in work array * @param workLen usable size of work array */
  872. static void sort(char[] a, int left, int right,
  873. char[] work, int workBase, int workLen) {
  874. // Use counting sort on large arrays
  875. if (right - left > COUNTING_SORT_THRESHOLD_FOR_SHORT_OR_CHAR) {
  876. int[] count = new int[NUM_CHAR_VALUES];
  877. for (int i = left - 1; ++i <= right;
  878. count[a[i]]++
  879. );
  880. for (int i = NUM_CHAR_VALUES, k = right + 1; k > left; ) {
  881. while (count[--i] == 0);
  882. char value = (char) i;
  883. int s = count[i];
  884. do {
  885. a[--k] = value;
  886. } while (--s > 0);
  887. }
  888. } else { // Use Dual-Pivot Quicksort on small arrays
  889. doSort(a, left, right, work, workBase, workLen);
  890. }
  891. }
  892. /** The number of distinct char values. */
  893. private static final int NUM_CHAR_VALUES = 1 << 16;
  894. /** * Sorts the specified range of the array. * * @param a the array to be sorted * @param left the index of the first element, inclusive, to be sorted * @param right the index of the last element, inclusive, to be sorted * @param work a workspace array (slice) * @param workBase origin of usable space in work array * @param workLen usable size of work array */
  895. private static void doSort(char[] a, int left, int right,
  896. char[] work, int workBase, int workLen) {
  897. // Use Quicksort on small arrays
  898. if (right - left < QUICKSORT_THRESHOLD) {
  899. sort(a, left, right, true);
  900. return;
  901. }
  902. /* * Index run[i] is the start of i-th run * (ascending or descending sequence). */
  903. int[] run = new int[MAX_RUN_COUNT + 1];
  904. int count = 0; run[0] = left;
  905. // Check if the array is nearly sorted
  906. for (int k = left; k < right; run[count] = k) {
  907. if (a[k] < a[k + 1]) { // ascending
  908. while (++k <= right && a[k - 1] <= a[k]);
  909. } else if (a[k] > a[k + 1]) { // descending
  910. while (++k <= right && a[k - 1] >= a[k]);
  911. for (int lo = run[count] - 1, hi = k; ++lo < --hi; ) {
  912. char t = a[lo]; a[lo] = a[hi]; a[hi] = t;
  913. }
  914. } else { // equal
  915. for (int m = MAX_RUN_LENGTH; ++k <= right && a[k - 1] == a[k]; ) {
  916. if (--m == 0) {
  917. sort(a, left, right, true);
  918. return;
  919. }
  920. }
  921. }
  922. /* * The array is not highly structured, * use Quicksort instead of merge sort. */
  923. if (++count == MAX_RUN_COUNT) {
  924. sort(a, left, right, true);
  925. return;
  926. }
  927. }
  928. // Check special cases
  929. // Implementation note: variable "right" is increased by 1.
  930. if (run[count] == right++) { // The last run contains one element
  931. run[++count] = right;
  932. } else if (count == 1) { // The array is already sorted
  933. return;
  934. }
  935. // Determine alternation base for merge
  936. byte odd = 0;
  937. for (int n = 1; (n <<= 1) < count; odd ^= 1);
  938. // Use or create temporary array b for merging
  939. char[] b; // temp array; alternates with a
  940. int ao, bo; // array offsets from 'left'
  941. int blen = right - left; // space needed for b
  942. if (work == null || workLen < blen || workBase + blen > work.length) {
  943. work = new char[blen];
  944. workBase = 0;
  945. }
  946. if (odd == 0) {
  947. System.arraycopy(a, left, work, workBase, blen);
  948. b = a;
  949. bo = 0;
  950. a = work;
  951. ao = workBase - left;
  952. } else {
  953. b = work;
  954. ao = 0;
  955. bo = workBase - left;
  956. }
  957. // Merging
  958. for (int last; count > 1; count = last) {
  959. for (int k = (last = 0) + 2; k <= count; k += 2) {
  960. int hi = run[k], mi = run[k - 1];
  961. for (int i = run[k - 2], p = i, q = mi; i < hi; ++i) {
  962. if (q >= hi || p < mi && a[p + ao] <= a[q + ao]) {
  963. b[i + bo] = a[p++ + ao];
  964. } else {
  965. b[i + bo] = a[q++ + ao];
  966. }
  967. }
  968. run[++last] = hi;
  969. }
  970. if ((count & 1) != 0) {
  971. for (int i = right, lo = run[count - 1]; --i >= lo;
  972. b[i + bo] = a[i + ao]
  973. );
  974. run[++last] = right;
  975. }
  976. char[] t = a; a = b; b = t;
  977. int o = ao; ao = bo; bo = o;
  978. }
  979. }
  980. /** * Sorts the specified range of the array by Dual-Pivot Quicksort. * * @param a the array to be sorted * @param left the index of the first element, inclusive, to be sorted * @param right the index of the last element, inclusive, to be sorted * @param leftmost indicates if this part is the leftmost in the range */
  981. private static void sort(char[] a, int left, int right, boolean leftmost) {
  982. int length = right - left + 1;
  983. // Use insertion sort on tiny arrays
  984. if (length < INSERTION_SORT_THRESHOLD) {
  985. if (leftmost) {
  986. /* * Traditional (without sentinel) insertion sort, * optimized for server VM, is used in case of * the leftmost part. */
  987. for (int i = left, j = i; i < right; j = ++i) {
  988. char ai = a[i + 1];
  989. while (ai < a[j]) {
  990. a[j + 1] = a[j];
  991. if (j-- == left) {
  992. break;
  993. }
  994. }
  995. a[j + 1] = ai;
  996. }
  997. } else {
  998. /* * Skip the longest ascending sequence. */
  999. do {
  1000. if (left >= right) {
  1001. return;
  1002. }
  1003. } while (a[++left] >= a[left - 1]);
  1004. /* * Every element from adjoining part plays the role * of sentinel, therefore this allows us to avoid the * left range check on each iteration. Moreover, we use * the more optimized algorithm, so called pair insertion * sort, which is faster (in the context of Quicksort) * than traditional implementation of insertion sort. */
  1005. for (int k = left; ++left <= right; k = ++left) {
  1006. char a1 = a[k], a2 = a[left];
  1007. if (a1 < a2) {
  1008. a2 = a1; a1 = a[left];
  1009. }
  1010. while (a1 < a[--k]) {
  1011. a[k + 2] = a[k];
  1012. }
  1013. a[++k + 1] = a1;
  1014. while (a2 < a[--k]) {
  1015. a[k + 1] = a[k];
  1016. }
  1017. a[k + 1] = a2;
  1018. }
  1019. char last = a[right];
  1020. while (last < a[--right]) {
  1021. a[right + 1] = a[right];
  1022. }
  1023. a[right + 1] = last;
  1024. }
  1025. return;
  1026. }
  1027. // Inexpensive approximation of length / 7
  1028. int seventh = (length >> 3) + (length >> 6) + 1;
  1029. /* * Sort five evenly spaced elements around (and including) the * center element in the range. These elements will be used for * pivot selection as described below. The choice for spacing * these elements was empirically determined to work well on * a wide variety of inputs. */
  1030. int e3 = (left + right) >>> 1; // The midpoint
  1031. int e2 = e3 - seventh;
  1032. int e1 = e2 - seventh;
  1033. int e4 = e3 + seventh;
  1034. int e5 = e4 + seventh;
  1035. // Sort these elements using insertion sort
  1036. if (a[e2] < a[e1]) { char t = a[e2]; a[e2] = a[e1]; a[e1] = t; }
  1037. if (a[e3] < a[e2]) { char t = a[e3]; a[e3] = a[e2]; a[e2] = t;
  1038. if (t < a[e1]) { a[e2] = a[e1]; a[e1] = t; }
  1039. }
  1040. if (a[e4] < a[e3]) { char t = a[e4]; a[e4] = a[e3]; a[e3] = t;
  1041. if (t < a[e2]) { a[e3] = a[e2]; a[e2] = t;
  1042. if (t < a[e1]) { a[e2] = a[e1]; a[e1] = t; }
  1043. }
  1044. }
  1045. if (a[e5] < a[e4]) { char t = a[e5]; a[e5] = a[e4]; a[e4] = t;
  1046. if (t < a[e3]) { a[e4] = a[e3]; a[e3] = t;
  1047. if (t < a[e2]) { a[e3] = a[e2]; a[e2] = t;
  1048. if (t < a[e1]) { a[e2] = a[e1]; a[e1] = t; }
  1049. }
  1050. }
  1051. }
  1052. // Pointers
  1053. int less = left; // The index of the first element of center part
  1054. int great = right; // The index before the first element of right part
  1055. if (a[e1] != a[e2] && a[e2] != a[e3] && a[e3] != a[e4] && a[e4] != a[e5]) {
  1056. /* * Use the second and fourth of the five sorted elements as pivots. * These values are inexpensive approximations of the first and * second terciles of the array. Note that pivot1 <= pivot2. */
  1057. char pivot1 = a[e2];
  1058. char pivot2 = a[e4];
  1059. /* * The first and the last elements to be sorted are moved to the * locations formerly occupied by the pivots. When partitioning * is complete, the pivots are swapped back into their final * positions, and excluded from subsequent sorting. */
  1060. a[e2] = a[left];
  1061. a[e4] = a[right];
  1062. /* * Skip elements, which are less or greater than pivot values. */
  1063. while (a[++less] < pivot1);
  1064. while (a[--great] > pivot2);
  1065. /* * Partitioning: * * left part center part right part * +--------------------------------------------------------------+ * | < pivot1 | pivot1 <= && <= pivot2 | ? | > pivot2 | * +--------------------------------------------------------------+ * ^ ^ ^ * | | | * less k great * * Invariants: * * all in (left, less) < pivot1 * pivot1 <= all in [less, k) <= pivot2 * all in (great, right) > pivot2 * * Pointer k is the first index of ?-part. */
  1066. outer:
  1067. for (int k = less - 1; ++k <= great; ) {
  1068. char ak = a[k];
  1069. if (ak < pivot1) { // Move a[k] to left part
  1070. a[k] = a[less];
  1071. /* * Here and below we use "a[i] = b; i++;" instead * of "a[i++] = b;" due to performance issue. */
  1072. a[less] = ak;
  1073. ++less;
  1074. } else if (ak > pivot2) { // Move a[k] to right part
  1075. while (a[great] > pivot2) {
  1076. if (great-- == k) {
  1077. break outer;
  1078. }
  1079. }
  1080. if (a[great] < pivot1) { // a[great] <= pivot2
  1081. a[k] = a[less];
  1082. a[less] = a[great];
  1083. ++less;
  1084. } else { // pivot1 <= a[great] <= pivot2
  1085. a[k] = a[great];
  1086. }
  1087. /* * Here and below we use "a[i] = b; i--;" instead * of "a[i--] = b;" due to performance issue. */
  1088. a[great] = ak;
  1089. --great;
  1090. }
  1091. }
  1092. // Swap pivots into their final positions
  1093. a[left] = a[less - 1]; a[less - 1] = pivot1;
  1094. a[right] = a[great + 1]; a[great + 1] = pivot2;
  1095. // Sort left and right parts recursively, excluding known pivots
  1096. sort(a, left, less - 2, leftmost);
  1097. sort(a, great + 2, right, false);
  1098. /* * If center part is too large (comprises > 4/7 of the array), * swap internal pivot values to ends. */
  1099. if (less < e1 && e5 < great) {
  1100. /* * Skip elements, which are equal to pivot values. */
  1101. while (a[less] == pivot1) {
  1102. ++less;
  1103. }
  1104. while (a[great] == pivot2) {
  1105. --great;
  1106. }
  1107. /* * Partitioning: * * left part center part right part * +----------------------------------------------------------+ * | == pivot1 | pivot1 < && < pivot2 | ? | == pivot2 | * +----------------------------------------------------------+ * ^ ^ ^ * | | | * less k great * * Invariants: * * all in (*, less) == pivot1 * pivot1 < all in [less, k) < pivot2 * all in (great, *) == pivot2 * * Pointer k is the first index of ?-part. */
  1108. outer:
  1109. for (int k = less - 1; ++k <= great; ) {
  1110. char ak = a[k];
  1111. if (ak == pivot1) { // Move a[k] to left part
  1112. a[k] = a[less];
  1113. a[less] = ak;
  1114. ++less;
  1115. } else if (ak == pivot2) { // Move a[k] to right part
  1116. while (a[great] == pivot2) {
  1117. if (great-- == k) {
  1118. break outer;
  1119. }
  1120. }
  1121. if (a[great] == pivot1) { // a[great] < pivot2
  1122. a[k] = a[less];
  1123. /* * Even though a[great] equals to pivot1, the * assignment a[less] = pivot1 may be incorrect, * if a[great] and pivot1 are floating-point zeros * of different signs. Therefore in float and * double sorting methods we have to use more * accurate assignment a[less] = a[great]. */
  1124. a[less] = pivot1;
  1125. ++less;
  1126. } else { // pivot1 < a[great] < pivot2
  1127. a[k] = a[great];
  1128. }
  1129. a[great] = ak;
  1130. --great;
  1131. }
  1132. }
  1133. }
  1134. // Sort center part recursively
  1135. sort(a, less, great, false);
  1136. } else { // Partitioning with one pivot
  1137. /* * Use the third of the five sorted elements as pivot. * This value is inexpensive approximation of the median. */
  1138. char pivot = a[e3];
  1139. /* * Partitioning degenerates to the traditional 3-way * (or "Dutch National Flag") schema: * * left part center part right part * +-------------------------------------------------+ * | < pivot | == pivot | ? | > pivot | * +-------------------------------------------------+ * ^ ^ ^ * | | | * less k great * * Invariants: * * all in (left, less) < pivot * all in [less, k) == pivot * all in (great, right) > pivot * * Pointer k is the first index of ?-part. */
  1140. for (int k = less; k <= great; ++k) {
  1141. if (a[k] == pivot) {
  1142. continue;
  1143. }
  1144. char ak = a[k];
  1145. if (ak < pivot) { // Move a[k] to left part
  1146. a[k] = a[less];
  1147. a[less] = ak;
  1148. ++less;
  1149. } else { // a[k] > pivot - Move a[k] to right part
  1150. while (a[great] > pivot) {
  1151. --great;
  1152. }
  1153. if (a[great] < pivot) { // a[great] <= pivot
  1154. a[k] = a[less];
  1155. a[less] = a[great];
  1156. ++less;
  1157. } else { // a[great] == pivot
  1158. /* * Even though a[great] equals to pivot, the * assignment a[k] = pivot may be incorrect, * if a[great] and pivot are floating-point * zeros of different signs. Therefore in float * and double sorting methods we have to use * more accurate assignment a[k] = a[great]. */
  1159. a[k] = pivot;
  1160. }
  1161. a[great] = ak;
  1162. --great;
  1163. }
  1164. }
  1165. /* * Sort left and right parts recursively. * All elements from center part are equal * and, therefore, already sorted. */
  1166. sort(a, left, less - 1, leftmost);
  1167. sort(a, great + 1, right, false);
  1168. }
  1169. }
  1170. /** The number of distinct byte values. */
  1171. private static final int NUM_BYTE_VALUES = 1 << 8;
  1172. /** * Sorts the specified range of the array. * * @param a the array to be sorted * @param left the index of the first element, inclusive, to be sorted * @param right the index of the last element, inclusive, to be sorted */
  1173. static void sort(byte[] a, int left, int right) {
  1174. // Use counting sort on large arrays
  1175. if (right - left > COUNTING_SORT_THRESHOLD_FOR_BYTE) {
  1176. int[] count = new int[NUM_BYTE_VALUES];
  1177. for (int i = left - 1; ++i <= right;
  1178. count[a[i] - Byte.MIN_VALUE]++
  1179. );
  1180. for (int i = NUM_BYTE_VALUES, k = right + 1; k > left; ) {
  1181. while (count[--i] == 0);
  1182. byte value = (byte) (i + Byte.MIN_VALUE);
  1183. int s = count[i];
  1184. do {
  1185. a[--k] = value;
  1186. } while (--s > 0);
  1187. }
  1188. } else { // Use insertion sort on small arrays
  1189. for (int i = left, j = i; i < right; j = ++i) {
  1190. byte ai = a[i + 1];
  1191. while (ai < a[j]) {
  1192. a[j + 1] = a[j];
  1193. if (j-- == left) {
  1194. break;
  1195. }
  1196. }
  1197. a[j + 1] = ai;
  1198. }
  1199. }
  1200. }
  1201. /** * Sorts the specified range of the array using the given * workspace array slice if possible for merging * * @param a the array to be sorted * @param left the index of the first element, inclusive, to be sorted * @param right the index of the last element, inclusive, to be sorted * @param work a workspace array (slice) * @param workBase origin of usable space in work array * @param workLen usable size of work array */
  1202. static void sort(float[] a, int left, int right,
  1203. float[] work, int workBase, int workLen) {
  1204. /* * Phase 1: Move NaNs to the end of the array. */
  1205. while (left <= right && Float.isNaN(a[right])) {
  1206. --right;
  1207. }
  1208. for (int k = right; --k >= left; ) {
  1209. float ak = a[k];
  1210. if (ak != ak) { // a[k] is NaN
  1211. a[k] = a[right];
  1212. a[right] = ak;
  1213. --right;
  1214. }
  1215. }
  1216. /* * Phase 2: Sort everything except NaNs (which are already in place). */
  1217. doSort(a, left, right, work, workBase, workLen);
  1218. /* * Phase 3: Place negative zeros before positive zeros. */
  1219. int hi = right;
  1220. /* * Find the first zero, or first positive, or last negative element. */
  1221. while (left < hi) {
  1222. int middle = (left + hi) >>> 1;
  1223. float middleValue = a[middle];
  1224. if (middleValue < 0.0f) {
  1225. left = middle + 1;
  1226. } else {
  1227. hi = middle;
  1228. }
  1229. }
  1230. /* * Skip the last negative value (if any) or all leading negative zeros. */
  1231. while (left <= right && Float.floatToRawIntBits(a[left]) < 0) {
  1232. ++left;
  1233. }
  1234. /* * Move negative zeros to the beginning of the sub-range. * * Partitioning: * * +----------------------------------------------------+ * | < 0.0 | -0.0 | 0.0 | ? ( >= 0.0 ) | * +----------------------------------------------------+ * ^ ^ ^ * | | | * left p k * * Invariants: * * all in (*, left) < 0.0 * all in [left, p) == -0.0 * all in [p, k) == 0.0 * all in [k, right] >= 0.0 * * Pointer k is the first index of ?-part. */
  1235. for (int k = left, p = left - 1; ++k <= right; ) {
  1236. float ak = a[k];
  1237. if (ak != 0.0f) {
  1238. break;
  1239. }
  1240. if (Float.floatToRawIntBits(ak) < 0) { // ak is -0.0f
  1241. a[k] = 0.0f;
  1242. a[++p] = -0.0f;
  1243. }
  1244. }
  1245. }
  1246. /** * Sorts the specified range of the array. * * @param a the array to be sorted * @param left the index of the first element, inclusive, to be sorted * @param right the index of the last element, inclusive, to be sorted * @param work a workspace array (slice) * @param workBase origin of usable space in work array * @param workLen usable size of work array */
  1247. private static void doSort(float[] a, int left, int right,
  1248. float[] work, int workBase, int workLen) {
  1249. // Use Quicksort on small arrays
  1250. if (right - left < QUICKSORT_THRESHOLD) {
  1251. sort(a, left, right, true);
  1252. return;
  1253. }
  1254. /* * Index run[i] is the start of i-th run * (ascending or descending sequence). */
  1255. int[] run = new int[MAX_RUN_COUNT + 1];
  1256. int count = 0; run[0] = left;
  1257. // Check if the array is nearly sorted
  1258. for (int k = left; k < right; run[count] = k) {
  1259. if (a[k] < a[k + 1]) { // ascending
  1260. while (++k <= right && a[k - 1] <= a[k]);
  1261. } else if (a[k] > a[k + 1]) { // descending
  1262. while (++k <= right && a[k - 1] >= a[k]);
  1263. for (int lo = run[count] - 1, hi = k; ++lo < --hi; ) {
  1264. float t = a[lo]; a[lo] = a[hi]; a[hi] = t;
  1265. }
  1266. } else { // equal
  1267. for (int m = MAX_RUN_LENGTH; ++k <= right && a[k - 1] == a[k]; ) {
  1268. if (--m == 0) {
  1269. sort(a, left, right, true);
  1270. return;
  1271. }
  1272. }
  1273. }
  1274. /* * The array is not highly structured, * use Quicksort instead of merge sort. */
  1275. if (++count == MAX_RUN_COUNT) {
  1276. sort(a, left, right, true);
  1277. return;
  1278. }
  1279. }
  1280. // Check special cases
  1281. // Implementation note: variable "right" is increased by 1.
  1282. if (run[count] == right++) { // The last run contains one element
  1283. run[++count] = right;
  1284. } else if (count == 1) { // The array is already sorted
  1285. return;
  1286. }
  1287. // Determine alternation base for merge
  1288. byte odd = 0;
  1289. for (int n = 1; (n <<= 1) < count; odd ^= 1);
  1290. // Use or create temporary array b for merging
  1291. float[] b; // temp array; alternates with a
  1292. int ao, bo; // array offsets from 'left'
  1293. int blen = right - left; // space needed for b
  1294. if (work == null || workLen < blen || workBase + blen > work.length) {
  1295. work = new float[blen];
  1296. workBase = 0;
  1297. }
  1298. if (odd == 0) {
  1299. System.arraycopy(a, left, work, workBase, blen);
  1300. b = a;
  1301. bo = 0;
  1302. a = work;
  1303. ao = workBase - left;
  1304. } else {
  1305. b = work;
  1306. ao = 0;
  1307. bo = workBase - left;
  1308. }
  1309. // Merging
  1310. for (int last; count > 1; count = last) {
  1311. for (int k = (last = 0) + 2; k <= count; k += 2) {
  1312. int hi = run[k], mi = run[k - 1];
  1313. for (int i = run[k - 2], p = i, q = mi; i < hi; ++i) {
  1314. if (q >= hi || p < mi && a[p + ao] <= a[q + ao]) {
  1315. b[i + bo] = a[p++ + ao];
  1316. } else {
  1317. b[i + bo] = a[q++ + ao];
  1318. }
  1319. }
  1320. run[++last] = hi;
  1321. }
  1322. if ((count & 1) != 0) {
  1323. for (int i = right, lo = run[count - 1]; --i >= lo;
  1324. b[i + bo] = a[i + ao]
  1325. );
  1326. run[++last] = right;
  1327. }
  1328. float[] t = a; a = b; b = t;
  1329. int o = ao; ao = bo; bo = o;
  1330. }
  1331. }
  1332. /** * Sorts the specified range of the array by Dual-Pivot Quicksort. * * @param a the array to be sorted * @param left the index of the first element, inclusive, to be sorted * @param right the index of the last element, inclusive, to be sorted * @param leftmost indicates if this part is the leftmost in the range */
  1333. private static void sort(float[] a, int left, int right, boolean leftmost) {
  1334. int length = right - left + 1;
  1335. // Use insertion sort on tiny arrays
  1336. if (length < INSERTION_SORT_THRESHOLD) {
  1337. if (leftmost) {
  1338. /* * Traditional (without sentinel) insertion sort, * optimized for server VM, is used in case of * the leftmost part. */
  1339. for (int i = left, j = i; i < right; j = ++i) {
  1340. float ai = a[i + 1];
  1341. while (ai < a[j]) {
  1342. a[j + 1] = a[j];
  1343. if (j-- == left) {
  1344. break;
  1345. }
  1346. }
  1347. a[j + 1] = ai;
  1348. }
  1349. } else {
  1350. /* * Skip the longest ascending sequence. */
  1351. do {
  1352. if (left >= right) {
  1353. return;
  1354. }
  1355. } while (a[++left] >= a[left - 1]);
  1356. /* * Every element from adjoining part plays the role * of sentinel, therefore this allows us to avoid the * left range check on each iteration. Moreover, we use * the more optimized algorithm, so called pair insertion * sort, which is faster (in the context of Quicksort) * than traditional implementation of insertion sort. */
  1357. for (int k = left; ++left <= right; k = ++left) {
  1358. float a1 = a[k], a2 = a[left];
  1359. if (a1 < a2) {
  1360. a2 = a1; a1 = a[left];
  1361. }
  1362. while (a1 < a[--k]) {
  1363. a[k + 2] = a[k];
  1364. }
  1365. a[++k + 1] = a1;
  1366. while (a2 < a[--k]) {
  1367. a[k + 1] = a[k];
  1368. }
  1369. a[k + 1] = a2;
  1370. }
  1371. float last = a[right];
  1372. while (last < a[--right]) {
  1373. a[right + 1] = a[right];
  1374. }
  1375. a[right + 1] = last;
  1376. }
  1377. return;
  1378. }
  1379. // Inexpensive approximation of length / 7
  1380. int seventh = (length >> 3) + (length >> 6) + 1;
  1381. /* * Sort five evenly spaced elements around (and including) the * center element in the range. These elements will be used for * pivot selection as described below. The choice for spacing * these elements was empirically determined to work well on * a wide variety of inputs. */
  1382. int e3 = (left + right) >>> 1; // The midpoint
  1383. int e2 = e3 - seventh;
  1384. int e1 = e2 - seventh;
  1385. int e4 = e3 + seventh;
  1386. int e5 = e4 + seventh;
  1387. // Sort these elements using insertion sort
  1388. if (a[e2] < a[e1]) { float t = a[e2]; a[e2] = a[e1]; a[e1] = t; }
  1389. if (a[e3] < a[e2]) { float t = a[e3]; a[e3] = a[e2]; a[e2] = t;
  1390. if (t < a[e1]) { a[e2] = a[e1]; a[e1] = t; }
  1391. }
  1392. if (a[e4] < a[e3]) { float t = a[e4]; a[e4] = a[e3]; a[e3] = t;
  1393. if (t < a[e2]) { a[e3] = a[e2]; a[e2] = t;
  1394. if (t < a[e1]) { a[e2] = a[e1]; a[e1] = t; }
  1395. }
  1396. }
  1397. if (a[e5] < a[e4]) { float t = a[e5]; a[e5] = a[e4]; a[e4] = t;
  1398. if (t < a[e3]) { a[e4] = a[e3]; a[e3] = t;
  1399. if (t < a[e2]) { a[e3] = a[e2]; a[e2] = t;
  1400. if (t < a[e1]) { a[e2] = a[e1]; a[e1] = t; }
  1401. }
  1402. }
  1403. }
  1404. // Pointers
  1405. int less = left; // The index of the first element of center part
  1406. int great = right; // The index before the first element of right part
  1407. if (a[e1] != a[e2] && a[e2] != a[e3] && a[e3] != a[e4] && a[e4] != a[e5]) {
  1408. /* * Use the second and fourth of the five sorted elements as pivots. * These values are inexpensive approximations of the first and * second terciles of the array. Note that pivot1 <= pivot2. */
  1409. float pivot1 = a[e2];
  1410. float pivot2 = a[e4];
  1411. /* * The first and the last elements to be sorted are moved to the * locations formerly occupied by the pivots. When partitioning * is complete, the pivots are swapped back into their final * positions, and excluded from subsequent sorting. */
  1412. a[e2] = a[left];
  1413. a[e4] = a[right];
  1414. /* * Skip elements, which are less or greater than pivot values. */
  1415. while (a[++less] < pivot1);
  1416. while (a[--great] > pivot2);
  1417. /* * Partitioning: * * left part center part right part * +--------------------------------------------------------------+ * | < pivot1 | pivot1 <= && <= pivot2 | ? | > pivot2 | * +--------------------------------------------------------------+ * ^ ^ ^ * | | | * less k great * * Invariants: * * all in (left, less) < pivot1 * pivot1 <= all in [less, k) <= pivot2 * all in (great, right) > pivot2 * * Pointer k is the first index of ?-part. */
  1418. outer:
  1419. for (int k = less - 1; ++k <= great; ) {
  1420. float ak = a[k];
  1421. if (ak < pivot1) { // Move a[k] to left part
  1422. a[k] = a[less];
  1423. /* * Here and below we use "a[i] = b; i++;" instead * of "a[i++] = b;" due to performance issue. */
  1424. a[less] = ak;
  1425. ++less;
  1426. } else if (ak > pivot2) { // Move a[k] to right part
  1427. while (a[great] > pivot2) {
  1428. if (great-- == k) {
  1429. break outer;
  1430. }
  1431. }
  1432. if (a[great] < pivot1) { // a[great] <= pivot2
  1433. a[k] = a[less];
  1434. a[less] = a[great];
  1435. ++less;
  1436. } else { // pivot1 <= a[great] <= pivot2
  1437. a[k] = a[great];
  1438. }
  1439. /* * Here and below we use "a[i] = b; i--;" instead * of "a[i--] = b;" due to performance issue. */
  1440. a[great] = ak;
  1441. --great;
  1442. }
  1443. }
  1444. // Swap pivots into their final positions
  1445. a[left] = a[less - 1]; a[less - 1] = pivot1;
  1446. a[right] = a[great + 1]; a[great + 1] = pivot2;
  1447. // Sort left and right parts recursively, excluding known pivots
  1448. sort(a, left, less - 2, leftmost);
  1449. sort(a, great + 2, right, false);
  1450. /* * If center part is too large (comprises > 4/7 of the array), * swap internal pivot values to ends. */
  1451. if (less < e1 && e5 < great) {
  1452. /* * Skip elements, which are equal to pivot values. */
  1453. while (a[less] == pivot1) {
  1454. ++less;
  1455. }
  1456. while (a[great] == pivot2) {
  1457. --great;
  1458. }
  1459. /* * Partitioning: * * left part center part right part * +----------------------------------------------------------+ * | == pivot1 | pivot1 < && < pivot2 | ? | == pivot2 | * +----------------------------------------------------------+ * ^ ^ ^ * | | | * less k great * * Invariants: * * all in (*, less) == pivot1 * pivot1 < all in [less, k) < pivot2 * all in (great, *) == pivot2 * * Pointer k is the first index of ?-part. */
  1460. outer:
  1461. for (int k = less - 1; ++k <= great; ) {
  1462. float ak = a[k];
  1463. if (ak == pivot1) { // Move a[k] to left part
  1464. a[k] = a[less];
  1465. a[less] = ak;
  1466. ++less;
  1467. } else if (ak == pivot2) { // Move a[k] to right part
  1468. while (a[great] == pivot2) {
  1469. if (great-- == k) {
  1470. break outer;
  1471. }
  1472. }
  1473. if (a[great] == pivot1) { // a[great] < pivot2
  1474. a[k] = a[less];
  1475. /* * Even though a[great] equals to pivot1, the * assignment a[less] = pivot1 may be incorrect, * if a[great] and pivot1 are floating-point zeros * of different signs. Therefore in float and * double sorting methods we have to use more * accurate assignment a[less] = a[great]. */
  1476. a[less] = a[great];
  1477. ++less;
  1478. } else { // pivot1 < a[great] < pivot2
  1479. a[k] = a[great];
  1480. }
  1481. a[great] = ak;
  1482. --great;
  1483. }
  1484. }
  1485. }
  1486. // Sort center part recursively
  1487. sort(a, less, great, false);
  1488. } else { // Partitioning with one pivot
  1489. /* * Use the third of the five sorted elements as pivot. * This value is inexpensive approximation of the median. */
  1490. float pivot = a[e3];
  1491. /* * Partitioning degenerates to the traditional 3-way * (or "Dutch National Flag") schema: * * left part center part right part * +-------------------------------------------------+ * | < pivot | == pivot | ? | > pivot | * +-------------------------------------------------+ * ^ ^ ^ * | | | * less k great * * Invariants: * * all in (left, less) < pivot * all in [less, k) == pivot * all in (great, right) > pivot * * Pointer k is the first index of ?-part. */
  1492. for (int k = less; k <= great; ++k) {
  1493. if (a[k] == pivot) {
  1494. continue;
  1495. }
  1496. float ak = a[k];
  1497. if (ak < pivot) { // Move a[k] to left part
  1498. a[k] = a[less];
  1499. a[less] = ak;
  1500. ++less;
  1501. } else { // a[k] > pivot - Move a[k] to right part
  1502. while (a[great] > pivot) {
  1503. --great;
  1504. }
  1505. if (a[great] < pivot) { // a[great] <= pivot
  1506. a[k] = a[less];
  1507. a[less] = a[great];
  1508. ++less;
  1509. } else { // a[great] == pivot
  1510. /* * Even though a[great] equals to pivot, the * assignment a[k] = pivot may be incorrect, * if a[great] and pivot are floating-point * zeros of different signs. Therefore in float * and double sorting methods we have to use * more accurate assignment a[k] = a[great]. */
  1511. a[k] = a[great];
  1512. }
  1513. a[great] = ak;
  1514. --great;
  1515. }
  1516. }
  1517. /* * Sort left and right parts recursively. * All elements from center part are equal * and, therefore, already sorted. */
  1518. sort(a, left, less - 1, leftmost);
  1519. sort(a, great + 1, right, false);
  1520. }
  1521. }
  1522. /** * Sorts the specified range of the array using the given * workspace array slice if possible for merging * * @param a the array to be sorted * @param left the index of the first element, inclusive, to be sorted * @param right the index of the last element, inclusive, to be sorted * @param work a workspace array (slice) * @param workBase origin of usable space in work array * @param workLen usable size of work array */
  1523. static void sort(double[] a, int left, int right,
  1524. double[] work, int workBase, int workLen) {
  1525. /* * Phase 1: Move NaNs to the end of the array. */
  1526. while (left <= right && Double.isNaN(a[right])) {
  1527. --right;
  1528. }
  1529. for (int k = right; --k >= left; ) {
  1530. double ak = a[k];
  1531. if (ak != ak) { // a[k] is NaN
  1532. a[k] = a[right];
  1533. a[right] = ak;
  1534. --right;
  1535. }
  1536. }
  1537. /* * Phase 2: Sort everything except NaNs (which are already in place). */
  1538. doSort(a, left, right, work, workBase, workLen);
  1539. /* * Phase 3: Place negative zeros before positive zeros. */
  1540. int hi = right;
  1541. /* * Find the first zero, or first positive, or last negative element. */
  1542. while (left < hi) {
  1543. int middle = (left + hi) >>> 1;
  1544. double middleValue = a[middle];
  1545. if (middleValue < 0.0d) {
  1546. left = middle + 1;
  1547. } else {
  1548. hi = middle;
  1549. }
  1550. }
  1551. /* * Skip the last negative value (if any) or all leading negative zeros. */
  1552. while (left <= right && Double.doubleToRawLongBits(a[left]) < 0) {
  1553. ++left;
  1554. }
  1555. /* * Move negative zeros to the beginning of the sub-range. * * Partitioning: * * +----------------------------------------------------+ * | < 0.0 | -0.0 | 0.0 | ? ( >= 0.0 ) | * +----------------------------------------------------+ * ^ ^ ^ * | | | * left p k * * Invariants: * * all in (*, left) < 0.0 * all in [left, p) == -0.0 * all in [p, k) == 0.0 * all in [k, right] >= 0.0 * * Pointer k is the first index of ?-part. */
  1556. for (int k = left, p = left - 1; ++k <= right; ) {
  1557. double ak = a[k];
  1558. if (ak != 0.0d) {
  1559. break;
  1560. }
  1561. if (Double.doubleToRawLongBits(ak) < 0) { // ak is -0.0d
  1562. a[k] = 0.0d;
  1563. a[++p] = -0.0d;
  1564. }
  1565. }
  1566. }
  1567. /** * Sorts the specified range of the array. * * @param a the array to be sorted * @param left the index of the first element, inclusive, to be sorted * @param right the index of the last element, inclusive, to be sorted * @param work a workspace array (slice) * @param workBase origin of usable space in work array * @param workLen usable size of work array */
  1568. private static void doSort(double[] a, int left, int right,
  1569. double[] work, int workBase, int workLen) {
  1570. // Use Quicksort on small arrays
  1571. if (right - left < QUICKSORT_THRESHOLD) {
  1572. sort(a, left, right, true);
  1573. return;
  1574. }
  1575. /* * Index run[i] is the start of i-th run * (ascending or descending sequence). */
  1576. int[] run = new int[MAX_RUN_COUNT + 1];
  1577. int count = 0; run[0] = left;
  1578. // Check if the array is nearly sorted
  1579. for (int k = left; k < right; run[count] = k) {
  1580. if (a[k] < a[k + 1]) { // ascending
  1581. while (++k <= right && a[k - 1] <= a[k]);
  1582. } else if (a[k] > a[k + 1]) { // descending
  1583. while (++k <= right && a[k - 1] >= a[k]);
  1584. for (int lo = run[count] - 1, hi = k; ++lo < --hi; ) {
  1585. double t = a[lo]; a[lo] = a[hi]; a[hi] = t;
  1586. }
  1587. } else { // equal
  1588. for (int m = MAX_RUN_LENGTH; ++k <= right && a[k - 1] == a[k]; ) {
  1589. if (--m == 0) {
  1590. sort(a, left, right, true);
  1591. return;
  1592. }
  1593. }
  1594. }
  1595. /* * The array is not highly structured, * use Quicksort instead of merge sort. */
  1596. if (++count == MAX_RUN_COUNT) {
  1597. sort(a, left, right, true);
  1598. return;
  1599. }
  1600. }
  1601. // Check special cases
  1602. // Implementation note: variable "right" is increased by 1.
  1603. if (run[count] == right++) { // The last run contains one element
  1604. run[++count] = right;
  1605. } else if (count == 1) { // The array is already sorted
  1606. return;
  1607. }
  1608. // Determine alternation base for merge
  1609. byte odd = 0;
  1610. for (int n = 1; (n <<= 1) < count; odd ^= 1);
  1611. // Use or create temporary array b for merging
  1612. double[] b; // temp array; alternates with a
  1613. int ao, bo; // array offsets from 'left'
  1614. int blen = right - left; // space needed for b
  1615. if (work == null || workLen < blen || workBase + blen > work.length) {
  1616. work = new double[blen];
  1617. workBase = 0;
  1618. }
  1619. if (odd == 0) {
  1620. System.arraycopy(a, left, work, workBase, blen);
  1621. b = a;
  1622. bo = 0;
  1623. a = work;
  1624. ao = workBase - left;
  1625. } else {
  1626. b = work;
  1627. ao = 0;
  1628. bo = workBase - left;
  1629. }
  1630. // Merging
  1631. for (int last; count > 1; count = last) {
  1632. for (int k = (last = 0) + 2; k <= count; k += 2) {
  1633. int hi = run[k], mi = run[k - 1];
  1634. for (int i = run[k - 2], p = i, q = mi; i < hi; ++i) {
  1635. if (q >= hi || p < mi && a[p + ao] <= a[q + ao]) {
  1636. b[i + bo] = a[p++ + ao];
  1637. } else {
  1638. b[i + bo] = a[q++ + ao];
  1639. }
  1640. }
  1641. run[++last] = hi;
  1642. }
  1643. if ((count & 1) != 0) {
  1644. for (int i = right, lo = run[count - 1]; --i >= lo;
  1645. b[i + bo] = a[i + ao]
  1646. );
  1647. run[++last] = right;
  1648. }
  1649. double[] t = a; a = b; b = t;
  1650. int o = ao; ao = bo; bo = o;
  1651. }
  1652. }
  1653. /** * Sorts the specified range of the array by Dual-Pivot Quicksort. * * @param a the array to be sorted * @param left the index of the first element, inclusive, to be sorted * @param right the index of the last element, inclusive, to be sorted * @param leftmost indicates if this part is the leftmost in the range */
  1654. private static void sort(double[] a, int left, int right, boolean leftmost) {
  1655. int length = right - left + 1;
  1656. // Use insertion sort on tiny arrays
  1657. if (length < INSERTION_SORT_THRESHOLD) {
  1658. if (leftmost) {
  1659. /* * Traditional (without sentinel) insertion sort, * optimized for server VM, is used in case of * the leftmost part. */
  1660. for (int i = left, j = i; i < right; j = ++i) {
  1661. double ai = a[i + 1];
  1662. while (ai < a[j]) {
  1663. a[j + 1] = a[j];
  1664. if (j-- == left) {
  1665. break;
  1666. }
  1667. }
  1668. a[j + 1] = ai;
  1669. }
  1670. } else {
  1671. /* * Skip the longest ascending sequence. */
  1672. do {
  1673. if (left >= right) {
  1674. return;
  1675. }
  1676. } while (a[++left] >= a[left - 1]);
  1677. /* * Every element from adjoining part plays the role * of sentinel, therefore this allows us to avoid the * left range check on each iteration. Moreover, we use * the more optimized algorithm, so called pair insertion * sort, which is faster (in the context of Quicksort) * than traditional implementation of insertion sort. */
  1678. for (int k = left; ++left <= right; k = ++left) {
  1679. double a1 = a[k], a2 = a[left];
  1680. if (a1 < a2) {
  1681. a2 = a1; a1 = a[left];
  1682. }
  1683. while (a1 < a[--k]) {
  1684. a[k + 2] = a[k];
  1685. }
  1686. a[++k + 1] = a1;
  1687. while (a2 < a[--k]) {
  1688. a[k + 1] = a[k];
  1689. }
  1690. a[k + 1] = a2;
  1691. }
  1692. double last = a[right];
  1693. while (last < a[--right]) {
  1694. a[right + 1] = a[right];
  1695. }
  1696. a[right + 1] = last;
  1697. }
  1698. return;
  1699. }
  1700. // Inexpensive approximation of length / 7
  1701. int seventh = (length >> 3) + (length >> 6) + 1;
  1702. /* * Sort five evenly spaced elements around (and including) the * center element in the range. These elements will be used for * pivot selection as described below. The choice for spacing * these elements was empirically determined to work well on * a wide variety of inputs. */
  1703. int e3 = (left + right) >>> 1; // The midpoint
  1704. int e2 = e3 - seventh;
  1705. int e1 = e2 - seventh;
  1706. int e4 = e3 + seventh;
  1707. int e5 = e4 + seventh;
  1708. // Sort these elements using insertion sort
  1709. if (a[e2] < a[e1]) { double t = a[e2]; a[e2] = a[e1]; a[e1] = t; }
  1710. if (a[e3] < a[e2]) { double t = a[e3]; a[e3] = a[e2]; a[e2] = t;
  1711. if (t < a[e1]) { a[e2] = a[e1]; a[e1] = t; }
  1712. }
  1713. if (a[e4] < a[e3]) { double t = a[e4]; a[e4] = a[e3]; a[e3] = t;
  1714. if (t < a[e2]) { a[e3] = a[e2]; a[e2] = t;
  1715. if (t < a[e1]) { a[e2] = a[e1]; a[e1] = t; }
  1716. }
  1717. }
  1718. if (a[e5] < a[e4]) { double t = a[e5]; a[e5] = a[e4]; a[e4] = t;
  1719. if (t < a[e3]) { a[e4] = a[e3]; a[e3] = t;
  1720. if (t < a[e2]) { a[e3] = a[e2]; a[e2] = t;
  1721. if (t < a[e1]) { a[e2] = a[e1]; a[e1] = t; }
  1722. }
  1723. }
  1724. }
  1725. // Pointers
  1726. int less = left; // The index of the first element of center part
  1727. int great = right; // The index before the first element of right part
  1728. if (a[e1] != a[e2] && a[e2] != a[e3] && a[e3] != a[e4] && a[e4] != a[e5]) {
  1729. /* * Use the second and fourth of the five sorted elements as pivots. * These values are inexpensive approximations of the first and * second terciles of the array. Note that pivot1 <= pivot2. */
  1730. double pivot1 = a[e2];
  1731. double pivot2 = a[e4];
  1732. /* * The first and the last elements to be sorted are moved to the * locations formerly occupied by the pivots. When partitioning * is complete, the pivots are swapped back into their final * positions, and excluded from subsequent sorting. */
  1733. a[e2] = a[left];
  1734. a[e4] = a[right];
  1735. /* * Skip elements, which are less or greater than pivot values. */
  1736. while (a[++less] < pivot1);
  1737. while (a[--great] > pivot2);
  1738. /* * Partitioning: * * left part center part right part * +--------------------------------------------------------------+ * | < pivot1 | pivot1 <= && <= pivot2 | ? | > pivot2 | * +--------------------------------------------------------------+ * ^ ^ ^ * | | | * less k great * * Invariants: * * all in (left, less) < pivot1 * pivot1 <= all in [less, k) <= pivot2 * all in (great, right) > pivot2 * * Pointer k is the first index of ?-part. */
  1739. outer:
  1740. for (int k = less - 1; ++k <= great; ) {
  1741. double ak = a[k];
  1742. if (ak < pivot1) { // Move a[k] to left part
  1743. a[k] = a[less];
  1744. /* * Here and below we use "a[i] = b; i++;" instead * of "a[i++] = b;" due to performance issue. */
  1745. a[less] = ak;
  1746. ++less;
  1747. } else if (ak > pivot2) { // Move a[k] to right part
  1748. while (a[great] > pivot2) {
  1749. if (great-- == k) {
  1750. break outer;
  1751. }
  1752. }
  1753. if (a[great] < pivot1) { // a[great] <= pivot2
  1754. a[k] = a[less];
  1755. a[less] = a[great];
  1756. ++less;
  1757. } else { // pivot1 <= a[great] <= pivot2
  1758. a[k] = a[great];
  1759. }
  1760. /* * Here and below we use "a[i] = b; i--;" instead * of "a[i--] = b;" due to performance issue. */
  1761. a[great] = ak;
  1762. --great;
  1763. }
  1764. }
  1765. // Swap pivots into their final positions
  1766. a[left] = a[less - 1]; a[less - 1] = pivot1;
  1767. a[right] = a[great + 1]; a[great + 1] = pivot2;
  1768. // Sort left and right parts recursively, excluding known pivots
  1769. sort(a, left, less - 2, leftmost);
  1770. sort(a, great + 2, right, false);
  1771. /* * If center part is too large (comprises > 4/7 of the array), * swap internal pivot values to ends. */
  1772. if (less < e1 && e5 < great) {
  1773. /* * Skip elements, which are equal to pivot values. */
  1774. while (a[less] == pivot1) {
  1775. ++less;
  1776. }
  1777. while (a[great] == pivot2) {
  1778. --great;
  1779. }
  1780. /* * Partitioning: * * left part center part right part * +----------------------------------------------------------+ * | == pivot1 | pivot1 < && < pivot2 | ? | == pivot2 | * +----------------------------------------------------------+ * ^ ^ ^ * | | | * less k great * * Invariants: * * all in (*, less) == pivot1 * pivot1 < all in [less, k) < pivot2 * all in (great, *) == pivot2 * * Pointer k is the first index of ?-part. */
  1781. outer:
  1782. for (int k = less - 1; ++k <= great; ) {
  1783. double ak = a[k];
  1784. if (ak == pivot1) { // Move a[k] to left part
  1785. a[k] = a[less];
  1786. a[less] = ak;
  1787. ++less;
  1788. } else if (ak == pivot2) { // Move a[k] to right part
  1789. while (a[great] == pivot2) {
  1790. if (great-- == k) {
  1791. break outer;
  1792. }
  1793. }
  1794. if (a[great] == pivot1) { // a[great] < pivot2
  1795. a[k] = a[less];
  1796. /* * Even though a[great] equals to pivot1, the * assignment a[less] = pivot1 may be incorrect, * if a[great] and pivot1 are floating-point zeros * of different signs. Therefore in float and * double sorting methods we have to use more * accurate assignment a[less] = a[great]. */
  1797. a[less] = a[great];
  1798. ++less;
  1799. } else { // pivot1 < a[great] < pivot2
  1800. a[k] = a[great];
  1801. }
  1802. a[great] = ak;
  1803. --great;
  1804. }
  1805. }
  1806. }
  1807. // Sort center part recursively
  1808. sort(a, less, great, false);
  1809. } else { // Partitioning with one pivot
  1810. /* * Use the third of the five sorted elements as pivot. * This value is inexpensive approximation of the median. */
  1811. double pivot = a[e3];
  1812. /* * Partitioning degenerates to the traditional 3-way * (or "Dutch National Flag") schema: * * left part center part right part * +-------------------------------------------------+ * | < pivot | == pivot | ? | > pivot | * +-------------------------------------------------+ * ^ ^ ^ * | | | * less k great * * Invariants: * * all in (left, less) < pivot * all in [less, k) == pivot * all in (great, right) > pivot * * Pointer k is the first index of ?-part. */
  1813. for (int k = less; k <= great; ++k) {
  1814. if (a[k] == pivot) {
  1815. continue;
  1816. }
  1817. double ak = a[k];
  1818. if (ak < pivot) { // Move a[k] to left part
  1819. a[k] = a[less];
  1820. a[less] = ak;
  1821. ++less;
  1822. } else { // a[k] > pivot - Move a[k] to right part
  1823. while (a[great] > pivot) {
  1824. --great;
  1825. }
  1826. if (a[great] < pivot) { // a[great] <= pivot
  1827. a[k] = a[less];
  1828. a[less] = a[great];
  1829. ++less;
  1830. } else { // a[great] == pivot
  1831. /* * Even though a[great] equals to pivot, the * assignment a[k] = pivot may be incorrect, * if a[great] and pivot are floating-point * zeros of different signs. Therefore in float * and double sorting methods we have to use * more accurate assignment a[k] = a[great]. */
  1832. a[k] = a[great];
  1833. }
  1834. a[great] = ak;
  1835. --great;
  1836. }
  1837. }
  1838. /* * Sort left and right parts recursively. * All elements from center part are equal * and, therefore, already sorted. */
  1839. sort(a, left, less - 1, leftmost);
  1840. sort(a, great + 1, right, false);
  1841. }
  1842. }
  1843. }

相关文章