30 * Vladimir Yaroslavskiy, Jon Bentley, and Josh Bloch. The algorithm |
30 * Vladimir Yaroslavskiy, Jon Bentley, and Josh Bloch. The algorithm |
31 * offers O(n log(n)) performance on many data sets that cause other |
31 * offers O(n log(n)) performance on many data sets that cause other |
32 * quicksorts to degrade to quadratic performance, and is typically |
32 * quicksorts to degrade to quadratic performance, and is typically |
33 * faster than traditional (one-pivot) Quicksort implementations. |
33 * faster than traditional (one-pivot) Quicksort implementations. |
34 * |
34 * |
|
35 * All exposed methods are package-private, designed to be invoked |
|
36 * from public methods (in class Arrays) after performing any |
|
37 * necessary array bounds checks and expanding parameters into the |
|
38 * required forms. |
|
39 * |
35 * @author Vladimir Yaroslavskiy |
40 * @author Vladimir Yaroslavskiy |
36 * @author Jon Bentley |
41 * @author Jon Bentley |
37 * @author Josh Bloch |
42 * @author Josh Bloch |
38 * |
43 * |
39 * @version 2011.02.11 m765.827.12i:5\7pm |
44 * @version 2011.02.11 m765.827.12i:5\7pm |
87 /* |
92 /* |
88 * Sorting methods for seven primitive types. |
93 * Sorting methods for seven primitive types. |
89 */ |
94 */ |
90 |
95 |
91 /** |
96 /** |
92 * Sorts the specified array. |
97 * Sorts the specified range of the array using the given |
93 * |
98 * workspace array slice if possible for merging |
94 * @param a the array to be sorted |
|
95 */ |
|
96 public static void sort(int[] a) { |
|
97 sort(a, 0, a.length - 1); |
|
98 } |
|
99 |
|
100 /** |
|
101 * Sorts the specified range of the array. |
|
102 * |
99 * |
103 * @param a the array to be sorted |
100 * @param a the array to be sorted |
104 * @param left the index of the first element, inclusive, to be sorted |
101 * @param left the index of the first element, inclusive, to be sorted |
105 * @param right the index of the last element, inclusive, to be sorted |
102 * @param right the index of the last element, inclusive, to be sorted |
|
103 * @param work a workspace array (slice) |
|
104 * @param workBase origin of usable space in work array |
|
105 * @param workLen usable size of work array |
106 */ |
106 */ |
107 public static void sort(int[] a, int left, int right) { |
107 static void sort(int[] a, int left, int right, |
|
108 int[] work, int workBase, int workLen) { |
108 // Use Quicksort on small arrays |
109 // Use Quicksort on small arrays |
109 if (right - left < QUICKSORT_THRESHOLD) { |
110 if (right - left < QUICKSORT_THRESHOLD) { |
110 sort(a, left, right, true); |
111 sort(a, left, right, true); |
111 return; |
112 return; |
112 } |
113 } |
145 return; |
146 return; |
146 } |
147 } |
147 } |
148 } |
148 |
149 |
149 // Check special cases |
150 // Check special cases |
|
151 // Implementation note: variable "right" is increased by 1. |
150 if (run[count] == right++) { // The last run contains one element |
152 if (run[count] == right++) { // The last run contains one element |
151 run[++count] = right; |
153 run[++count] = right; |
152 } else if (count == 1) { // The array is already sorted |
154 } else if (count == 1) { // The array is already sorted |
153 return; |
155 return; |
154 } |
156 } |
155 |
157 |
156 /* |
158 // Determine alternation base for merge |
157 * Create temporary array, which is used for merging. |
159 byte odd = 0; |
158 * Implementation note: variable "right" is increased by 1. |
|
159 */ |
|
160 int[] b; byte odd = 0; |
|
161 for (int n = 1; (n <<= 1) < count; odd ^= 1); |
160 for (int n = 1; (n <<= 1) < count; odd ^= 1); |
162 |
161 |
|
162 // Use or create temporary array b for merging |
|
163 int[] b; // temp array; alternates with a |
|
164 int ao, bo; // array offsets from 'left' |
|
165 int blen = right - left; // space needed for b |
|
166 if (work == null || workLen < blen || workBase + blen > work.length) { |
|
167 work = new int[blen]; |
|
168 workBase = 0; |
|
169 } |
163 if (odd == 0) { |
170 if (odd == 0) { |
164 b = a; a = new int[b.length]; |
171 System.arraycopy(a, left, work, workBase, blen); |
165 for (int i = left - 1; ++i < right; a[i] = b[i]); |
172 b = a; |
|
173 bo = 0; |
|
174 a = work; |
|
175 ao = workBase - left; |
166 } else { |
176 } else { |
167 b = new int[a.length]; |
177 b = work; |
|
178 ao = 0; |
|
179 bo = workBase - left; |
168 } |
180 } |
169 |
181 |
170 // Merging |
182 // Merging |
171 for (int last; count > 1; count = last) { |
183 for (int last; count > 1; count = last) { |
172 for (int k = (last = 0) + 2; k <= count; k += 2) { |
184 for (int k = (last = 0) + 2; k <= count; k += 2) { |
173 int hi = run[k], mi = run[k - 1]; |
185 int hi = run[k], mi = run[k - 1]; |
174 for (int i = run[k - 2], p = i, q = mi; i < hi; ++i) { |
186 for (int i = run[k - 2], p = i, q = mi; i < hi; ++i) { |
175 if (q >= hi || p < mi && a[p] <= a[q]) { |
187 if (q >= hi || p < mi && a[p + ao] <= a[q + ao]) { |
176 b[i] = a[p++]; |
188 b[i + bo] = a[p++ + ao]; |
177 } else { |
189 } else { |
178 b[i] = a[q++]; |
190 b[i + bo] = a[q++ + ao]; |
179 } |
191 } |
180 } |
192 } |
181 run[++last] = hi; |
193 run[++last] = hi; |
182 } |
194 } |
183 if ((count & 1) != 0) { |
195 if ((count & 1) != 0) { |
184 for (int i = right, lo = run[count - 1]; --i >= lo; |
196 for (int i = right, lo = run[count - 1]; --i >= lo; |
185 b[i] = a[i] |
197 b[i + bo] = a[i + ao] |
186 ); |
198 ); |
187 run[++last] = right; |
199 run[++last] = right; |
188 } |
200 } |
189 int[] t = a; a = b; b = t; |
201 int[] t = a; a = b; b = t; |
|
202 int o = ao; ao = bo; bo = o; |
190 } |
203 } |
191 } |
204 } |
192 |
205 |
193 /** |
206 /** |
194 * Sorts the specified range of the array by Dual-Pivot Quicksort. |
207 * Sorts the specified range of the array by Dual-Pivot Quicksort. |
527 sort(a, great + 1, right, false); |
540 sort(a, great + 1, right, false); |
528 } |
541 } |
529 } |
542 } |
530 |
543 |
531 /** |
544 /** |
532 * Sorts the specified array. |
545 * Sorts the specified range of the array using the given |
533 * |
546 * workspace array slice if possible for merging |
534 * @param a the array to be sorted |
|
535 */ |
|
536 public static void sort(long[] a) { |
|
537 sort(a, 0, a.length - 1); |
|
538 } |
|
539 |
|
540 /** |
|
541 * Sorts the specified range of the array. |
|
542 * |
547 * |
543 * @param a the array to be sorted |
548 * @param a the array to be sorted |
544 * @param left the index of the first element, inclusive, to be sorted |
549 * @param left the index of the first element, inclusive, to be sorted |
545 * @param right the index of the last element, inclusive, to be sorted |
550 * @param right the index of the last element, inclusive, to be sorted |
|
551 * @param work a workspace array (slice) |
|
552 * @param workBase origin of usable space in work array |
|
553 * @param workLen usable size of work array |
546 */ |
554 */ |
547 public static void sort(long[] a, int left, int right) { |
555 static void sort(long[] a, int left, int right, |
|
556 long[] work, int workBase, int workLen) { |
548 // Use Quicksort on small arrays |
557 // Use Quicksort on small arrays |
549 if (right - left < QUICKSORT_THRESHOLD) { |
558 if (right - left < QUICKSORT_THRESHOLD) { |
550 sort(a, left, right, true); |
559 sort(a, left, right, true); |
551 return; |
560 return; |
552 } |
561 } |
585 return; |
594 return; |
586 } |
595 } |
587 } |
596 } |
588 |
597 |
589 // Check special cases |
598 // Check special cases |
|
599 // Implementation note: variable "right" is increased by 1. |
590 if (run[count] == right++) { // The last run contains one element |
600 if (run[count] == right++) { // The last run contains one element |
591 run[++count] = right; |
601 run[++count] = right; |
592 } else if (count == 1) { // The array is already sorted |
602 } else if (count == 1) { // The array is already sorted |
593 return; |
603 return; |
594 } |
604 } |
595 |
605 |
596 /* |
606 // Determine alternation base for merge |
597 * Create temporary array, which is used for merging. |
607 byte odd = 0; |
598 * Implementation note: variable "right" is increased by 1. |
|
599 */ |
|
600 long[] b; byte odd = 0; |
|
601 for (int n = 1; (n <<= 1) < count; odd ^= 1); |
608 for (int n = 1; (n <<= 1) < count; odd ^= 1); |
602 |
609 |
|
610 // Use or create temporary array b for merging |
|
611 long[] b; // temp array; alternates with a |
|
612 int ao, bo; // array offsets from 'left' |
|
613 int blen = right - left; // space needed for b |
|
614 if (work == null || workLen < blen || workBase + blen > work.length) { |
|
615 work = new long[blen]; |
|
616 workBase = 0; |
|
617 } |
603 if (odd == 0) { |
618 if (odd == 0) { |
604 b = a; a = new long[b.length]; |
619 System.arraycopy(a, left, work, workBase, blen); |
605 for (int i = left - 1; ++i < right; a[i] = b[i]); |
620 b = a; |
|
621 bo = 0; |
|
622 a = work; |
|
623 ao = workBase - left; |
606 } else { |
624 } else { |
607 b = new long[a.length]; |
625 b = work; |
|
626 ao = 0; |
|
627 bo = workBase - left; |
608 } |
628 } |
609 |
629 |
610 // Merging |
630 // Merging |
611 for (int last; count > 1; count = last) { |
631 for (int last; count > 1; count = last) { |
612 for (int k = (last = 0) + 2; k <= count; k += 2) { |
632 for (int k = (last = 0) + 2; k <= count; k += 2) { |
613 int hi = run[k], mi = run[k - 1]; |
633 int hi = run[k], mi = run[k - 1]; |
614 for (int i = run[k - 2], p = i, q = mi; i < hi; ++i) { |
634 for (int i = run[k - 2], p = i, q = mi; i < hi; ++i) { |
615 if (q >= hi || p < mi && a[p] <= a[q]) { |
635 if (q >= hi || p < mi && a[p + ao] <= a[q + ao]) { |
616 b[i] = a[p++]; |
636 b[i + bo] = a[p++ + ao]; |
617 } else { |
637 } else { |
618 b[i] = a[q++]; |
638 b[i + bo] = a[q++ + ao]; |
619 } |
639 } |
620 } |
640 } |
621 run[++last] = hi; |
641 run[++last] = hi; |
622 } |
642 } |
623 if ((count & 1) != 0) { |
643 if ((count & 1) != 0) { |
624 for (int i = right, lo = run[count - 1]; --i >= lo; |
644 for (int i = right, lo = run[count - 1]; --i >= lo; |
625 b[i] = a[i] |
645 b[i + bo] = a[i + ao] |
626 ); |
646 ); |
627 run[++last] = right; |
647 run[++last] = right; |
628 } |
648 } |
629 long[] t = a; a = b; b = t; |
649 long[] t = a; a = b; b = t; |
|
650 int o = ao; ao = bo; bo = o; |
630 } |
651 } |
631 } |
652 } |
632 |
653 |
633 /** |
654 /** |
634 * Sorts the specified range of the array by Dual-Pivot Quicksort. |
655 * Sorts the specified range of the array by Dual-Pivot Quicksort. |
967 sort(a, great + 1, right, false); |
988 sort(a, great + 1, right, false); |
968 } |
989 } |
969 } |
990 } |
970 |
991 |
971 /** |
992 /** |
972 * Sorts the specified array. |
993 * Sorts the specified range of the array using the given |
973 * |
994 * workspace array slice if possible for merging |
974 * @param a the array to be sorted |
|
975 */ |
|
976 public static void sort(short[] a) { |
|
977 sort(a, 0, a.length - 1); |
|
978 } |
|
979 |
|
980 /** |
|
981 * Sorts the specified range of the array. |
|
982 * |
995 * |
983 * @param a the array to be sorted |
996 * @param a the array to be sorted |
984 * @param left the index of the first element, inclusive, to be sorted |
997 * @param left the index of the first element, inclusive, to be sorted |
985 * @param right the index of the last element, inclusive, to be sorted |
998 * @param right the index of the last element, inclusive, to be sorted |
|
999 * @param work a workspace array (slice) |
|
1000 * @param workBase origin of usable space in work array |
|
1001 * @param workLen usable size of work array |
986 */ |
1002 */ |
987 public static void sort(short[] a, int left, int right) { |
1003 static void sort(short[] a, int left, int right, |
|
1004 short[] work, int workBase, int workLen) { |
988 // Use counting sort on large arrays |
1005 // Use counting sort on large arrays |
989 if (right - left > COUNTING_SORT_THRESHOLD_FOR_SHORT_OR_CHAR) { |
1006 if (right - left > COUNTING_SORT_THRESHOLD_FOR_SHORT_OR_CHAR) { |
990 int[] count = new int[NUM_SHORT_VALUES]; |
1007 int[] count = new int[NUM_SHORT_VALUES]; |
991 |
1008 |
992 for (int i = left - 1; ++i <= right; |
1009 for (int i = left - 1; ++i <= right; |
1013 * Sorts the specified range of the array. |
1030 * Sorts the specified range of the array. |
1014 * |
1031 * |
1015 * @param a the array to be sorted |
1032 * @param a the array to be sorted |
1016 * @param left the index of the first element, inclusive, to be sorted |
1033 * @param left the index of the first element, inclusive, to be sorted |
1017 * @param right the index of the last element, inclusive, to be sorted |
1034 * @param right the index of the last element, inclusive, to be sorted |
|
1035 * @param work a workspace array (slice) |
|
1036 * @param workBase origin of usable space in work array |
|
1037 * @param workLen usable size of work array |
1018 */ |
1038 */ |
1019 private static void doSort(short[] a, int left, int right) { |
1039 private static void doSort(short[] a, int left, int right, |
|
1040 short[] work, int workBase, int workLen) { |
1020 // Use Quicksort on small arrays |
1041 // Use Quicksort on small arrays |
1021 if (right - left < QUICKSORT_THRESHOLD) { |
1042 if (right - left < QUICKSORT_THRESHOLD) { |
1022 sort(a, left, right, true); |
1043 sort(a, left, right, true); |
1023 return; |
1044 return; |
1024 } |
1045 } |
1057 return; |
1078 return; |
1058 } |
1079 } |
1059 } |
1080 } |
1060 |
1081 |
1061 // Check special cases |
1082 // Check special cases |
|
1083 // Implementation note: variable "right" is increased by 1. |
1062 if (run[count] == right++) { // The last run contains one element |
1084 if (run[count] == right++) { // The last run contains one element |
1063 run[++count] = right; |
1085 run[++count] = right; |
1064 } else if (count == 1) { // The array is already sorted |
1086 } else if (count == 1) { // The array is already sorted |
1065 return; |
1087 return; |
1066 } |
1088 } |
1067 |
1089 |
1068 /* |
1090 // Determine alternation base for merge |
1069 * Create temporary array, which is used for merging. |
1091 byte odd = 0; |
1070 * Implementation note: variable "right" is increased by 1. |
|
1071 */ |
|
1072 short[] b; byte odd = 0; |
|
1073 for (int n = 1; (n <<= 1) < count; odd ^= 1); |
1092 for (int n = 1; (n <<= 1) < count; odd ^= 1); |
1074 |
1093 |
|
1094 // Use or create temporary array b for merging |
|
1095 short[] b; // temp array; alternates with a |
|
1096 int ao, bo; // array offsets from 'left' |
|
1097 int blen = right - left; // space needed for b |
|
1098 if (work == null || workLen < blen || workBase + blen > work.length) { |
|
1099 work = new short[blen]; |
|
1100 workBase = 0; |
|
1101 } |
1075 if (odd == 0) { |
1102 if (odd == 0) { |
1076 b = a; a = new short[b.length]; |
1103 System.arraycopy(a, left, work, workBase, blen); |
1077 for (int i = left - 1; ++i < right; a[i] = b[i]); |
1104 b = a; |
|
1105 bo = 0; |
|
1106 a = work; |
|
1107 ao = workBase - left; |
1078 } else { |
1108 } else { |
1079 b = new short[a.length]; |
1109 b = work; |
|
1110 ao = 0; |
|
1111 bo = workBase - left; |
1080 } |
1112 } |
1081 |
1113 |
1082 // Merging |
1114 // Merging |
1083 for (int last; count > 1; count = last) { |
1115 for (int last; count > 1; count = last) { |
1084 for (int k = (last = 0) + 2; k <= count; k += 2) { |
1116 for (int k = (last = 0) + 2; k <= count; k += 2) { |
1085 int hi = run[k], mi = run[k - 1]; |
1117 int hi = run[k], mi = run[k - 1]; |
1086 for (int i = run[k - 2], p = i, q = mi; i < hi; ++i) { |
1118 for (int i = run[k - 2], p = i, q = mi; i < hi; ++i) { |
1087 if (q >= hi || p < mi && a[p] <= a[q]) { |
1119 if (q >= hi || p < mi && a[p + ao] <= a[q + ao]) { |
1088 b[i] = a[p++]; |
1120 b[i + bo] = a[p++ + ao]; |
1089 } else { |
1121 } else { |
1090 b[i] = a[q++]; |
1122 b[i + bo] = a[q++ + ao]; |
1091 } |
1123 } |
1092 } |
1124 } |
1093 run[++last] = hi; |
1125 run[++last] = hi; |
1094 } |
1126 } |
1095 if ((count & 1) != 0) { |
1127 if ((count & 1) != 0) { |
1096 for (int i = right, lo = run[count - 1]; --i >= lo; |
1128 for (int i = right, lo = run[count - 1]; --i >= lo; |
1097 b[i] = a[i] |
1129 b[i + bo] = a[i + ao] |
1098 ); |
1130 ); |
1099 run[++last] = right; |
1131 run[++last] = right; |
1100 } |
1132 } |
1101 short[] t = a; a = b; b = t; |
1133 short[] t = a; a = b; b = t; |
|
1134 int o = ao; ao = bo; bo = o; |
1102 } |
1135 } |
1103 } |
1136 } |
1104 |
1137 |
1105 /** |
1138 /** |
1106 * Sorts the specified range of the array by Dual-Pivot Quicksort. |
1139 * Sorts the specified range of the array by Dual-Pivot Quicksort. |
1439 sort(a, great + 1, right, false); |
1472 sort(a, great + 1, right, false); |
1440 } |
1473 } |
1441 } |
1474 } |
1442 |
1475 |
1443 /** |
1476 /** |
1444 * Sorts the specified array. |
1477 * Sorts the specified range of the array using the given |
1445 * |
1478 * workspace array slice if possible for merging |
1446 * @param a the array to be sorted |
|
1447 */ |
|
1448 public static void sort(char[] a) { |
|
1449 sort(a, 0, a.length - 1); |
|
1450 } |
|
1451 |
|
1452 /** |
|
1453 * Sorts the specified range of the array. |
|
1454 * |
1479 * |
1455 * @param a the array to be sorted |
1480 * @param a the array to be sorted |
1456 * @param left the index of the first element, inclusive, to be sorted |
1481 * @param left the index of the first element, inclusive, to be sorted |
1457 * @param right the index of the last element, inclusive, to be sorted |
1482 * @param right the index of the last element, inclusive, to be sorted |
|
1483 * @param work a workspace array (slice) |
|
1484 * @param workBase origin of usable space in work array |
|
1485 * @param workLen usable size of work array |
1458 */ |
1486 */ |
1459 public static void sort(char[] a, int left, int right) { |
1487 static void sort(char[] a, int left, int right, |
|
1488 char[] work, int workBase, int workLen) { |
1460 // Use counting sort on large arrays |
1489 // Use counting sort on large arrays |
1461 if (right - left > COUNTING_SORT_THRESHOLD_FOR_SHORT_OR_CHAR) { |
1490 if (right - left > COUNTING_SORT_THRESHOLD_FOR_SHORT_OR_CHAR) { |
1462 int[] count = new int[NUM_CHAR_VALUES]; |
1491 int[] count = new int[NUM_CHAR_VALUES]; |
1463 |
1492 |
1464 for (int i = left - 1; ++i <= right; |
1493 for (int i = left - 1; ++i <= right; |
1485 * Sorts the specified range of the array. |
1514 * Sorts the specified range of the array. |
1486 * |
1515 * |
1487 * @param a the array to be sorted |
1516 * @param a the array to be sorted |
1488 * @param left the index of the first element, inclusive, to be sorted |
1517 * @param left the index of the first element, inclusive, to be sorted |
1489 * @param right the index of the last element, inclusive, to be sorted |
1518 * @param right the index of the last element, inclusive, to be sorted |
|
1519 * @param work a workspace array (slice) |
|
1520 * @param workBase origin of usable space in work array |
|
1521 * @param workLen usable size of work array |
1490 */ |
1522 */ |
1491 private static void doSort(char[] a, int left, int right) { |
1523 private static void doSort(char[] a, int left, int right, |
|
1524 char[] work, int workBase, int workLen) { |
1492 // Use Quicksort on small arrays |
1525 // Use Quicksort on small arrays |
1493 if (right - left < QUICKSORT_THRESHOLD) { |
1526 if (right - left < QUICKSORT_THRESHOLD) { |
1494 sort(a, left, right, true); |
1527 sort(a, left, right, true); |
1495 return; |
1528 return; |
1496 } |
1529 } |
1529 return; |
1562 return; |
1530 } |
1563 } |
1531 } |
1564 } |
1532 |
1565 |
1533 // Check special cases |
1566 // Check special cases |
|
1567 // Implementation note: variable "right" is increased by 1. |
1534 if (run[count] == right++) { // The last run contains one element |
1568 if (run[count] == right++) { // The last run contains one element |
1535 run[++count] = right; |
1569 run[++count] = right; |
1536 } else if (count == 1) { // The array is already sorted |
1570 } else if (count == 1) { // The array is already sorted |
1537 return; |
1571 return; |
1538 } |
1572 } |
1539 |
1573 |
1540 /* |
1574 // Determine alternation base for merge |
1541 * Create temporary array, which is used for merging. |
1575 byte odd = 0; |
1542 * Implementation note: variable "right" is increased by 1. |
|
1543 */ |
|
1544 char[] b; byte odd = 0; |
|
1545 for (int n = 1; (n <<= 1) < count; odd ^= 1); |
1576 for (int n = 1; (n <<= 1) < count; odd ^= 1); |
1546 |
1577 |
|
1578 // Use or create temporary array b for merging |
|
1579 char[] b; // temp array; alternates with a |
|
1580 int ao, bo; // array offsets from 'left' |
|
1581 int blen = right - left; // space needed for b |
|
1582 if (work == null || workLen < blen || workBase + blen > work.length) { |
|
1583 work = new char[blen]; |
|
1584 workBase = 0; |
|
1585 } |
1547 if (odd == 0) { |
1586 if (odd == 0) { |
1548 b = a; a = new char[b.length]; |
1587 System.arraycopy(a, left, work, workBase, blen); |
1549 for (int i = left - 1; ++i < right; a[i] = b[i]); |
1588 b = a; |
|
1589 bo = 0; |
|
1590 a = work; |
|
1591 ao = workBase - left; |
1550 } else { |
1592 } else { |
1551 b = new char[a.length]; |
1593 b = work; |
|
1594 ao = 0; |
|
1595 bo = workBase - left; |
1552 } |
1596 } |
1553 |
1597 |
1554 // Merging |
1598 // Merging |
1555 for (int last; count > 1; count = last) { |
1599 for (int last; count > 1; count = last) { |
1556 for (int k = (last = 0) + 2; k <= count; k += 2) { |
1600 for (int k = (last = 0) + 2; k <= count; k += 2) { |
1557 int hi = run[k], mi = run[k - 1]; |
1601 int hi = run[k], mi = run[k - 1]; |
1558 for (int i = run[k - 2], p = i, q = mi; i < hi; ++i) { |
1602 for (int i = run[k - 2], p = i, q = mi; i < hi; ++i) { |
1559 if (q >= hi || p < mi && a[p] <= a[q]) { |
1603 if (q >= hi || p < mi && a[p + ao] <= a[q + ao]) { |
1560 b[i] = a[p++]; |
1604 b[i + bo] = a[p++ + ao]; |
1561 } else { |
1605 } else { |
1562 b[i] = a[q++]; |
1606 b[i + bo] = a[q++ + ao]; |
1563 } |
1607 } |
1564 } |
1608 } |
1565 run[++last] = hi; |
1609 run[++last] = hi; |
1566 } |
1610 } |
1567 if ((count & 1) != 0) { |
1611 if ((count & 1) != 0) { |
1568 for (int i = right, lo = run[count - 1]; --i >= lo; |
1612 for (int i = right, lo = run[count - 1]; --i >= lo; |
1569 b[i] = a[i] |
1613 b[i + bo] = a[i + ao] |
1570 ); |
1614 ); |
1571 run[++last] = right; |
1615 run[++last] = right; |
1572 } |
1616 } |
1573 char[] t = a; a = b; b = t; |
1617 char[] t = a; a = b; b = t; |
|
1618 int o = ao; ao = bo; bo = o; |
1574 } |
1619 } |
1575 } |
1620 } |
1576 |
1621 |
1577 /** |
1622 /** |
1578 * Sorts the specified range of the array by Dual-Pivot Quicksort. |
1623 * Sorts the specified range of the array by Dual-Pivot Quicksort. |
1914 |
1959 |
1915 /** The number of distinct byte values. */ |
1960 /** The number of distinct byte values. */ |
1916 private static final int NUM_BYTE_VALUES = 1 << 8; |
1961 private static final int NUM_BYTE_VALUES = 1 << 8; |
1917 |
1962 |
1918 /** |
1963 /** |
1919 * Sorts the specified array. |
|
1920 * |
|
1921 * @param a the array to be sorted |
|
1922 */ |
|
1923 public static void sort(byte[] a) { |
|
1924 sort(a, 0, a.length - 1); |
|
1925 } |
|
1926 |
|
1927 /** |
|
1928 * Sorts the specified range of the array. |
1964 * Sorts the specified range of the array. |
1929 * |
1965 * |
1930 * @param a the array to be sorted |
1966 * @param a the array to be sorted |
1931 * @param left the index of the first element, inclusive, to be sorted |
1967 * @param left the index of the first element, inclusive, to be sorted |
1932 * @param right the index of the last element, inclusive, to be sorted |
1968 * @param right the index of the last element, inclusive, to be sorted |
1933 */ |
1969 */ |
1934 public static void sort(byte[] a, int left, int right) { |
1970 static void sort(byte[] a, int left, int right) { |
1935 // Use counting sort on large arrays |
1971 // Use counting sort on large arrays |
1936 if (right - left > COUNTING_SORT_THRESHOLD_FOR_BYTE) { |
1972 if (right - left > COUNTING_SORT_THRESHOLD_FOR_BYTE) { |
1937 int[] count = new int[NUM_BYTE_VALUES]; |
1973 int[] count = new int[NUM_BYTE_VALUES]; |
1938 |
1974 |
1939 for (int i = left - 1; ++i <= right; |
1975 for (int i = left - 1; ++i <= right; |
1961 } |
1997 } |
1962 } |
1998 } |
1963 } |
1999 } |
1964 |
2000 |
1965 /** |
2001 /** |
1966 * Sorts the specified array. |
2002 * Sorts the specified range of the array using the given |
1967 * |
2003 * workspace array slice if possible for merging |
1968 * @param a the array to be sorted |
|
1969 */ |
|
1970 public static void sort(float[] a) { |
|
1971 sort(a, 0, a.length - 1); |
|
1972 } |
|
1973 |
|
1974 /** |
|
1975 * Sorts the specified range of the array. |
|
1976 * |
2004 * |
1977 * @param a the array to be sorted |
2005 * @param a the array to be sorted |
1978 * @param left the index of the first element, inclusive, to be sorted |
2006 * @param left the index of the first element, inclusive, to be sorted |
1979 * @param right the index of the last element, inclusive, to be sorted |
2007 * @param right the index of the last element, inclusive, to be sorted |
|
2008 * @param work a workspace array (slice) |
|
2009 * @param workBase origin of usable space in work array |
|
2010 * @param workLen usable size of work array |
1980 */ |
2011 */ |
1981 public static void sort(float[] a, int left, int right) { |
2012 static void sort(float[] a, int left, int right, |
|
2013 float[] work, int workBase, int workLen) { |
1982 /* |
2014 /* |
1983 * Phase 1: Move NaNs to the end of the array. |
2015 * Phase 1: Move NaNs to the end of the array. |
1984 */ |
2016 */ |
1985 while (left <= right && Float.isNaN(a[right])) { |
2017 while (left <= right && Float.isNaN(a[right])) { |
1986 --right; |
2018 --right; |
2062 * Sorts the specified range of the array. |
2094 * Sorts the specified range of the array. |
2063 * |
2095 * |
2064 * @param a the array to be sorted |
2096 * @param a the array to be sorted |
2065 * @param left the index of the first element, inclusive, to be sorted |
2097 * @param left the index of the first element, inclusive, to be sorted |
2066 * @param right the index of the last element, inclusive, to be sorted |
2098 * @param right the index of the last element, inclusive, to be sorted |
|
2099 * @param work a workspace array (slice) |
|
2100 * @param workBase origin of usable space in work array |
|
2101 * @param workLen usable size of work array |
2067 */ |
2102 */ |
2068 private static void doSort(float[] a, int left, int right) { |
2103 private static void doSort(float[] a, int left, int right, |
|
2104 float[] work, int workBase, int workLen) { |
2069 // Use Quicksort on small arrays |
2105 // Use Quicksort on small arrays |
2070 if (right - left < QUICKSORT_THRESHOLD) { |
2106 if (right - left < QUICKSORT_THRESHOLD) { |
2071 sort(a, left, right, true); |
2107 sort(a, left, right, true); |
2072 return; |
2108 return; |
2073 } |
2109 } |
2106 return; |
2142 return; |
2107 } |
2143 } |
2108 } |
2144 } |
2109 |
2145 |
2110 // Check special cases |
2146 // Check special cases |
|
2147 // Implementation note: variable "right" is increased by 1. |
2111 if (run[count] == right++) { // The last run contains one element |
2148 if (run[count] == right++) { // The last run contains one element |
2112 run[++count] = right; |
2149 run[++count] = right; |
2113 } else if (count == 1) { // The array is already sorted |
2150 } else if (count == 1) { // The array is already sorted |
2114 return; |
2151 return; |
2115 } |
2152 } |
2116 |
2153 |
2117 /* |
2154 // Determine alternation base for merge |
2118 * Create temporary array, which is used for merging. |
2155 byte odd = 0; |
2119 * Implementation note: variable "right" is increased by 1. |
|
2120 */ |
|
2121 float[] b; byte odd = 0; |
|
2122 for (int n = 1; (n <<= 1) < count; odd ^= 1); |
2156 for (int n = 1; (n <<= 1) < count; odd ^= 1); |
2123 |
2157 |
|
2158 // Use or create temporary array b for merging |
|
2159 float[] b; // temp array; alternates with a |
|
2160 int ao, bo; // array offsets from 'left' |
|
2161 int blen = right - left; // space needed for b |
|
2162 if (work == null || workLen < blen || workBase + blen > work.length) { |
|
2163 work = new float[blen]; |
|
2164 workBase = 0; |
|
2165 } |
2124 if (odd == 0) { |
2166 if (odd == 0) { |
2125 b = a; a = new float[b.length]; |
2167 System.arraycopy(a, left, work, workBase, blen); |
2126 for (int i = left - 1; ++i < right; a[i] = b[i]); |
2168 b = a; |
|
2169 bo = 0; |
|
2170 a = work; |
|
2171 ao = workBase - left; |
2127 } else { |
2172 } else { |
2128 b = new float[a.length]; |
2173 b = work; |
|
2174 ao = 0; |
|
2175 bo = workBase - left; |
2129 } |
2176 } |
2130 |
2177 |
2131 // Merging |
2178 // Merging |
2132 for (int last; count > 1; count = last) { |
2179 for (int last; count > 1; count = last) { |
2133 for (int k = (last = 0) + 2; k <= count; k += 2) { |
2180 for (int k = (last = 0) + 2; k <= count; k += 2) { |
2134 int hi = run[k], mi = run[k - 1]; |
2181 int hi = run[k], mi = run[k - 1]; |
2135 for (int i = run[k - 2], p = i, q = mi; i < hi; ++i) { |
2182 for (int i = run[k - 2], p = i, q = mi; i < hi; ++i) { |
2136 if (q >= hi || p < mi && a[p] <= a[q]) { |
2183 if (q >= hi || p < mi && a[p + ao] <= a[q + ao]) { |
2137 b[i] = a[p++]; |
2184 b[i + bo] = a[p++ + ao]; |
2138 } else { |
2185 } else { |
2139 b[i] = a[q++]; |
2186 b[i + bo] = a[q++ + ao]; |
2140 } |
2187 } |
2141 } |
2188 } |
2142 run[++last] = hi; |
2189 run[++last] = hi; |
2143 } |
2190 } |
2144 if ((count & 1) != 0) { |
2191 if ((count & 1) != 0) { |
2145 for (int i = right, lo = run[count - 1]; --i >= lo; |
2192 for (int i = right, lo = run[count - 1]; --i >= lo; |
2146 b[i] = a[i] |
2193 b[i + bo] = a[i + ao] |
2147 ); |
2194 ); |
2148 run[++last] = right; |
2195 run[++last] = right; |
2149 } |
2196 } |
2150 float[] t = a; a = b; b = t; |
2197 float[] t = a; a = b; b = t; |
|
2198 int o = ao; ao = bo; bo = o; |
2151 } |
2199 } |
2152 } |
2200 } |
2153 |
2201 |
2154 /** |
2202 /** |
2155 * Sorts the specified range of the array by Dual-Pivot Quicksort. |
2203 * Sorts the specified range of the array by Dual-Pivot Quicksort. |
2488 sort(a, great + 1, right, false); |
2536 sort(a, great + 1, right, false); |
2489 } |
2537 } |
2490 } |
2538 } |
2491 |
2539 |
2492 /** |
2540 /** |
2493 * Sorts the specified array. |
2541 * Sorts the specified range of the array using the given |
2494 * |
2542 * workspace array slice if possible for merging |
2495 * @param a the array to be sorted |
|
2496 */ |
|
2497 public static void sort(double[] a) { |
|
2498 sort(a, 0, a.length - 1); |
|
2499 } |
|
2500 |
|
2501 /** |
|
2502 * Sorts the specified range of the array. |
|
2503 * |
2543 * |
2504 * @param a the array to be sorted |
2544 * @param a the array to be sorted |
2505 * @param left the index of the first element, inclusive, to be sorted |
2545 * @param left the index of the first element, inclusive, to be sorted |
2506 * @param right the index of the last element, inclusive, to be sorted |
2546 * @param right the index of the last element, inclusive, to be sorted |
|
2547 * @param work a workspace array (slice) |
|
2548 * @param workBase origin of usable space in work array |
|
2549 * @param workLen usable size of work array |
2507 */ |
2550 */ |
2508 public static void sort(double[] a, int left, int right) { |
2551 static void sort(double[] a, int left, int right, |
|
2552 double[] work, int workBase, int workLen) { |
2509 /* |
2553 /* |
2510 * Phase 1: Move NaNs to the end of the array. |
2554 * Phase 1: Move NaNs to the end of the array. |
2511 */ |
2555 */ |
2512 while (left <= right && Double.isNaN(a[right])) { |
2556 while (left <= right && Double.isNaN(a[right])) { |
2513 --right; |
2557 --right; |
2589 * Sorts the specified range of the array. |
2633 * Sorts the specified range of the array. |
2590 * |
2634 * |
2591 * @param a the array to be sorted |
2635 * @param a the array to be sorted |
2592 * @param left the index of the first element, inclusive, to be sorted |
2636 * @param left the index of the first element, inclusive, to be sorted |
2593 * @param right the index of the last element, inclusive, to be sorted |
2637 * @param right the index of the last element, inclusive, to be sorted |
|
2638 * @param work a workspace array (slice) |
|
2639 * @param workBase origin of usable space in work array |
|
2640 * @param workLen usable size of work array |
2594 */ |
2641 */ |
2595 private static void doSort(double[] a, int left, int right) { |
2642 private static void doSort(double[] a, int left, int right, |
|
2643 double[] work, int workBase, int workLen) { |
2596 // Use Quicksort on small arrays |
2644 // Use Quicksort on small arrays |
2597 if (right - left < QUICKSORT_THRESHOLD) { |
2645 if (right - left < QUICKSORT_THRESHOLD) { |
2598 sort(a, left, right, true); |
2646 sort(a, left, right, true); |
2599 return; |
2647 return; |
2600 } |
2648 } |
2633 return; |
2681 return; |
2634 } |
2682 } |
2635 } |
2683 } |
2636 |
2684 |
2637 // Check special cases |
2685 // Check special cases |
|
2686 // Implementation note: variable "right" is increased by 1. |
2638 if (run[count] == right++) { // The last run contains one element |
2687 if (run[count] == right++) { // The last run contains one element |
2639 run[++count] = right; |
2688 run[++count] = right; |
2640 } else if (count == 1) { // The array is already sorted |
2689 } else if (count == 1) { // The array is already sorted |
2641 return; |
2690 return; |
2642 } |
2691 } |
2643 |
2692 |
2644 /* |
2693 // Determine alternation base for merge |
2645 * Create temporary array, which is used for merging. |
2694 byte odd = 0; |
2646 * Implementation note: variable "right" is increased by 1. |
|
2647 */ |
|
2648 double[] b; byte odd = 0; |
|
2649 for (int n = 1; (n <<= 1) < count; odd ^= 1); |
2695 for (int n = 1; (n <<= 1) < count; odd ^= 1); |
2650 |
2696 |
|
2697 // Use or create temporary array b for merging |
|
2698 double[] b; // temp array; alternates with a |
|
2699 int ao, bo; // array offsets from 'left' |
|
2700 int blen = right - left; // space needed for b |
|
2701 if (work == null || workLen < blen || workBase + blen > work.length) { |
|
2702 work = new double[blen]; |
|
2703 workBase = 0; |
|
2704 } |
2651 if (odd == 0) { |
2705 if (odd == 0) { |
2652 b = a; a = new double[b.length]; |
2706 System.arraycopy(a, left, work, workBase, blen); |
2653 for (int i = left - 1; ++i < right; a[i] = b[i]); |
2707 b = a; |
|
2708 bo = 0; |
|
2709 a = work; |
|
2710 ao = workBase - left; |
2654 } else { |
2711 } else { |
2655 b = new double[a.length]; |
2712 b = work; |
|
2713 ao = 0; |
|
2714 bo = workBase - left; |
2656 } |
2715 } |
2657 |
2716 |
2658 // Merging |
2717 // Merging |
2659 for (int last; count > 1; count = last) { |
2718 for (int last; count > 1; count = last) { |
2660 for (int k = (last = 0) + 2; k <= count; k += 2) { |
2719 for (int k = (last = 0) + 2; k <= count; k += 2) { |
2661 int hi = run[k], mi = run[k - 1]; |
2720 int hi = run[k], mi = run[k - 1]; |
2662 for (int i = run[k - 2], p = i, q = mi; i < hi; ++i) { |
2721 for (int i = run[k - 2], p = i, q = mi; i < hi; ++i) { |
2663 if (q >= hi || p < mi && a[p] <= a[q]) { |
2722 if (q >= hi || p < mi && a[p + ao] <= a[q + ao]) { |
2664 b[i] = a[p++]; |
2723 b[i + bo] = a[p++ + ao]; |
2665 } else { |
2724 } else { |
2666 b[i] = a[q++]; |
2725 b[i + bo] = a[q++ + ao]; |
2667 } |
2726 } |
2668 } |
2727 } |
2669 run[++last] = hi; |
2728 run[++last] = hi; |
2670 } |
2729 } |
2671 if ((count & 1) != 0) { |
2730 if ((count & 1) != 0) { |
2672 for (int i = right, lo = run[count - 1]; --i >= lo; |
2731 for (int i = right, lo = run[count - 1]; --i >= lo; |
2673 b[i] = a[i] |
2732 b[i + bo] = a[i + ao] |
2674 ); |
2733 ); |
2675 run[++last] = right; |
2734 run[++last] = right; |
2676 } |
2735 } |
2677 double[] t = a; a = b; b = t; |
2736 double[] t = a; a = b; b = t; |
|
2737 int o = ao; ao = bo; bo = o; |
2678 } |
2738 } |
2679 } |
2739 } |
2680 |
2740 |
2681 /** |
2741 /** |
2682 * Sorts the specified range of the array by Dual-Pivot Quicksort. |
2742 * Sorts the specified range of the array by Dual-Pivot Quicksort. |