99 } |
99 } |
100 |
100 |
101 /** |
101 /** |
102 * Sorts the specified range of the array into ascending order. This |
102 * Sorts the specified range of the array into ascending order. This |
103 * method differs from the public {@code sort} method in that the |
103 * method differs from the public {@code sort} method in that the |
104 * {@code right} index is inclusive, and it does no range checking on |
104 * {@code right} index is inclusive, and it does no range checking |
105 * {@code left} or {@code right}. |
105 * on {@code left} or {@code right}. |
106 * |
106 * |
107 * @param a the array to be sorted |
107 * @param a the array to be sorted |
108 * @param left the index of the first element, inclusive, to be sorted |
108 * @param left the index of the first element, inclusive, to be sorted |
109 * @param right the index of the last element, inclusive, to be sorted |
109 * @param right the index of the last element, inclusive, to be sorted |
110 */ |
110 */ |
111 private static void doSort(int[] a, int left, int right) { |
111 private static void doSort(int[] a, int left, int right) { |
112 // Use insertion sort on tiny arrays |
112 // Use insertion sort on tiny arrays |
113 if (right - left + 1 < INSERTION_SORT_THRESHOLD) { |
113 if (right - left + 1 < INSERTION_SORT_THRESHOLD) { |
114 for (int k = left + 1; k <= right; k++) { |
114 for (int i = left + 1; i <= right; i++) { |
115 int ak = a[k]; |
115 int ai = a[i]; |
116 int j; |
116 int j; |
117 for (j = k - 1; j >= left && ak < a[j]; j--) { |
117 for (j = i - 1; j >= left && ai < a[j]; j--) { |
118 a[j + 1] = a[j]; |
118 a[j + 1] = a[j]; |
119 } |
119 } |
120 a[j + 1] = ak; |
120 a[j + 1] = ai; |
121 } |
121 } |
122 } else { // Use Dual-Pivot Quicksort on large arrays |
122 } else { // Use Dual-Pivot Quicksort on large arrays |
123 dualPivotQuicksort(a, left, right); |
123 dualPivotQuicksort(a, left, right); |
124 } |
124 } |
125 } |
125 } |
160 * Use the second and fourth of the five sorted elements as pivots. |
160 * Use the second and fourth of the five sorted elements as pivots. |
161 * These values are inexpensive approximations of the first and |
161 * These values are inexpensive approximations of the first and |
162 * second terciles of the array. Note that pivot1 <= pivot2. |
162 * second terciles of the array. Note that pivot1 <= pivot2. |
163 * |
163 * |
164 * The pivots are stored in local variables, and the first and |
164 * The pivots are stored in local variables, and the first and |
165 * the last of the sorted elements are moved to the locations |
165 * the last of the elements to be sorted are moved to the locations |
166 * formerly occupied by the pivots. When partitioning is complete, |
166 * formerly occupied by the pivots. When partitioning is complete, |
167 * the pivots are swapped back into their final positions, and |
167 * the pivots are swapped back into their final positions, and |
168 * excluded from subsequent sorting. |
168 * excluded from subsequent sorting. |
169 */ |
169 */ |
170 int pivot1 = ae2; a[e2] = a[left]; |
170 int pivot1 = ae2; a[e2] = a[left]; |
171 int pivot2 = ae4; a[e4] = a[right]; |
171 int pivot2 = ae4; a[e4] = a[right]; |
172 |
172 |
173 /* |
|
174 * Partitioning |
|
175 * |
|
176 * left part center part right part |
|
177 * ------------------------------------------------------------ |
|
178 * [ < pivot1 | pivot1 <= && <= pivot2 | ? | > pivot2 ] |
|
179 * ------------------------------------------------------------ |
|
180 * ^ ^ ^ |
|
181 * | | | |
|
182 * less k great |
|
183 */ |
|
184 |
|
185 // Pointers |
173 // Pointers |
186 int less = left + 1; // The index of first element of center part |
174 int less = left + 1; // The index of first element of center part |
187 int great = right - 1; // The index before first element of right part |
175 int great = right - 1; // The index before first element of right part |
188 |
176 |
189 boolean pivotsDiffer = pivot1 != pivot2; |
177 boolean pivotsDiffer = (pivot1 != pivot2); |
190 |
178 |
191 if (pivotsDiffer) { |
179 if (pivotsDiffer) { |
192 /* |
180 /* |
|
181 * Partitioning: |
|
182 * |
|
183 * left part center part right part |
|
184 * +------------------------------------------------------------+ |
|
185 * | < pivot1 | pivot1 <= && <= pivot2 | ? | > pivot2 | |
|
186 * +------------------------------------------------------------+ |
|
187 * ^ ^ ^ |
|
188 * | | | |
|
189 * less k great |
|
190 * |
193 * Invariants: |
191 * Invariants: |
|
192 * |
194 * all in (left, less) < pivot1 |
193 * all in (left, less) < pivot1 |
195 * pivot1 <= all in [less, k) <= pivot2 |
194 * pivot1 <= all in [less, k) <= pivot2 |
196 * all in (great, right) > pivot2 |
195 * all in (great, right) > pivot2 |
197 * |
196 * |
198 * Pointer k is the first index of ?-part |
197 * Pointer k is the first index of ?-part |
199 */ |
198 */ |
200 outer: |
199 outer: |
201 for (int k = less; k <= great; k++) { |
200 for (int k = less; k <= great; k++) { |
202 int ak = a[k]; |
201 int ak = a[k]; |
203 if (ak < pivot1) { |
202 if (ak < pivot1) { // Move a[k] to left part |
204 if (k > less) { |
203 if (k != less) { |
205 a[k] = a[less]; |
204 a[k] = a[less]; |
206 a[less] = ak; |
205 a[less] = ak; |
207 } |
206 } |
208 less++; |
207 less++; |
209 } else if (ak > pivot2) { |
208 } else if (ak > pivot2) { // Move a[k] to right part |
210 while (a[great] > pivot2) { |
209 while (a[great] > pivot2) { |
211 if (k == great--) { |
210 if (great-- == k) { |
212 break outer; |
211 break outer; |
213 } |
212 } |
214 } |
213 } |
215 a[k] = a[great]; |
214 if (a[great] < pivot1) { |
216 a[great--] = ak; |
215 a[k] = a[less]; |
217 |
216 a[less++] = a[great]; |
218 if ((ak = a[k]) < pivot1) { |
217 a[great--] = ak; |
219 a[k] = a[less]; |
218 } else { // pivot1 <= a[great] <= pivot2 |
220 a[less++] = ak; |
219 a[k] = a[great]; |
|
220 a[great--] = ak; |
221 } |
221 } |
222 } |
222 } |
223 } |
223 } |
224 } else { // Pivots are equal |
224 } else { // Pivots are equal |
225 /* |
225 /* |
226 * Partition degenerates to the traditional 3-way |
226 * Partition degenerates to the traditional 3-way, |
227 * (or "Dutch National Flag") partition: |
227 * or "Dutch National Flag", partition: |
228 * |
228 * |
229 * left part center part right part |
229 * left part center part right part |
230 * ------------------------------------------------- |
230 * +----------------------------------------------+ |
231 * [ < pivot | == pivot | ? | > pivot ] |
231 * | < pivot | == pivot | ? | > pivot | |
232 * ------------------------------------------------- |
232 * +----------------------------------------------+ |
233 * |
|
234 * ^ ^ ^ |
233 * ^ ^ ^ |
235 * | | | |
234 * | | | |
236 * less k great |
235 * less k great |
237 * |
236 * |
238 * Invariants: |
237 * Invariants: |
239 * |
238 * |
240 * all in (left, less) < pivot |
239 * all in (left, less) < pivot |
241 * all in [less, k) == pivot |
240 * all in [less, k) == pivot |
242 * all in (great, right) > pivot |
241 * all in (great, right) > pivot |
|
242 * |
|
243 * Pointer k is the first index of ?-part |
|
244 */ |
|
245 for (int k = less; k <= great; k++) { |
|
246 int ak = a[k]; |
|
247 if (ak == pivot1) { |
|
248 continue; |
|
249 } |
|
250 if (ak < pivot1) { // Move a[k] to left part |
|
251 if (k != less) { |
|
252 a[k] = a[less]; |
|
253 a[less] = ak; |
|
254 } |
|
255 less++; |
|
256 } else { // (a[k] > pivot1) - Move a[k] to right part |
|
257 /* |
|
258 * We know that pivot1 == a[e3] == pivot2. Thus, we know |
|
259 * that great will still be >= k when the following loop |
|
260 * terminates, even though we don't test for it explicitly. |
|
261 * In other words, a[e3] acts as a sentinel for great. |
|
262 */ |
|
263 while (a[great] > pivot1) { |
|
264 great--; |
|
265 } |
|
266 if (a[great] < pivot1) { |
|
267 a[k] = a[less]; |
|
268 a[less++] = a[great]; |
|
269 a[great--] = ak; |
|
270 } else { // a[great] == pivot1 |
|
271 a[k] = pivot1; |
|
272 a[great--] = ak; |
|
273 } |
|
274 } |
|
275 } |
|
276 } |
|
277 |
|
278 // Swap pivots into their final positions |
|
279 a[left] = a[less - 1]; a[less - 1] = pivot1; |
|
280 a[right] = a[great + 1]; a[great + 1] = pivot2; |
|
281 |
|
282 // Sort left and right parts recursively, excluding known pivot values |
|
283 doSort(a, left, less - 2); |
|
284 doSort(a, great + 2, right); |
|
285 |
|
286 /* |
|
287 * If pivot1 == pivot2, all elements from center |
|
288 * part are equal and, therefore, already sorted |
|
289 */ |
|
290 if (!pivotsDiffer) { |
|
291 return; |
|
292 } |
|
293 |
|
294 /* |
|
295 * If center part is too large (comprises > 2/3 of the array), |
|
296 * swap internal pivot values to ends |
|
297 */ |
|
298 if (less < e1 && great > e5) { |
|
299 while (a[less] == pivot1) { |
|
300 less++; |
|
301 } |
|
302 while (a[great] == pivot2) { |
|
303 great--; |
|
304 } |
|
305 |
|
306 /* |
|
307 * Partitioning: |
|
308 * |
|
309 * left part center part right part |
|
310 * +----------------------------------------------------------+ |
|
311 * | == pivot1 | pivot1 < && < pivot2 | ? | == pivot2 | |
|
312 * +----------------------------------------------------------+ |
|
313 * ^ ^ ^ |
|
314 * | | | |
|
315 * less k great |
|
316 * |
|
317 * Invariants: |
|
318 * |
|
319 * all in (*, less) == pivot1 |
|
320 * pivot1 < all in [less, k) < pivot2 |
|
321 * all in (great, *) == pivot2 |
243 * |
322 * |
244 * Pointer k is the first index of ?-part |
323 * Pointer k is the first index of ?-part |
245 */ |
324 */ |
246 outer: |
325 outer: |
247 for (int k = less; k <= great; k++) { |
326 for (int k = less; k <= great; k++) { |
248 int ak = a[k]; |
327 int ak = a[k]; |
249 if (ak == pivot1) { |
328 if (ak == pivot2) { // Move a[k] to right part |
250 continue; |
329 while (a[great] == pivot2) { |
251 } |
330 if (great-- == k) { |
252 if (ak < pivot1) { |
|
253 if (k > less) { |
|
254 a[k] = a[less]; |
|
255 a[less] = ak; |
|
256 } |
|
257 less++; |
|
258 } else { // a[k] > pivot |
|
259 while (a[great] > pivot1) { |
|
260 if (k == great--) { |
|
261 break outer; |
331 break outer; |
262 } |
332 } |
263 } |
333 } |
264 a[k] = a[great]; |
334 if (a[great] == pivot1) { |
265 a[great--] = ak; |
335 a[k] = a[less]; |
266 |
336 a[less++] = pivot1; |
267 if ((ak = a[k]) < pivot1) { |
337 } else { // pivot1 < a[great] < pivot2 |
268 a[k] = a[less]; |
338 a[k] = a[great]; |
269 a[less++] = ak; |
339 } |
270 } |
340 a[great--] = pivot2; |
271 } |
341 } else if (ak == pivot1) { // Move a[k] to left part |
272 } |
342 a[k] = a[less]; |
273 } |
|
274 |
|
275 // Swap pivots into their final positions |
|
276 a[left] = a[less - 1]; a[less - 1] = pivot1; |
|
277 a[right] = a[great + 1]; a[great + 1] = pivot2; |
|
278 |
|
279 // Sort left and right parts recursively, excluding known pivot values |
|
280 doSort(a, left, less - 2); |
|
281 doSort(a, great + 2, right); |
|
282 |
|
283 /* |
|
284 * If pivot1 == pivot2, all elements from center |
|
285 * part are equal and, therefore, already sorted |
|
286 */ |
|
287 if (!pivotsDiffer) { |
|
288 return; |
|
289 } |
|
290 |
|
291 /* |
|
292 * If center part is too large (comprises > 5/6 of |
|
293 * the array), swap internal pivot values to ends |
|
294 */ |
|
295 if (less < e1 && e5 < great) { |
|
296 while (a[less] == pivot1) { |
|
297 less++; |
|
298 } |
|
299 while (a[great] == pivot2) { |
|
300 great--; |
|
301 } |
|
302 for (int k = less + 1; k <= great; ) { |
|
303 int ak = a[k]; |
|
304 if (ak == pivot1) { |
|
305 a[k++] = a[less]; |
|
306 a[less++] = pivot1; |
343 a[less++] = pivot1; |
307 } else if (ak == pivot2) { |
|
308 a[k] = a[great]; |
|
309 a[great--] = pivot2; |
|
310 } else { |
|
311 k++; |
|
312 } |
344 } |
313 } |
345 } |
314 } |
346 } |
315 |
347 |
316 // Sort center part recursively, excluding known pivot values |
348 // Sort center part recursively, excluding known pivot values |
406 * Use the second and fourth of the five sorted elements as pivots. |
438 * Use the second and fourth of the five sorted elements as pivots. |
407 * These values are inexpensive approximations of the first and |
439 * These values are inexpensive approximations of the first and |
408 * second terciles of the array. Note that pivot1 <= pivot2. |
440 * second terciles of the array. Note that pivot1 <= pivot2. |
409 * |
441 * |
410 * The pivots are stored in local variables, and the first and |
442 * The pivots are stored in local variables, and the first and |
411 * the last of the sorted elements are moved to the locations |
443 * the last of the elements to be sorted are moved to the locations |
412 * formerly occupied by the pivots. When partitioning is complete, |
444 * formerly occupied by the pivots. When partitioning is complete, |
413 * the pivots are swapped back into their final positions, and |
445 * the pivots are swapped back into their final positions, and |
414 * excluded from subsequent sorting. |
446 * excluded from subsequent sorting. |
415 */ |
447 */ |
416 long pivot1 = ae2; a[e2] = a[left]; |
448 long pivot1 = ae2; a[e2] = a[left]; |
417 long pivot2 = ae4; a[e4] = a[right]; |
449 long pivot2 = ae4; a[e4] = a[right]; |
418 |
450 |
419 /* |
|
420 * Partitioning |
|
421 * |
|
422 * left part center part right part |
|
423 * ------------------------------------------------------------ |
|
424 * [ < pivot1 | pivot1 <= && <= pivot2 | ? | > pivot2 ] |
|
425 * ------------------------------------------------------------ |
|
426 * ^ ^ ^ |
|
427 * | | | |
|
428 * less k great |
|
429 */ |
|
430 |
|
431 // Pointers |
451 // Pointers |
432 int less = left + 1; // The index of first element of center part |
452 int less = left + 1; // The index of first element of center part |
433 int great = right - 1; // The index before first element of right part |
453 int great = right - 1; // The index before first element of right part |
434 |
454 |
435 boolean pivotsDiffer = pivot1 != pivot2; |
455 boolean pivotsDiffer = (pivot1 != pivot2); |
436 |
456 |
437 if (pivotsDiffer) { |
457 if (pivotsDiffer) { |
438 /* |
458 /* |
|
459 * Partitioning: |
|
460 * |
|
461 * left part center part right part |
|
462 * +------------------------------------------------------------+ |
|
463 * | < pivot1 | pivot1 <= && <= pivot2 | ? | > pivot2 | |
|
464 * +------------------------------------------------------------+ |
|
465 * ^ ^ ^ |
|
466 * | | | |
|
467 * less k great |
|
468 * |
439 * Invariants: |
469 * Invariants: |
|
470 * |
440 * all in (left, less) < pivot1 |
471 * all in (left, less) < pivot1 |
441 * pivot1 <= all in [less, k) <= pivot2 |
472 * pivot1 <= all in [less, k) <= pivot2 |
442 * all in (great, right) > pivot2 |
473 * all in (great, right) > pivot2 |
443 * |
474 * |
444 * Pointer k is the first index of ?-part |
475 * Pointer k is the first index of ?-part |
445 */ |
476 */ |
446 outer: |
477 outer: |
447 for (int k = less; k <= great; k++) { |
478 for (int k = less; k <= great; k++) { |
448 long ak = a[k]; |
479 long ak = a[k]; |
449 if (ak < pivot1) { |
480 if (ak < pivot1) { // Move a[k] to left part |
450 if (k > less) { |
481 if (k != less) { |
451 a[k] = a[less]; |
482 a[k] = a[less]; |
452 a[less] = ak; |
483 a[less] = ak; |
453 } |
484 } |
454 less++; |
485 less++; |
455 } else if (ak > pivot2) { |
486 } else if (ak > pivot2) { // Move a[k] to right part |
456 while (a[great] > pivot2) { |
487 while (a[great] > pivot2) { |
457 if (k == great--) { |
488 if (great-- == k) { |
458 break outer; |
489 break outer; |
459 } |
490 } |
460 } |
491 } |
461 a[k] = a[great]; |
492 if (a[great] < pivot1) { |
462 a[great--] = ak; |
493 a[k] = a[less]; |
463 |
494 a[less++] = a[great]; |
464 if ((ak = a[k]) < pivot1) { |
495 a[great--] = ak; |
465 a[k] = a[less]; |
496 } else { // pivot1 <= a[great] <= pivot2 |
466 a[less++] = ak; |
497 a[k] = a[great]; |
|
498 a[great--] = ak; |
467 } |
499 } |
468 } |
500 } |
469 } |
501 } |
470 } else { // Pivots are equal |
502 } else { // Pivots are equal |
471 /* |
503 /* |
472 * Partition degenerates to the traditional 3-way |
504 * Partition degenerates to the traditional 3-way, |
473 * (or "Dutch National Flag") partition: |
505 * or "Dutch National Flag", partition: |
474 * |
506 * |
475 * left part center part right part |
507 * left part center part right part |
476 * ------------------------------------------------- |
508 * +----------------------------------------------+ |
477 * [ < pivot | == pivot | ? | > pivot ] |
509 * | < pivot | == pivot | ? | > pivot | |
478 * ------------------------------------------------- |
510 * +----------------------------------------------+ |
479 * |
|
480 * ^ ^ ^ |
511 * ^ ^ ^ |
481 * | | | |
512 * | | | |
482 * less k great |
513 * less k great |
483 * |
514 * |
484 * Invariants: |
515 * Invariants: |
485 * |
516 * |
486 * all in (left, less) < pivot |
517 * all in (left, less) < pivot |
487 * all in [less, k) == pivot |
518 * all in [less, k) == pivot |
488 * all in (great, right) > pivot |
519 * all in (great, right) > pivot |
|
520 * |
|
521 * Pointer k is the first index of ?-part |
|
522 */ |
|
523 for (int k = less; k <= great; k++) { |
|
524 long ak = a[k]; |
|
525 if (ak == pivot1) { |
|
526 continue; |
|
527 } |
|
528 if (ak < pivot1) { // Move a[k] to left part |
|
529 if (k != less) { |
|
530 a[k] = a[less]; |
|
531 a[less] = ak; |
|
532 } |
|
533 less++; |
|
534 } else { // (a[k] > pivot1) - Move a[k] to right part |
|
535 /* |
|
536 * We know that pivot1 == a[e3] == pivot2. Thus, we know |
|
537 * that great will still be >= k when the following loop |
|
538 * terminates, even though we don't test for it explicitly. |
|
539 * In other words, a[e3] acts as a sentinel for great. |
|
540 */ |
|
541 while (a[great] > pivot1) { |
|
542 great--; |
|
543 } |
|
544 if (a[great] < pivot1) { |
|
545 a[k] = a[less]; |
|
546 a[less++] = a[great]; |
|
547 a[great--] = ak; |
|
548 } else { // a[great] == pivot1 |
|
549 a[k] = pivot1; |
|
550 a[great--] = ak; |
|
551 } |
|
552 } |
|
553 } |
|
554 } |
|
555 |
|
556 // Swap pivots into their final positions |
|
557 a[left] = a[less - 1]; a[less - 1] = pivot1; |
|
558 a[right] = a[great + 1]; a[great + 1] = pivot2; |
|
559 |
|
560 // Sort left and right parts recursively, excluding known pivot values |
|
561 doSort(a, left, less - 2); |
|
562 doSort(a, great + 2, right); |
|
563 |
|
564 /* |
|
565 * If pivot1 == pivot2, all elements from center |
|
566 * part are equal and, therefore, already sorted |
|
567 */ |
|
568 if (!pivotsDiffer) { |
|
569 return; |
|
570 } |
|
571 |
|
572 /* |
|
573 * If center part is too large (comprises > 2/3 of the array), |
|
574 * swap internal pivot values to ends |
|
575 */ |
|
576 if (less < e1 && great > e5) { |
|
577 while (a[less] == pivot1) { |
|
578 less++; |
|
579 } |
|
580 while (a[great] == pivot2) { |
|
581 great--; |
|
582 } |
|
583 |
|
584 /* |
|
585 * Partitioning: |
|
586 * |
|
587 * left part center part right part |
|
588 * +----------------------------------------------------------+ |
|
589 * | == pivot1 | pivot1 < && < pivot2 | ? | == pivot2 | |
|
590 * +----------------------------------------------------------+ |
|
591 * ^ ^ ^ |
|
592 * | | | |
|
593 * less k great |
|
594 * |
|
595 * Invariants: |
|
596 * |
|
597 * all in (*, less) == pivot1 |
|
598 * pivot1 < all in [less, k) < pivot2 |
|
599 * all in (great, *) == pivot2 |
489 * |
600 * |
490 * Pointer k is the first index of ?-part |
601 * Pointer k is the first index of ?-part |
491 */ |
602 */ |
492 outer: |
603 outer: |
493 for (int k = less; k <= great; k++) { |
604 for (int k = less; k <= great; k++) { |
494 long ak = a[k]; |
605 long ak = a[k]; |
495 if (ak == pivot1) { |
606 if (ak == pivot2) { // Move a[k] to right part |
496 continue; |
607 while (a[great] == pivot2) { |
497 } |
608 if (great-- == k) { |
498 if (ak < pivot1) { |
|
499 if (k > less) { |
|
500 a[k] = a[less]; |
|
501 a[less] = ak; |
|
502 } |
|
503 less++; |
|
504 } else { // a[k] > pivot |
|
505 while (a[great] > pivot1) { |
|
506 if (k == great--) { |
|
507 break outer; |
609 break outer; |
508 } |
610 } |
509 } |
611 } |
510 a[k] = a[great]; |
612 if (a[great] == pivot1) { |
511 a[great--] = ak; |
613 a[k] = a[less]; |
512 |
614 a[less++] = pivot1; |
513 if ((ak = a[k]) < pivot1) { |
615 } else { // pivot1 < a[great] < pivot2 |
514 a[k] = a[less]; |
616 a[k] = a[great]; |
515 a[less++] = ak; |
617 } |
516 } |
618 a[great--] = pivot2; |
517 } |
619 } else if (ak == pivot1) { // Move a[k] to left part |
518 } |
620 a[k] = a[less]; |
519 } |
|
520 |
|
521 // Swap pivots into their final positions |
|
522 a[left] = a[less - 1]; a[less - 1] = pivot1; |
|
523 a[right] = a[great + 1]; a[great + 1] = pivot2; |
|
524 |
|
525 // Sort left and right parts recursively, excluding known pivot values |
|
526 doSort(a, left, less - 2); |
|
527 doSort(a, great + 2, right); |
|
528 |
|
529 /* |
|
530 * If pivot1 == pivot2, all elements from center |
|
531 * part are equal and, therefore, already sorted |
|
532 */ |
|
533 if (!pivotsDiffer) { |
|
534 return; |
|
535 } |
|
536 |
|
537 /* |
|
538 * If center part is too large (comprises > 5/6 of |
|
539 * the array), swap internal pivot values to ends |
|
540 */ |
|
541 if (less < e1 && e5 < great) { |
|
542 while (a[less] == pivot1) { |
|
543 less++; |
|
544 } |
|
545 while (a[great] == pivot2) { |
|
546 great--; |
|
547 } |
|
548 for (int k = less + 1; k <= great; ) { |
|
549 long ak = a[k]; |
|
550 if (ak == pivot1) { |
|
551 a[k++] = a[less]; |
|
552 a[less++] = pivot1; |
621 a[less++] = pivot1; |
553 } else if (ak == pivot2) { |
|
554 a[k] = a[great]; |
|
555 a[great--] = pivot2; |
|
556 } else { |
|
557 k++; |
|
558 } |
622 } |
559 } |
623 } |
560 } |
624 } |
561 |
625 |
562 // Sort center part recursively, excluding known pivot values |
626 // Sort center part recursively, excluding known pivot values |
669 * Use the second and fourth of the five sorted elements as pivots. |
733 * Use the second and fourth of the five sorted elements as pivots. |
670 * These values are inexpensive approximations of the first and |
734 * These values are inexpensive approximations of the first and |
671 * second terciles of the array. Note that pivot1 <= pivot2. |
735 * second terciles of the array. Note that pivot1 <= pivot2. |
672 * |
736 * |
673 * The pivots are stored in local variables, and the first and |
737 * The pivots are stored in local variables, and the first and |
674 * the last of the sorted elements are moved to the locations |
738 * the last of the elements to be sorted are moved to the locations |
675 * formerly occupied by the pivots. When partitioning is complete, |
739 * formerly occupied by the pivots. When partitioning is complete, |
676 * the pivots are swapped back into their final positions, and |
740 * the pivots are swapped back into their final positions, and |
677 * excluded from subsequent sorting. |
741 * excluded from subsequent sorting. |
678 */ |
742 */ |
679 short pivot1 = ae2; a[e2] = a[left]; |
743 short pivot1 = ae2; a[e2] = a[left]; |
680 short pivot2 = ae4; a[e4] = a[right]; |
744 short pivot2 = ae4; a[e4] = a[right]; |
681 |
745 |
682 /* |
|
683 * Partitioning |
|
684 * |
|
685 * left part center part right part |
|
686 * ------------------------------------------------------------ |
|
687 * [ < pivot1 | pivot1 <= && <= pivot2 | ? | > pivot2 ] |
|
688 * ------------------------------------------------------------ |
|
689 * ^ ^ ^ |
|
690 * | | | |
|
691 * less k great |
|
692 */ |
|
693 |
|
694 // Pointers |
746 // Pointers |
695 int less = left + 1; // The index of first element of center part |
747 int less = left + 1; // The index of first element of center part |
696 int great = right - 1; // The index before first element of right part |
748 int great = right - 1; // The index before first element of right part |
697 |
749 |
698 boolean pivotsDiffer = pivot1 != pivot2; |
750 boolean pivotsDiffer = (pivot1 != pivot2); |
699 |
751 |
700 if (pivotsDiffer) { |
752 if (pivotsDiffer) { |
701 /* |
753 /* |
|
754 * Partitioning: |
|
755 * |
|
756 * left part center part right part |
|
757 * +------------------------------------------------------------+ |
|
758 * | < pivot1 | pivot1 <= && <= pivot2 | ? | > pivot2 | |
|
759 * +------------------------------------------------------------+ |
|
760 * ^ ^ ^ |
|
761 * | | | |
|
762 * less k great |
|
763 * |
702 * Invariants: |
764 * Invariants: |
|
765 * |
703 * all in (left, less) < pivot1 |
766 * all in (left, less) < pivot1 |
704 * pivot1 <= all in [less, k) <= pivot2 |
767 * pivot1 <= all in [less, k) <= pivot2 |
705 * all in (great, right) > pivot2 |
768 * all in (great, right) > pivot2 |
706 * |
769 * |
707 * Pointer k is the first index of ?-part |
770 * Pointer k is the first index of ?-part |
708 */ |
771 */ |
709 outer: |
772 outer: |
710 for (int k = less; k <= great; k++) { |
773 for (int k = less; k <= great; k++) { |
711 short ak = a[k]; |
774 short ak = a[k]; |
712 if (ak < pivot1) { |
775 if (ak < pivot1) { // Move a[k] to left part |
713 if (k > less) { |
776 if (k != less) { |
714 a[k] = a[less]; |
777 a[k] = a[less]; |
715 a[less] = ak; |
778 a[less] = ak; |
716 } |
779 } |
717 less++; |
780 less++; |
718 } else if (ak > pivot2) { |
781 } else if (ak > pivot2) { // Move a[k] to right part |
719 while (a[great] > pivot2) { |
782 while (a[great] > pivot2) { |
720 if (k == great--) { |
783 if (great-- == k) { |
721 break outer; |
784 break outer; |
722 } |
785 } |
723 } |
786 } |
724 a[k] = a[great]; |
787 if (a[great] < pivot1) { |
725 a[great--] = ak; |
788 a[k] = a[less]; |
726 |
789 a[less++] = a[great]; |
727 if ((ak = a[k]) < pivot1) { |
790 a[great--] = ak; |
728 a[k] = a[less]; |
791 } else { // pivot1 <= a[great] <= pivot2 |
729 a[less++] = ak; |
792 a[k] = a[great]; |
|
793 a[great--] = ak; |
730 } |
794 } |
731 } |
795 } |
732 } |
796 } |
733 } else { // Pivots are equal |
797 } else { // Pivots are equal |
734 /* |
798 /* |
735 * Partition degenerates to the traditional 3-way |
799 * Partition degenerates to the traditional 3-way, |
736 * (or "Dutch National Flag") partition: |
800 * or "Dutch National Flag", partition: |
737 * |
801 * |
738 * left part center part right part |
802 * left part center part right part |
739 * ------------------------------------------------- |
803 * +----------------------------------------------+ |
740 * [ < pivot | == pivot | ? | > pivot ] |
804 * | < pivot | == pivot | ? | > pivot | |
741 * ------------------------------------------------- |
805 * +----------------------------------------------+ |
742 * |
|
743 * ^ ^ ^ |
806 * ^ ^ ^ |
744 * | | | |
807 * | | | |
745 * less k great |
808 * less k great |
746 * |
809 * |
747 * Invariants: |
810 * Invariants: |
748 * |
811 * |
749 * all in (left, less) < pivot |
812 * all in (left, less) < pivot |
750 * all in [less, k) == pivot |
813 * all in [less, k) == pivot |
751 * all in (great, right) > pivot |
814 * all in (great, right) > pivot |
|
815 * |
|
816 * Pointer k is the first index of ?-part |
|
817 */ |
|
818 for (int k = less; k <= great; k++) { |
|
819 short ak = a[k]; |
|
820 if (ak == pivot1) { |
|
821 continue; |
|
822 } |
|
823 if (ak < pivot1) { // Move a[k] to left part |
|
824 if (k != less) { |
|
825 a[k] = a[less]; |
|
826 a[less] = ak; |
|
827 } |
|
828 less++; |
|
829 } else { // (a[k] > pivot1) - Move a[k] to right part |
|
830 /* |
|
831 * We know that pivot1 == a[e3] == pivot2. Thus, we know |
|
832 * that great will still be >= k when the following loop |
|
833 * terminates, even though we don't test for it explicitly. |
|
834 * In other words, a[e3] acts as a sentinel for great. |
|
835 */ |
|
836 while (a[great] > pivot1) { |
|
837 great--; |
|
838 } |
|
839 if (a[great] < pivot1) { |
|
840 a[k] = a[less]; |
|
841 a[less++] = a[great]; |
|
842 a[great--] = ak; |
|
843 } else { // a[great] == pivot1 |
|
844 a[k] = pivot1; |
|
845 a[great--] = ak; |
|
846 } |
|
847 } |
|
848 } |
|
849 } |
|
850 |
|
851 // Swap pivots into their final positions |
|
852 a[left] = a[less - 1]; a[less - 1] = pivot1; |
|
853 a[right] = a[great + 1]; a[great + 1] = pivot2; |
|
854 |
|
855 // Sort left and right parts recursively, excluding known pivot values |
|
856 doSort(a, left, less - 2); |
|
857 doSort(a, great + 2, right); |
|
858 |
|
859 /* |
|
860 * If pivot1 == pivot2, all elements from center |
|
861 * part are equal and, therefore, already sorted |
|
862 */ |
|
863 if (!pivotsDiffer) { |
|
864 return; |
|
865 } |
|
866 |
|
867 /* |
|
868 * If center part is too large (comprises > 2/3 of the array), |
|
869 * swap internal pivot values to ends |
|
870 */ |
|
871 if (less < e1 && great > e5) { |
|
872 while (a[less] == pivot1) { |
|
873 less++; |
|
874 } |
|
875 while (a[great] == pivot2) { |
|
876 great--; |
|
877 } |
|
878 |
|
879 /* |
|
880 * Partitioning: |
|
881 * |
|
882 * left part center part right part |
|
883 * +----------------------------------------------------------+ |
|
884 * | == pivot1 | pivot1 < && < pivot2 | ? | == pivot2 | |
|
885 * +----------------------------------------------------------+ |
|
886 * ^ ^ ^ |
|
887 * | | | |
|
888 * less k great |
|
889 * |
|
890 * Invariants: |
|
891 * |
|
892 * all in (*, less) == pivot1 |
|
893 * pivot1 < all in [less, k) < pivot2 |
|
894 * all in (great, *) == pivot2 |
752 * |
895 * |
753 * Pointer k is the first index of ?-part |
896 * Pointer k is the first index of ?-part |
754 */ |
897 */ |
755 outer: |
898 outer: |
756 for (int k = less; k <= great; k++) { |
899 for (int k = less; k <= great; k++) { |
757 short ak = a[k]; |
900 short ak = a[k]; |
758 if (ak == pivot1) { |
901 if (ak == pivot2) { // Move a[k] to right part |
759 continue; |
902 while (a[great] == pivot2) { |
760 } |
903 if (great-- == k) { |
761 if (ak < pivot1) { |
|
762 if (k > less) { |
|
763 a[k] = a[less]; |
|
764 a[less] = ak; |
|
765 } |
|
766 less++; |
|
767 } else { // a[k] > pivot |
|
768 while (a[great] > pivot1) { |
|
769 if (k == great--) { |
|
770 break outer; |
904 break outer; |
771 } |
905 } |
772 } |
906 } |
773 a[k] = a[great]; |
907 if (a[great] == pivot1) { |
774 a[great--] = ak; |
908 a[k] = a[less]; |
775 |
909 a[less++] = pivot1; |
776 if ((ak = a[k]) < pivot1) { |
910 } else { // pivot1 < a[great] < pivot2 |
777 a[k] = a[less]; |
911 a[k] = a[great]; |
778 a[less++] = ak; |
912 } |
779 } |
913 a[great--] = pivot2; |
780 } |
914 } else if (ak == pivot1) { // Move a[k] to left part |
781 } |
915 a[k] = a[less]; |
782 } |
|
783 |
|
784 // Swap pivots into their final positions |
|
785 a[left] = a[less - 1]; a[less - 1] = pivot1; |
|
786 a[right] = a[great + 1]; a[great + 1] = pivot2; |
|
787 |
|
788 // Sort left and right parts recursively, excluding known pivot values |
|
789 doSort(a, left, less - 2); |
|
790 doSort(a, great + 2, right); |
|
791 |
|
792 /* |
|
793 * If pivot1 == pivot2, all elements from center |
|
794 * part are equal and, therefore, already sorted |
|
795 */ |
|
796 if (!pivotsDiffer) { |
|
797 return; |
|
798 } |
|
799 |
|
800 /* |
|
801 * If center part is too large (comprises > 5/6 of |
|
802 * the array), swap internal pivot values to ends |
|
803 */ |
|
804 if (less < e1 && e5 < great) { |
|
805 while (a[less] == pivot1) { |
|
806 less++; |
|
807 } |
|
808 while (a[great] == pivot2) { |
|
809 great--; |
|
810 } |
|
811 for (int k = less + 1; k <= great; ) { |
|
812 short ak = a[k]; |
|
813 if (ak == pivot1) { |
|
814 a[k++] = a[less]; |
|
815 a[less++] = pivot1; |
916 a[less++] = pivot1; |
816 } else if (ak == pivot2) { |
|
817 a[k] = a[great]; |
|
818 a[great--] = pivot2; |
|
819 } else { |
|
820 k++; |
|
821 } |
917 } |
822 } |
918 } |
823 } |
919 } |
824 |
920 |
825 // Sort center part recursively, excluding known pivot values |
921 // Sort center part recursively, excluding known pivot values |
930 * Use the second and fourth of the five sorted elements as pivots. |
1026 * Use the second and fourth of the five sorted elements as pivots. |
931 * These values are inexpensive approximations of the first and |
1027 * These values are inexpensive approximations of the first and |
932 * second terciles of the array. Note that pivot1 <= pivot2. |
1028 * second terciles of the array. Note that pivot1 <= pivot2. |
933 * |
1029 * |
934 * The pivots are stored in local variables, and the first and |
1030 * The pivots are stored in local variables, and the first and |
935 * the last of the sorted elements are moved to the locations |
1031 * the last of the elements to be sorted are moved to the locations |
936 * formerly occupied by the pivots. When partitioning is complete, |
1032 * formerly occupied by the pivots. When partitioning is complete, |
937 * the pivots are swapped back into their final positions, and |
1033 * the pivots are swapped back into their final positions, and |
938 * excluded from subsequent sorting. |
1034 * excluded from subsequent sorting. |
939 */ |
1035 */ |
940 char pivot1 = ae2; a[e2] = a[left]; |
1036 char pivot1 = ae2; a[e2] = a[left]; |
941 char pivot2 = ae4; a[e4] = a[right]; |
1037 char pivot2 = ae4; a[e4] = a[right]; |
942 |
1038 |
943 /* |
|
944 * Partitioning |
|
945 * |
|
946 * left part center part right part |
|
947 * ------------------------------------------------------------ |
|
948 * [ < pivot1 | pivot1 <= && <= pivot2 | ? | > pivot2 ] |
|
949 * ------------------------------------------------------------ |
|
950 * ^ ^ ^ |
|
951 * | | | |
|
952 * less k great |
|
953 */ |
|
954 |
|
955 // Pointers |
1039 // Pointers |
956 int less = left + 1; // The index of first element of center part |
1040 int less = left + 1; // The index of first element of center part |
957 int great = right - 1; // The index before first element of right part |
1041 int great = right - 1; // The index before first element of right part |
958 |
1042 |
959 boolean pivotsDiffer = pivot1 != pivot2; |
1043 boolean pivotsDiffer = (pivot1 != pivot2); |
960 |
1044 |
961 if (pivotsDiffer) { |
1045 if (pivotsDiffer) { |
962 /* |
1046 /* |
|
1047 * Partitioning: |
|
1048 * |
|
1049 * left part center part right part |
|
1050 * +------------------------------------------------------------+ |
|
1051 * | < pivot1 | pivot1 <= && <= pivot2 | ? | > pivot2 | |
|
1052 * +------------------------------------------------------------+ |
|
1053 * ^ ^ ^ |
|
1054 * | | | |
|
1055 * less k great |
|
1056 * |
963 * Invariants: |
1057 * Invariants: |
|
1058 * |
964 * all in (left, less) < pivot1 |
1059 * all in (left, less) < pivot1 |
965 * pivot1 <= all in [less, k) <= pivot2 |
1060 * pivot1 <= all in [less, k) <= pivot2 |
966 * all in (great, right) > pivot2 |
1061 * all in (great, right) > pivot2 |
967 * |
1062 * |
968 * Pointer k is the first index of ?-part |
1063 * Pointer k is the first index of ?-part |
969 */ |
1064 */ |
970 outer: |
1065 outer: |
971 for (int k = less; k <= great; k++) { |
1066 for (int k = less; k <= great; k++) { |
972 char ak = a[k]; |
1067 char ak = a[k]; |
973 if (ak < pivot1) { |
1068 if (ak < pivot1) { // Move a[k] to left part |
974 if (k > less) { |
1069 if (k != less) { |
975 a[k] = a[less]; |
1070 a[k] = a[less]; |
976 a[less] = ak; |
1071 a[less] = ak; |
977 } |
1072 } |
978 less++; |
1073 less++; |
979 } else if (ak > pivot2) { |
1074 } else if (ak > pivot2) { // Move a[k] to right part |
980 while (a[great] > pivot2) { |
1075 while (a[great] > pivot2) { |
981 if (k == great--) { |
1076 if (great-- == k) { |
982 break outer; |
1077 break outer; |
983 } |
1078 } |
984 } |
1079 } |
985 a[k] = a[great]; |
1080 if (a[great] < pivot1) { |
986 a[great--] = ak; |
1081 a[k] = a[less]; |
987 |
1082 a[less++] = a[great]; |
988 if ((ak = a[k]) < pivot1) { |
1083 a[great--] = ak; |
989 a[k] = a[less]; |
1084 } else { // pivot1 <= a[great] <= pivot2 |
990 a[less++] = ak; |
1085 a[k] = a[great]; |
|
1086 a[great--] = ak; |
991 } |
1087 } |
992 } |
1088 } |
993 } |
1089 } |
994 } else { // Pivots are equal |
1090 } else { // Pivots are equal |
995 /* |
1091 /* |
996 * Partition degenerates to the traditional 3-way |
1092 * Partition degenerates to the traditional 3-way, |
997 * (or "Dutch National Flag") partition: |
1093 * or "Dutch National Flag", partition: |
998 * |
1094 * |
999 * left part center part right part |
1095 * left part center part right part |
1000 * ------------------------------------------------- |
1096 * +----------------------------------------------+ |
1001 * [ < pivot | == pivot | ? | > pivot ] |
1097 * | < pivot | == pivot | ? | > pivot | |
1002 * ------------------------------------------------- |
1098 * +----------------------------------------------+ |
1003 * |
|
1004 * ^ ^ ^ |
1099 * ^ ^ ^ |
1005 * | | | |
1100 * | | | |
1006 * less k great |
1101 * less k great |
1007 * |
1102 * |
1008 * Invariants: |
1103 * Invariants: |
1009 * |
1104 * |
1010 * all in (left, less) < pivot |
1105 * all in (left, less) < pivot |
1011 * all in [less, k) == pivot |
1106 * all in [less, k) == pivot |
1012 * all in (great, right) > pivot |
1107 * all in (great, right) > pivot |
|
1108 * |
|
1109 * Pointer k is the first index of ?-part |
|
1110 */ |
|
1111 for (int k = less; k <= great; k++) { |
|
1112 char ak = a[k]; |
|
1113 if (ak == pivot1) { |
|
1114 continue; |
|
1115 } |
|
1116 if (ak < pivot1) { // Move a[k] to left part |
|
1117 if (k != less) { |
|
1118 a[k] = a[less]; |
|
1119 a[less] = ak; |
|
1120 } |
|
1121 less++; |
|
1122 } else { // (a[k] > pivot1) - Move a[k] to right part |
|
1123 /* |
|
1124 * We know that pivot1 == a[e3] == pivot2. Thus, we know |
|
1125 * that great will still be >= k when the following loop |
|
1126 * terminates, even though we don't test for it explicitly. |
|
1127 * In other words, a[e3] acts as a sentinel for great. |
|
1128 */ |
|
1129 while (a[great] > pivot1) { |
|
1130 great--; |
|
1131 } |
|
1132 if (a[great] < pivot1) { |
|
1133 a[k] = a[less]; |
|
1134 a[less++] = a[great]; |
|
1135 a[great--] = ak; |
|
1136 } else { // a[great] == pivot1 |
|
1137 a[k] = pivot1; |
|
1138 a[great--] = ak; |
|
1139 } |
|
1140 } |
|
1141 } |
|
1142 } |
|
1143 |
|
1144 // Swap pivots into their final positions |
|
1145 a[left] = a[less - 1]; a[less - 1] = pivot1; |
|
1146 a[right] = a[great + 1]; a[great + 1] = pivot2; |
|
1147 |
|
1148 // Sort left and right parts recursively, excluding known pivot values |
|
1149 doSort(a, left, less - 2); |
|
1150 doSort(a, great + 2, right); |
|
1151 |
|
1152 /* |
|
1153 * If pivot1 == pivot2, all elements from center |
|
1154 * part are equal and, therefore, already sorted |
|
1155 */ |
|
1156 if (!pivotsDiffer) { |
|
1157 return; |
|
1158 } |
|
1159 |
|
1160 /* |
|
1161 * If center part is too large (comprises > 2/3 of the array), |
|
1162 * swap internal pivot values to ends |
|
1163 */ |
|
1164 if (less < e1 && great > e5) { |
|
1165 while (a[less] == pivot1) { |
|
1166 less++; |
|
1167 } |
|
1168 while (a[great] == pivot2) { |
|
1169 great--; |
|
1170 } |
|
1171 |
|
1172 /* |
|
1173 * Partitioning: |
|
1174 * |
|
1175 * left part center part right part |
|
1176 * +----------------------------------------------------------+ |
|
1177 * | == pivot1 | pivot1 < && < pivot2 | ? | == pivot2 | |
|
1178 * +----------------------------------------------------------+ |
|
1179 * ^ ^ ^ |
|
1180 * | | | |
|
1181 * less k great |
|
1182 * |
|
1183 * Invariants: |
|
1184 * |
|
1185 * all in (*, less) == pivot1 |
|
1186 * pivot1 < all in [less, k) < pivot2 |
|
1187 * all in (great, *) == pivot2 |
1013 * |
1188 * |
1014 * Pointer k is the first index of ?-part |
1189 * Pointer k is the first index of ?-part |
1015 */ |
1190 */ |
1016 outer: |
1191 outer: |
1017 for (int k = less; k <= great; k++) { |
1192 for (int k = less; k <= great; k++) { |
1018 char ak = a[k]; |
1193 char ak = a[k]; |
1019 if (ak == pivot1) { |
1194 if (ak == pivot2) { // Move a[k] to right part |
1020 continue; |
1195 while (a[great] == pivot2) { |
1021 } |
1196 if (great-- == k) { |
1022 if (ak < pivot1) { |
|
1023 if (k > less) { |
|
1024 a[k] = a[less]; |
|
1025 a[less] = ak; |
|
1026 } |
|
1027 less++; |
|
1028 } else { // a[k] > pivot |
|
1029 while (a[great] > pivot1) { |
|
1030 if (k == great--) { |
|
1031 break outer; |
1197 break outer; |
1032 } |
1198 } |
1033 } |
1199 } |
1034 a[k] = a[great]; |
1200 if (a[great] == pivot1) { |
1035 a[great--] = ak; |
1201 a[k] = a[less]; |
1036 |
1202 a[less++] = pivot1; |
1037 if ((ak = a[k]) < pivot1) { |
1203 } else { // pivot1 < a[great] < pivot2 |
1038 a[k] = a[less]; |
1204 a[k] = a[great]; |
1039 a[less++] = ak; |
1205 } |
1040 } |
1206 a[great--] = pivot2; |
1041 } |
1207 } else if (ak == pivot1) { // Move a[k] to left part |
1042 } |
1208 a[k] = a[less]; |
1043 } |
|
1044 |
|
1045 // Swap pivots into their final positions |
|
1046 a[left] = a[less - 1]; a[less - 1] = pivot1; |
|
1047 a[right] = a[great + 1]; a[great + 1] = pivot2; |
|
1048 |
|
1049 // Sort left and right parts recursively, excluding known pivot values |
|
1050 doSort(a, left, less - 2); |
|
1051 doSort(a, great + 2, right); |
|
1052 |
|
1053 /* |
|
1054 * If pivot1 == pivot2, all elements from center |
|
1055 * part are equal and, therefore, already sorted |
|
1056 */ |
|
1057 if (!pivotsDiffer) { |
|
1058 return; |
|
1059 } |
|
1060 |
|
1061 /* |
|
1062 * If center part is too large (comprises > 5/6 of |
|
1063 * the array), swap internal pivot values to ends |
|
1064 */ |
|
1065 if (less < e1 && e5 < great) { |
|
1066 while (a[less] == pivot1) { |
|
1067 less++; |
|
1068 } |
|
1069 while (a[great] == pivot2) { |
|
1070 great--; |
|
1071 } |
|
1072 for (int k = less + 1; k <= great; ) { |
|
1073 char ak = a[k]; |
|
1074 if (ak == pivot1) { |
|
1075 a[k++] = a[less]; |
|
1076 a[less++] = pivot1; |
1209 a[less++] = pivot1; |
1077 } else if (ak == pivot2) { |
|
1078 a[k] = a[great]; |
|
1079 a[great--] = pivot2; |
|
1080 } else { |
|
1081 k++; |
|
1082 } |
1210 } |
1083 } |
1211 } |
1084 } |
1212 } |
1085 |
1213 |
1086 // Sort center part recursively, excluding known pivot values |
1214 // Sort center part recursively, excluding known pivot values |
1193 * Use the second and fourth of the five sorted elements as pivots. |
1321 * Use the second and fourth of the five sorted elements as pivots. |
1194 * These values are inexpensive approximations of the first and |
1322 * These values are inexpensive approximations of the first and |
1195 * second terciles of the array. Note that pivot1 <= pivot2. |
1323 * second terciles of the array. Note that pivot1 <= pivot2. |
1196 * |
1324 * |
1197 * The pivots are stored in local variables, and the first and |
1325 * The pivots are stored in local variables, and the first and |
1198 * the last of the sorted elements are moved to the locations |
1326 * the last of the elements to be sorted are moved to the locations |
1199 * formerly occupied by the pivots. When partitioning is complete, |
1327 * formerly occupied by the pivots. When partitioning is complete, |
1200 * the pivots are swapped back into their final positions, and |
1328 * the pivots are swapped back into their final positions, and |
1201 * excluded from subsequent sorting. |
1329 * excluded from subsequent sorting. |
1202 */ |
1330 */ |
1203 byte pivot1 = ae2; a[e2] = a[left]; |
1331 byte pivot1 = ae2; a[e2] = a[left]; |
1204 byte pivot2 = ae4; a[e4] = a[right]; |
1332 byte pivot2 = ae4; a[e4] = a[right]; |
1205 |
1333 |
1206 /* |
|
1207 * Partitioning |
|
1208 * |
|
1209 * left part center part right part |
|
1210 * ------------------------------------------------------------ |
|
1211 * [ < pivot1 | pivot1 <= && <= pivot2 | ? | > pivot2 ] |
|
1212 * ------------------------------------------------------------ |
|
1213 * ^ ^ ^ |
|
1214 * | | | |
|
1215 * less k great |
|
1216 */ |
|
1217 |
|
1218 // Pointers |
1334 // Pointers |
1219 int less = left + 1; // The index of first element of center part |
1335 int less = left + 1; // The index of first element of center part |
1220 int great = right - 1; // The index before first element of right part |
1336 int great = right - 1; // The index before first element of right part |
1221 |
1337 |
1222 boolean pivotsDiffer = pivot1 != pivot2; |
1338 boolean pivotsDiffer = (pivot1 != pivot2); |
1223 |
1339 |
1224 if (pivotsDiffer) { |
1340 if (pivotsDiffer) { |
1225 /* |
1341 /* |
|
1342 * Partitioning: |
|
1343 * |
|
1344 * left part center part right part |
|
1345 * +------------------------------------------------------------+ |
|
1346 * | < pivot1 | pivot1 <= && <= pivot2 | ? | > pivot2 | |
|
1347 * +------------------------------------------------------------+ |
|
1348 * ^ ^ ^ |
|
1349 * | | | |
|
1350 * less k great |
|
1351 * |
1226 * Invariants: |
1352 * Invariants: |
|
1353 * |
1227 * all in (left, less) < pivot1 |
1354 * all in (left, less) < pivot1 |
1228 * pivot1 <= all in [less, k) <= pivot2 |
1355 * pivot1 <= all in [less, k) <= pivot2 |
1229 * all in (great, right) > pivot2 |
1356 * all in (great, right) > pivot2 |
1230 * |
1357 * |
1231 * Pointer k is the first index of ?-part |
1358 * Pointer k is the first index of ?-part |
1232 */ |
1359 */ |
1233 outer: |
1360 outer: |
1234 for (int k = less; k <= great; k++) { |
1361 for (int k = less; k <= great; k++) { |
1235 byte ak = a[k]; |
1362 byte ak = a[k]; |
1236 if (ak < pivot1) { |
1363 if (ak < pivot1) { // Move a[k] to left part |
1237 if (k > less) { |
1364 if (k != less) { |
1238 a[k] = a[less]; |
1365 a[k] = a[less]; |
1239 a[less] = ak; |
1366 a[less] = ak; |
1240 } |
1367 } |
1241 less++; |
1368 less++; |
1242 } else if (ak > pivot2) { |
1369 } else if (ak > pivot2) { // Move a[k] to right part |
1243 while (a[great] > pivot2) { |
1370 while (a[great] > pivot2) { |
1244 if (k == great--) { |
1371 if (great-- == k) { |
1245 break outer; |
1372 break outer; |
1246 } |
1373 } |
1247 } |
1374 } |
1248 a[k] = a[great]; |
1375 if (a[great] < pivot1) { |
1249 a[great--] = ak; |
1376 a[k] = a[less]; |
1250 |
1377 a[less++] = a[great]; |
1251 if ((ak = a[k]) < pivot1) { |
1378 a[great--] = ak; |
1252 a[k] = a[less]; |
1379 } else { // pivot1 <= a[great] <= pivot2 |
1253 a[less++] = ak; |
1380 a[k] = a[great]; |
|
1381 a[great--] = ak; |
1254 } |
1382 } |
1255 } |
1383 } |
1256 } |
1384 } |
1257 } else { // Pivots are equal |
1385 } else { // Pivots are equal |
1258 /* |
1386 /* |
1259 * Partition degenerates to the traditional 3-way |
1387 * Partition degenerates to the traditional 3-way, |
1260 * (or "Dutch National Flag") partition: |
1388 * or "Dutch National Flag", partition: |
1261 * |
1389 * |
1262 * left part center part right part |
1390 * left part center part right part |
1263 * ------------------------------------------------- |
1391 * +----------------------------------------------+ |
1264 * [ < pivot | == pivot | ? | > pivot ] |
1392 * | < pivot | == pivot | ? | > pivot | |
1265 * ------------------------------------------------- |
1393 * +----------------------------------------------+ |
1266 * |
|
1267 * ^ ^ ^ |
1394 * ^ ^ ^ |
1268 * | | | |
1395 * | | | |
1269 * less k great |
1396 * less k great |
1270 * |
1397 * |
1271 * Invariants: |
1398 * Invariants: |
1272 * |
1399 * |
1273 * all in (left, less) < pivot |
1400 * all in (left, less) < pivot |
1274 * all in [less, k) == pivot |
1401 * all in [less, k) == pivot |
1275 * all in (great, right) > pivot |
1402 * all in (great, right) > pivot |
|
1403 * |
|
1404 * Pointer k is the first index of ?-part |
|
1405 */ |
|
1406 for (int k = less; k <= great; k++) { |
|
1407 byte ak = a[k]; |
|
1408 if (ak == pivot1) { |
|
1409 continue; |
|
1410 } |
|
1411 if (ak < pivot1) { // Move a[k] to left part |
|
1412 if (k != less) { |
|
1413 a[k] = a[less]; |
|
1414 a[less] = ak; |
|
1415 } |
|
1416 less++; |
|
1417 } else { // (a[k] > pivot1) - Move a[k] to right part |
|
1418 /* |
|
1419 * We know that pivot1 == a[e3] == pivot2. Thus, we know |
|
1420 * that great will still be >= k when the following loop |
|
1421 * terminates, even though we don't test for it explicitly. |
|
1422 * In other words, a[e3] acts as a sentinel for great. |
|
1423 */ |
|
1424 while (a[great] > pivot1) { |
|
1425 great--; |
|
1426 } |
|
1427 if (a[great] < pivot1) { |
|
1428 a[k] = a[less]; |
|
1429 a[less++] = a[great]; |
|
1430 a[great--] = ak; |
|
1431 } else { // a[great] == pivot1 |
|
1432 a[k] = pivot1; |
|
1433 a[great--] = ak; |
|
1434 } |
|
1435 } |
|
1436 } |
|
1437 } |
|
1438 |
|
1439 // Swap pivots into their final positions |
|
1440 a[left] = a[less - 1]; a[less - 1] = pivot1; |
|
1441 a[right] = a[great + 1]; a[great + 1] = pivot2; |
|
1442 |
|
1443 // Sort left and right parts recursively, excluding known pivot values |
|
1444 doSort(a, left, less - 2); |
|
1445 doSort(a, great + 2, right); |
|
1446 |
|
1447 /* |
|
1448 * If pivot1 == pivot2, all elements from center |
|
1449 * part are equal and, therefore, already sorted |
|
1450 */ |
|
1451 if (!pivotsDiffer) { |
|
1452 return; |
|
1453 } |
|
1454 |
|
1455 /* |
|
1456 * If center part is too large (comprises > 2/3 of the array), |
|
1457 * swap internal pivot values to ends |
|
1458 */ |
|
1459 if (less < e1 && great > e5) { |
|
1460 while (a[less] == pivot1) { |
|
1461 less++; |
|
1462 } |
|
1463 while (a[great] == pivot2) { |
|
1464 great--; |
|
1465 } |
|
1466 |
|
1467 /* |
|
1468 * Partitioning: |
|
1469 * |
|
1470 * left part center part right part |
|
1471 * +----------------------------------------------------------+ |
|
1472 * | == pivot1 | pivot1 < && < pivot2 | ? | == pivot2 | |
|
1473 * +----------------------------------------------------------+ |
|
1474 * ^ ^ ^ |
|
1475 * | | | |
|
1476 * less k great |
|
1477 * |
|
1478 * Invariants: |
|
1479 * |
|
1480 * all in (*, less) == pivot1 |
|
1481 * pivot1 < all in [less, k) < pivot2 |
|
1482 * all in (great, *) == pivot2 |
1276 * |
1483 * |
1277 * Pointer k is the first index of ?-part |
1484 * Pointer k is the first index of ?-part |
1278 */ |
1485 */ |
1279 outer: |
1486 outer: |
1280 for (int k = less; k <= great; k++) { |
1487 for (int k = less; k <= great; k++) { |
1281 byte ak = a[k]; |
1488 byte ak = a[k]; |
1282 if (ak == pivot1) { |
1489 if (ak == pivot2) { // Move a[k] to right part |
1283 continue; |
1490 while (a[great] == pivot2) { |
1284 } |
1491 if (great-- == k) { |
1285 if (ak < pivot1) { |
|
1286 if (k > less) { |
|
1287 a[k] = a[less]; |
|
1288 a[less] = ak; |
|
1289 } |
|
1290 less++; |
|
1291 } else { // a[k] > pivot |
|
1292 while (a[great] > pivot1) { |
|
1293 if (k == great--) { |
|
1294 break outer; |
1492 break outer; |
1295 } |
1493 } |
1296 } |
1494 } |
1297 a[k] = a[great]; |
1495 if (a[great] == pivot1) { |
1298 a[great--] = ak; |
1496 a[k] = a[less]; |
1299 |
1497 a[less++] = pivot1; |
1300 if ((ak = a[k]) < pivot1) { |
1498 } else { // pivot1 < a[great] < pivot2 |
1301 a[k] = a[less]; |
1499 a[k] = a[great]; |
1302 a[less++] = ak; |
1500 } |
1303 } |
1501 a[great--] = pivot2; |
1304 } |
1502 } else if (ak == pivot1) { // Move a[k] to left part |
1305 } |
1503 a[k] = a[less]; |
1306 } |
|
1307 |
|
1308 // Swap pivots into their final positions |
|
1309 a[left] = a[less - 1]; a[less - 1] = pivot1; |
|
1310 a[right] = a[great + 1]; a[great + 1] = pivot2; |
|
1311 |
|
1312 // Sort left and right parts recursively, excluding known pivot values |
|
1313 doSort(a, left, less - 2); |
|
1314 doSort(a, great + 2, right); |
|
1315 |
|
1316 /* |
|
1317 * If pivot1 == pivot2, all elements from center |
|
1318 * part are equal and, therefore, already sorted |
|
1319 */ |
|
1320 if (!pivotsDiffer) { |
|
1321 return; |
|
1322 } |
|
1323 |
|
1324 /* |
|
1325 * If center part is too large (comprises > 5/6 of |
|
1326 * the array), swap internal pivot values to ends |
|
1327 */ |
|
1328 if (less < e1 && e5 < great) { |
|
1329 while (a[less] == pivot1) { |
|
1330 less++; |
|
1331 } |
|
1332 while (a[great] == pivot2) { |
|
1333 great--; |
|
1334 } |
|
1335 for (int k = less + 1; k <= great; ) { |
|
1336 byte ak = a[k]; |
|
1337 if (ak == pivot1) { |
|
1338 a[k++] = a[less]; |
|
1339 a[less++] = pivot1; |
1504 a[less++] = pivot1; |
1340 } else if (ak == pivot2) { |
|
1341 a[k] = a[great]; |
|
1342 a[great--] = pivot2; |
|
1343 } else { |
|
1344 k++; |
|
1345 } |
1505 } |
1346 } |
1506 } |
1347 } |
1507 } |
1348 |
1508 |
1349 // Sort center part recursively, excluding known pivot values |
1509 // Sort center part recursively, excluding known pivot values |
1534 * Use the second and fourth of the five sorted elements as pivots. |
1694 * Use the second and fourth of the five sorted elements as pivots. |
1535 * These values are inexpensive approximations of the first and |
1695 * These values are inexpensive approximations of the first and |
1536 * second terciles of the array. Note that pivot1 <= pivot2. |
1696 * second terciles of the array. Note that pivot1 <= pivot2. |
1537 * |
1697 * |
1538 * The pivots are stored in local variables, and the first and |
1698 * The pivots are stored in local variables, and the first and |
1539 * the last of the sorted elements are moved to the locations |
1699 * the last of the elements to be sorted are moved to the locations |
1540 * formerly occupied by the pivots. When partitioning is complete, |
1700 * formerly occupied by the pivots. When partitioning is complete, |
1541 * the pivots are swapped back into their final positions, and |
1701 * the pivots are swapped back into their final positions, and |
1542 * excluded from subsequent sorting. |
1702 * excluded from subsequent sorting. |
1543 */ |
1703 */ |
1544 float pivot1 = ae2; a[e2] = a[left]; |
1704 float pivot1 = ae2; a[e2] = a[left]; |
1545 float pivot2 = ae4; a[e4] = a[right]; |
1705 float pivot2 = ae4; a[e4] = a[right]; |
1546 |
1706 |
1547 /* |
|
1548 * Partitioning |
|
1549 * |
|
1550 * left part center part right part |
|
1551 * ------------------------------------------------------------ |
|
1552 * [ < pivot1 | pivot1 <= && <= pivot2 | ? | > pivot2 ] |
|
1553 * ------------------------------------------------------------ |
|
1554 * ^ ^ ^ |
|
1555 * | | | |
|
1556 * less k great |
|
1557 */ |
|
1558 |
|
1559 // Pointers |
1707 // Pointers |
1560 int less = left + 1; // The index of first element of center part |
1708 int less = left + 1; // The index of first element of center part |
1561 int great = right - 1; // The index before first element of right part |
1709 int great = right - 1; // The index before first element of right part |
1562 |
1710 |
1563 boolean pivotsDiffer = pivot1 != pivot2; |
1711 boolean pivotsDiffer = (pivot1 != pivot2); |
1564 |
1712 |
1565 if (pivotsDiffer) { |
1713 if (pivotsDiffer) { |
1566 /* |
1714 /* |
|
1715 * Partitioning: |
|
1716 * |
|
1717 * left part center part right part |
|
1718 * +------------------------------------------------------------+ |
|
1719 * | < pivot1 | pivot1 <= && <= pivot2 | ? | > pivot2 | |
|
1720 * +------------------------------------------------------------+ |
|
1721 * ^ ^ ^ |
|
1722 * | | | |
|
1723 * less k great |
|
1724 * |
1567 * Invariants: |
1725 * Invariants: |
|
1726 * |
1568 * all in (left, less) < pivot1 |
1727 * all in (left, less) < pivot1 |
1569 * pivot1 <= all in [less, k) <= pivot2 |
1728 * pivot1 <= all in [less, k) <= pivot2 |
1570 * all in (great, right) > pivot2 |
1729 * all in (great, right) > pivot2 |
1571 * |
1730 * |
1572 * Pointer k is the first index of ?-part |
1731 * Pointer k is the first index of ?-part |
1573 */ |
1732 */ |
1574 outer: |
1733 outer: |
1575 for (int k = less; k <= great; k++) { |
1734 for (int k = less; k <= great; k++) { |
1576 float ak = a[k]; |
1735 float ak = a[k]; |
1577 if (ak < pivot1) { |
1736 if (ak < pivot1) { // Move a[k] to left part |
1578 if (k > less) { |
1737 if (k != less) { |
1579 a[k] = a[less]; |
1738 a[k] = a[less]; |
1580 a[less] = ak; |
1739 a[less] = ak; |
1581 } |
1740 } |
1582 less++; |
1741 less++; |
1583 } else if (ak > pivot2) { |
1742 } else if (ak > pivot2) { // Move a[k] to right part |
1584 while (a[great] > pivot2) { |
1743 while (a[great] > pivot2) { |
1585 if (k == great--) { |
1744 if (great-- == k) { |
1586 break outer; |
1745 break outer; |
1587 } |
1746 } |
1588 } |
1747 } |
1589 a[k] = a[great]; |
1748 if (a[great] < pivot1) { |
1590 a[great--] = ak; |
1749 a[k] = a[less]; |
1591 |
1750 a[less++] = a[great]; |
1592 if ((ak = a[k]) < pivot1) { |
1751 a[great--] = ak; |
1593 a[k] = a[less]; |
1752 } else { // pivot1 <= a[great] <= pivot2 |
1594 a[less++] = ak; |
1753 a[k] = a[great]; |
|
1754 a[great--] = ak; |
1595 } |
1755 } |
1596 } |
1756 } |
1597 } |
1757 } |
1598 } else { // Pivots are equal |
1758 } else { // Pivots are equal |
1599 /* |
1759 /* |
1600 * Partition degenerates to the traditional 3-way |
1760 * Partition degenerates to the traditional 3-way, |
1601 * (or "Dutch National Flag") partition: |
1761 * or "Dutch National Flag", partition: |
1602 * |
1762 * |
1603 * left part center part right part |
1763 * left part center part right part |
1604 * ------------------------------------------------- |
1764 * +----------------------------------------------+ |
1605 * [ < pivot | == pivot | ? | > pivot ] |
1765 * | < pivot | == pivot | ? | > pivot | |
1606 * ------------------------------------------------- |
1766 * +----------------------------------------------+ |
1607 * |
|
1608 * ^ ^ ^ |
1767 * ^ ^ ^ |
1609 * | | | |
1768 * | | | |
1610 * less k great |
1769 * less k great |
1611 * |
1770 * |
1612 * Invariants: |
1771 * Invariants: |
1613 * |
1772 * |
1614 * all in (left, less) < pivot |
1773 * all in (left, less) < pivot |
1615 * all in [less, k) == pivot |
1774 * all in [less, k) == pivot |
1616 * all in (great, right) > pivot |
1775 * all in (great, right) > pivot |
|
1776 * |
|
1777 * Pointer k is the first index of ?-part |
|
1778 */ |
|
1779 for (int k = less; k <= great; k++) { |
|
1780 float ak = a[k]; |
|
1781 if (ak == pivot1) { |
|
1782 continue; |
|
1783 } |
|
1784 if (ak < pivot1) { // Move a[k] to left part |
|
1785 if (k != less) { |
|
1786 a[k] = a[less]; |
|
1787 a[less] = ak; |
|
1788 } |
|
1789 less++; |
|
1790 } else { // (a[k] > pivot1) - Move a[k] to right part |
|
1791 /* |
|
1792 * We know that pivot1 == a[e3] == pivot2. Thus, we know |
|
1793 * that great will still be >= k when the following loop |
|
1794 * terminates, even though we don't test for it explicitly. |
|
1795 * In other words, a[e3] acts as a sentinel for great. |
|
1796 */ |
|
1797 while (a[great] > pivot1) { |
|
1798 great--; |
|
1799 } |
|
1800 if (a[great] < pivot1) { |
|
1801 a[k] = a[less]; |
|
1802 a[less++] = a[great]; |
|
1803 a[great--] = ak; |
|
1804 } else { // a[great] == pivot1 |
|
1805 a[k] = pivot1; |
|
1806 a[great--] = ak; |
|
1807 } |
|
1808 } |
|
1809 } |
|
1810 } |
|
1811 |
|
1812 // Swap pivots into their final positions |
|
1813 a[left] = a[less - 1]; a[less - 1] = pivot1; |
|
1814 a[right] = a[great + 1]; a[great + 1] = pivot2; |
|
1815 |
|
1816 // Sort left and right parts recursively, excluding known pivot values |
|
1817 doSort(a, left, less - 2); |
|
1818 doSort(a, great + 2, right); |
|
1819 |
|
1820 /* |
|
1821 * If pivot1 == pivot2, all elements from center |
|
1822 * part are equal and, therefore, already sorted |
|
1823 */ |
|
1824 if (!pivotsDiffer) { |
|
1825 return; |
|
1826 } |
|
1827 |
|
1828 /* |
|
1829 * If center part is too large (comprises > 2/3 of the array), |
|
1830 * swap internal pivot values to ends |
|
1831 */ |
|
1832 if (less < e1 && great > e5) { |
|
1833 while (a[less] == pivot1) { |
|
1834 less++; |
|
1835 } |
|
1836 while (a[great] == pivot2) { |
|
1837 great--; |
|
1838 } |
|
1839 |
|
1840 /* |
|
1841 * Partitioning: |
|
1842 * |
|
1843 * left part center part right part |
|
1844 * +----------------------------------------------------------+ |
|
1845 * | == pivot1 | pivot1 < && < pivot2 | ? | == pivot2 | |
|
1846 * +----------------------------------------------------------+ |
|
1847 * ^ ^ ^ |
|
1848 * | | | |
|
1849 * less k great |
|
1850 * |
|
1851 * Invariants: |
|
1852 * |
|
1853 * all in (*, less) == pivot1 |
|
1854 * pivot1 < all in [less, k) < pivot2 |
|
1855 * all in (great, *) == pivot2 |
1617 * |
1856 * |
1618 * Pointer k is the first index of ?-part |
1857 * Pointer k is the first index of ?-part |
1619 */ |
1858 */ |
1620 outer: |
1859 outer: |
1621 for (int k = less; k <= great; k++) { |
1860 for (int k = less; k <= great; k++) { |
1622 float ak = a[k]; |
1861 float ak = a[k]; |
1623 if (ak == pivot1) { |
1862 if (ak == pivot2) { // Move a[k] to right part |
1624 continue; |
1863 while (a[great] == pivot2) { |
1625 } |
1864 if (great-- == k) { |
1626 if (ak < pivot1) { |
|
1627 if (k > less) { |
|
1628 a[k] = a[less]; |
|
1629 a[less] = ak; |
|
1630 } |
|
1631 less++; |
|
1632 } else { // a[k] > pivot |
|
1633 while (a[great] > pivot1) { |
|
1634 if (k == great--) { |
|
1635 break outer; |
1865 break outer; |
1636 } |
1866 } |
1637 } |
1867 } |
1638 a[k] = a[great]; |
1868 if (a[great] == pivot1) { |
1639 a[great--] = ak; |
1869 a[k] = a[less]; |
1640 |
1870 a[less++] = pivot1; |
1641 if ((ak = a[k]) < pivot1) { |
1871 } else { // pivot1 < a[great] < pivot2 |
1642 a[k] = a[less]; |
1872 a[k] = a[great]; |
1643 a[less++] = ak; |
1873 } |
1644 } |
1874 a[great--] = pivot2; |
1645 } |
1875 } else if (ak == pivot1) { // Move a[k] to left part |
1646 } |
1876 a[k] = a[less]; |
1647 } |
|
1648 |
|
1649 // Swap pivots into their final positions |
|
1650 a[left] = a[less - 1]; a[less - 1] = pivot1; |
|
1651 a[right] = a[great + 1]; a[great + 1] = pivot2; |
|
1652 |
|
1653 // Sort left and right parts recursively, excluding known pivot values |
|
1654 doSort(a, left, less - 2); |
|
1655 doSort(a, great + 2, right); |
|
1656 |
|
1657 /* |
|
1658 * If pivot1 == pivot2, all elements from center |
|
1659 * part are equal and, therefore, already sorted |
|
1660 */ |
|
1661 if (!pivotsDiffer) { |
|
1662 return; |
|
1663 } |
|
1664 |
|
1665 /* |
|
1666 * If center part is too large (comprises > 5/6 of |
|
1667 * the array), swap internal pivot values to ends |
|
1668 */ |
|
1669 if (less < e1 && e5 < great) { |
|
1670 while (a[less] == pivot1) { |
|
1671 less++; |
|
1672 } |
|
1673 while (a[great] == pivot2) { |
|
1674 great--; |
|
1675 } |
|
1676 for (int k = less + 1; k <= great; ) { |
|
1677 float ak = a[k]; |
|
1678 if (ak == pivot1) { |
|
1679 a[k++] = a[less]; |
|
1680 a[less++] = pivot1; |
1877 a[less++] = pivot1; |
1681 } else if (ak == pivot2) { |
|
1682 a[k] = a[great]; |
|
1683 a[great--] = pivot2; |
|
1684 } else { |
|
1685 k++; |
|
1686 } |
1878 } |
1687 } |
1879 } |
1688 } |
1880 } |
1689 |
1881 |
1690 // Sort center part recursively, excluding known pivot values |
1882 // Sort center part recursively, excluding known pivot values |
1875 * Use the second and fourth of the five sorted elements as pivots. |
2067 * Use the second and fourth of the five sorted elements as pivots. |
1876 * These values are inexpensive approximations of the first and |
2068 * These values are inexpensive approximations of the first and |
1877 * second terciles of the array. Note that pivot1 <= pivot2. |
2069 * second terciles of the array. Note that pivot1 <= pivot2. |
1878 * |
2070 * |
1879 * The pivots are stored in local variables, and the first and |
2071 * The pivots are stored in local variables, and the first and |
1880 * the last of the sorted elements are moved to the locations |
2072 * the last of the elements to be sorted are moved to the locations |
1881 * formerly occupied by the pivots. When partitioning is complete, |
2073 * formerly occupied by the pivots. When partitioning is complete, |
1882 * the pivots are swapped back into their final positions, and |
2074 * the pivots are swapped back into their final positions, and |
1883 * excluded from subsequent sorting. |
2075 * excluded from subsequent sorting. |
1884 */ |
2076 */ |
1885 double pivot1 = ae2; a[e2] = a[left]; |
2077 double pivot1 = ae2; a[e2] = a[left]; |
1886 double pivot2 = ae4; a[e4] = a[right]; |
2078 double pivot2 = ae4; a[e4] = a[right]; |
1887 |
2079 |
1888 /* |
|
1889 * Partitioning |
|
1890 * |
|
1891 * left part center part right part |
|
1892 * ------------------------------------------------------------ |
|
1893 * [ < pivot1 | pivot1 <= && <= pivot2 | ? | > pivot2 ] |
|
1894 * ------------------------------------------------------------ |
|
1895 * ^ ^ ^ |
|
1896 * | | | |
|
1897 * less k great |
|
1898 */ |
|
1899 |
|
1900 // Pointers |
2080 // Pointers |
1901 int less = left + 1; // The index of first element of center part |
2081 int less = left + 1; // The index of first element of center part |
1902 int great = right - 1; // The index before first element of right part |
2082 int great = right - 1; // The index before first element of right part |
1903 |
2083 |
1904 boolean pivotsDiffer = pivot1 != pivot2; |
2084 boolean pivotsDiffer = (pivot1 != pivot2); |
1905 |
2085 |
1906 if (pivotsDiffer) { |
2086 if (pivotsDiffer) { |
1907 /* |
2087 /* |
|
2088 * Partitioning: |
|
2089 * |
|
2090 * left part center part right part |
|
2091 * +------------------------------------------------------------+ |
|
2092 * | < pivot1 | pivot1 <= && <= pivot2 | ? | > pivot2 | |
|
2093 * +------------------------------------------------------------+ |
|
2094 * ^ ^ ^ |
|
2095 * | | | |
|
2096 * less k great |
|
2097 * |
1908 * Invariants: |
2098 * Invariants: |
|
2099 * |
1909 * all in (left, less) < pivot1 |
2100 * all in (left, less) < pivot1 |
1910 * pivot1 <= all in [less, k) <= pivot2 |
2101 * pivot1 <= all in [less, k) <= pivot2 |
1911 * all in (great, right) > pivot2 |
2102 * all in (great, right) > pivot2 |
1912 * |
2103 * |
1913 * Pointer k is the first index of ?-part |
2104 * Pointer k is the first index of ?-part |
1914 */ |
2105 */ |
1915 outer: |
2106 outer: |
1916 for (int k = less; k <= great; k++) { |
2107 for (int k = less; k <= great; k++) { |
1917 double ak = a[k]; |
2108 double ak = a[k]; |
1918 if (ak < pivot1) { |
2109 if (ak < pivot1) { // Move a[k] to left part |
1919 if (k > less) { |
2110 if (k != less) { |
1920 a[k] = a[less]; |
2111 a[k] = a[less]; |
1921 a[less] = ak; |
2112 a[less] = ak; |
1922 } |
2113 } |
1923 less++; |
2114 less++; |
1924 } else if (ak > pivot2) { |
2115 } else if (ak > pivot2) { // Move a[k] to right part |
1925 while (a[great] > pivot2) { |
2116 while (a[great] > pivot2) { |
1926 if (k == great--) { |
2117 if (great-- == k) { |
1927 break outer; |
2118 break outer; |
1928 } |
2119 } |
1929 } |
2120 } |
1930 a[k] = a[great]; |
2121 if (a[great] < pivot1) { |
1931 a[great--] = ak; |
2122 a[k] = a[less]; |
1932 |
2123 a[less++] = a[great]; |
1933 if ((ak = a[k]) < pivot1) { |
2124 a[great--] = ak; |
1934 a[k] = a[less]; |
2125 } else { // pivot1 <= a[great] <= pivot2 |
1935 a[less++] = ak; |
2126 a[k] = a[great]; |
|
2127 a[great--] = ak; |
1936 } |
2128 } |
1937 } |
2129 } |
1938 } |
2130 } |
1939 } else { // Pivots are equal |
2131 } else { // Pivots are equal |
1940 /* |
2132 /* |
1941 * Partition degenerates to the traditional 3-way |
2133 * Partition degenerates to the traditional 3-way, |
1942 * (or "Dutch National Flag") partition: |
2134 * or "Dutch National Flag", partition: |
1943 * |
2135 * |
1944 * left part center part right part |
2136 * left part center part right part |
1945 * ------------------------------------------------- |
2137 * +----------------------------------------------+ |
1946 * [ < pivot | == pivot | ? | > pivot ] |
2138 * | < pivot | == pivot | ? | > pivot | |
1947 * ------------------------------------------------- |
2139 * +----------------------------------------------+ |
1948 * |
|
1949 * ^ ^ ^ |
2140 * ^ ^ ^ |
1950 * | | | |
2141 * | | | |
1951 * less k great |
2142 * less k great |
1952 * |
2143 * |
1953 * Invariants: |
2144 * Invariants: |
1954 * |
2145 * |
1955 * all in (left, less) < pivot |
2146 * all in (left, less) < pivot |
1956 * all in [less, k) == pivot |
2147 * all in [less, k) == pivot |
1957 * all in (great, right) > pivot |
2148 * all in (great, right) > pivot |
|
2149 * |
|
2150 * Pointer k is the first index of ?-part |
|
2151 */ |
|
2152 for (int k = less; k <= great; k++) { |
|
2153 double ak = a[k]; |
|
2154 if (ak == pivot1) { |
|
2155 continue; |
|
2156 } |
|
2157 if (ak < pivot1) { // Move a[k] to left part |
|
2158 if (k != less) { |
|
2159 a[k] = a[less]; |
|
2160 a[less] = ak; |
|
2161 } |
|
2162 less++; |
|
2163 } else { // (a[k] > pivot1) - Move a[k] to right part |
|
2164 /* |
|
2165 * We know that pivot1 == a[e3] == pivot2. Thus, we know |
|
2166 * that great will still be >= k when the following loop |
|
2167 * terminates, even though we don't test for it explicitly. |
|
2168 * In other words, a[e3] acts as a sentinel for great. |
|
2169 */ |
|
2170 while (a[great] > pivot1) { |
|
2171 great--; |
|
2172 } |
|
2173 if (a[great] < pivot1) { |
|
2174 a[k] = a[less]; |
|
2175 a[less++] = a[great]; |
|
2176 a[great--] = ak; |
|
2177 } else { // a[great] == pivot1 |
|
2178 a[k] = pivot1; |
|
2179 a[great--] = ak; |
|
2180 } |
|
2181 } |
|
2182 } |
|
2183 } |
|
2184 |
|
2185 // Swap pivots into their final positions |
|
2186 a[left] = a[less - 1]; a[less - 1] = pivot1; |
|
2187 a[right] = a[great + 1]; a[great + 1] = pivot2; |
|
2188 |
|
2189 // Sort left and right parts recursively, excluding known pivot values |
|
2190 doSort(a, left, less - 2); |
|
2191 doSort(a, great + 2, right); |
|
2192 |
|
2193 /* |
|
2194 * If pivot1 == pivot2, all elements from center |
|
2195 * part are equal and, therefore, already sorted |
|
2196 */ |
|
2197 if (!pivotsDiffer) { |
|
2198 return; |
|
2199 } |
|
2200 |
|
2201 /* |
|
2202 * If center part is too large (comprises > 2/3 of the array), |
|
2203 * swap internal pivot values to ends |
|
2204 */ |
|
2205 if (less < e1 && great > e5) { |
|
2206 while (a[less] == pivot1) { |
|
2207 less++; |
|
2208 } |
|
2209 while (a[great] == pivot2) { |
|
2210 great--; |
|
2211 } |
|
2212 |
|
2213 /* |
|
2214 * Partitioning: |
|
2215 * |
|
2216 * left part center part right part |
|
2217 * +----------------------------------------------------------+ |
|
2218 * | == pivot1 | pivot1 < && < pivot2 | ? | == pivot2 | |
|
2219 * +----------------------------------------------------------+ |
|
2220 * ^ ^ ^ |
|
2221 * | | | |
|
2222 * less k great |
|
2223 * |
|
2224 * Invariants: |
|
2225 * |
|
2226 * all in (*, less) == pivot1 |
|
2227 * pivot1 < all in [less, k) < pivot2 |
|
2228 * all in (great, *) == pivot2 |
1958 * |
2229 * |
1959 * Pointer k is the first index of ?-part |
2230 * Pointer k is the first index of ?-part |
1960 */ |
2231 */ |
1961 outer: |
2232 outer: |
1962 for (int k = less; k <= great; k++) { |
2233 for (int k = less; k <= great; k++) { |
1963 double ak = a[k]; |
2234 double ak = a[k]; |
1964 if (ak == pivot1) { |
2235 if (ak == pivot2) { // Move a[k] to right part |
1965 continue; |
2236 while (a[great] == pivot2) { |
1966 } |
2237 if (great-- == k) { |
1967 if (ak < pivot1) { |
|
1968 if (k > less) { |
|
1969 a[k] = a[less]; |
|
1970 a[less] = ak; |
|
1971 } |
|
1972 less++; |
|
1973 } else { // a[k] > pivot |
|
1974 while (a[great] > pivot1) { |
|
1975 if (k == great--) { |
|
1976 break outer; |
2238 break outer; |
1977 } |
2239 } |
1978 } |
2240 } |
1979 a[k] = a[great]; |
2241 if (a[great] == pivot1) { |
1980 a[great--] = ak; |
2242 a[k] = a[less]; |
1981 |
2243 a[less++] = pivot1; |
1982 if ((ak = a[k]) < pivot1) { |
2244 } else { // pivot1 < a[great] < pivot2 |
1983 a[k] = a[less]; |
2245 a[k] = a[great]; |
1984 a[less++] = ak; |
2246 } |
1985 } |
2247 a[great--] = pivot2; |
1986 } |
2248 } else if (ak == pivot1) { // Move a[k] to left part |
1987 } |
2249 a[k] = a[less]; |
1988 } |
|
1989 |
|
1990 // Swap pivots into their final positions |
|
1991 a[left] = a[less - 1]; a[less - 1] = pivot1; |
|
1992 a[right] = a[great + 1]; a[great + 1] = pivot2; |
|
1993 |
|
1994 // Sort left and right parts recursively, excluding known pivot values |
|
1995 doSort(a, left, less - 2); |
|
1996 doSort(a, great + 2, right); |
|
1997 |
|
1998 /* |
|
1999 * If pivot1 == pivot2, all elements from center |
|
2000 * part are equal and, therefore, already sorted |
|
2001 */ |
|
2002 if (!pivotsDiffer) { |
|
2003 return; |
|
2004 } |
|
2005 |
|
2006 /* |
|
2007 * If center part is too large (comprises > 5/6 of |
|
2008 * the array), swap internal pivot values to ends |
|
2009 */ |
|
2010 if (less < e1 && e5 < great) { |
|
2011 while (a[less] == pivot1) { |
|
2012 less++; |
|
2013 } |
|
2014 while (a[great] == pivot2) { |
|
2015 great--; |
|
2016 } |
|
2017 for (int k = less + 1; k <= great; ) { |
|
2018 double ak = a[k]; |
|
2019 if (ak == pivot1) { |
|
2020 a[k++] = a[less]; |
|
2021 a[less++] = pivot1; |
2250 a[less++] = pivot1; |
2022 } else if (ak == pivot2) { |
|
2023 a[k] = a[great]; |
|
2024 a[great--] = pivot2; |
|
2025 } else { |
|
2026 k++; |
|
2027 } |
2251 } |
2028 } |
2252 } |
2029 } |
2253 } |
2030 |
2254 |
2031 // Sort center part recursively, excluding known pivot values |
2255 // Sort center part recursively, excluding known pivot values |