1681 |
|
l+h, n-h, l, g, null).compute(); |
1682 |
|
} |
1683 |
|
else |
1684 |
< |
oquickSort(a, cmp, l, l+n-1); |
1684 |
> |
Arrays.sort(a, l, l+n, cmp); |
1685 |
|
} |
1686 |
|
} |
1687 |
|
|
1717 |
|
l+h, n-h, l, g, null).compute(); |
1718 |
|
} |
1719 |
|
else |
1720 |
< |
ocquickSort(a, l, l+n-1); |
1720 |
> |
Arrays.sort(a, l, l+n); |
1721 |
|
} |
1722 |
|
} |
1723 |
|
|
1754 |
|
l+h, n-h, l, g, null).compute(); |
1755 |
|
} |
1756 |
|
else |
1757 |
< |
dquickSort(a, cmp, l, l+n-1); |
1757 |
> |
Arrays.sort(a, l, l+n, cmp); |
1758 |
|
} |
1759 |
|
} |
1760 |
|
|
1790 |
|
l+h, n-h, l, g, null).compute(); |
1791 |
|
} |
1792 |
|
else |
1793 |
< |
dcquickSort(a, l, l+n-1); |
1793 |
> |
Arrays.sort(a, l, l+n); |
1794 |
|
} |
1795 |
|
} |
1796 |
|
|
1828 |
|
l+h, n-h, l, g, null).compute(); |
1829 |
|
} |
1830 |
|
else |
1831 |
< |
lquickSort(a, cmp, l, l+n-1); |
1831 |
> |
Arrays.sort(a, l, l+n, cmp); |
1832 |
|
} |
1833 |
|
} |
1834 |
|
|
1865 |
|
l+h, n-h, l, g, null).compute(); |
1866 |
|
} |
1867 |
|
else |
1868 |
< |
lcquickSort(a, l, l+n-1); |
1868 |
> |
Arrays.sort(a, l, l+n); |
1869 |
|
} |
1870 |
|
} |
1871 |
|
|
2301 |
|
} |
2302 |
|
} |
2303 |
|
} |
2304 |
– |
|
2305 |
– |
/** Cutoff for when to use insertion-sort instead of quicksort */ |
2306 |
– |
static final int INSERTION_SORT_THRESHOLD = 8; |
2307 |
– |
|
2308 |
– |
// Six nearly identical versions of quicksort |
2309 |
– |
|
2310 |
– |
static void oquickSort(Object[] a, Comparator cmp, int lo, int hi) { |
2311 |
– |
for (;;) { |
2312 |
– |
if (hi - lo <= INSERTION_SORT_THRESHOLD) { |
2313 |
– |
for (int i = lo + 1; i <= hi; i++) { |
2314 |
– |
Object t = a[i]; |
2315 |
– |
int j = i - 1; |
2316 |
– |
while (j >= lo && cmp.compare(t, a[j]) < 0) { |
2317 |
– |
a[j+1] = a[j]; |
2318 |
– |
--j; |
2319 |
– |
} |
2320 |
– |
a[j+1] = t; |
2321 |
– |
} |
2322 |
– |
return; |
2323 |
– |
} |
2324 |
– |
|
2325 |
– |
int mid = (lo + hi) >>> 1; |
2326 |
– |
if (cmp.compare(a[lo], a[mid]) > 0) { |
2327 |
– |
Object t = a[lo]; a[lo] = a[mid]; a[mid] = t; |
2328 |
– |
} |
2329 |
– |
if (cmp.compare(a[mid], a[hi]) > 0) { |
2330 |
– |
Object t = a[mid]; a[mid] = a[hi]; a[hi] = t; |
2331 |
– |
if (cmp.compare(a[lo], a[mid]) > 0) { |
2332 |
– |
Object u = a[lo]; a[lo] = a[mid]; a[mid] = u; |
2333 |
– |
} |
2334 |
– |
} |
2335 |
– |
|
2336 |
– |
Object pivot = a[mid]; |
2337 |
– |
int left = lo+1; |
2338 |
– |
int right = hi-1; |
2339 |
– |
for (;;) { |
2340 |
– |
while (cmp.compare(pivot, a[right]) < 0) |
2341 |
– |
--right; |
2342 |
– |
while (left < right && cmp.compare(pivot, a[left]) >= 0) |
2343 |
– |
++left; |
2344 |
– |
if (left < right) { |
2345 |
– |
Object t = a[left]; a[left] = a[right]; a[right] = t; |
2346 |
– |
--right; |
2347 |
– |
} |
2348 |
– |
else break; |
2349 |
– |
} |
2350 |
– |
|
2351 |
– |
if (left - lo <= hi - right) { |
2352 |
– |
oquickSort(a, cmp, lo, left); |
2353 |
– |
lo = left + 1; |
2354 |
– |
} |
2355 |
– |
else { |
2356 |
– |
oquickSort(a, cmp, right, hi); |
2357 |
– |
hi = left; |
2358 |
– |
} |
2359 |
– |
} |
2360 |
– |
} |
2361 |
– |
|
2362 |
– |
static void ocquickSort(Comparable[] a, int lo, int hi) { |
2363 |
– |
for (;;) { |
2364 |
– |
if (hi - lo <= INSERTION_SORT_THRESHOLD) { |
2365 |
– |
for (int i = lo + 1; i <= hi; i++) { |
2366 |
– |
Comparable t = a[i]; |
2367 |
– |
int j = i - 1; |
2368 |
– |
while (j >= lo && t.compareTo(a[j]) < 0) { |
2369 |
– |
a[j+1] = a[j]; |
2370 |
– |
--j; |
2371 |
– |
} |
2372 |
– |
a[j+1] = t; |
2373 |
– |
} |
2374 |
– |
return; |
2375 |
– |
} |
2376 |
– |
|
2377 |
– |
int mid = (lo + hi) >>> 1; |
2378 |
– |
if (a[lo].compareTo(a[mid]) > 0) { |
2379 |
– |
Comparable t = a[lo]; a[lo] = a[mid]; a[mid] = t; |
2380 |
– |
} |
2381 |
– |
if (a[mid].compareTo(a[hi]) > 0) { |
2382 |
– |
Comparable t = a[mid]; a[mid] = a[hi]; a[hi] = t; |
2383 |
– |
if (a[lo].compareTo(a[mid]) > 0) { |
2384 |
– |
Comparable u = a[lo]; a[lo] = a[mid]; a[mid] = u; |
2385 |
– |
} |
2386 |
– |
} |
2387 |
– |
|
2388 |
– |
Comparable pivot = a[mid]; |
2389 |
– |
int left = lo+1; |
2390 |
– |
int right = hi-1; |
2391 |
– |
for (;;) { |
2392 |
– |
while (pivot.compareTo(a[right]) < 0) |
2393 |
– |
--right; |
2394 |
– |
while (left < right && pivot.compareTo(a[left]) >= 0) |
2395 |
– |
++left; |
2396 |
– |
if (left < right) { |
2397 |
– |
Comparable t = a[left]; a[left] = a[right]; a[right] = t; |
2398 |
– |
--right; |
2399 |
– |
} |
2400 |
– |
else break; |
2401 |
– |
} |
2402 |
– |
|
2403 |
– |
if (left - lo <= hi - right) { |
2404 |
– |
ocquickSort(a, lo, left); |
2405 |
– |
lo = left + 1; |
2406 |
– |
} |
2407 |
– |
else { |
2408 |
– |
ocquickSort(a, right, hi); |
2409 |
– |
hi = left; |
2410 |
– |
} |
2411 |
– |
} |
2412 |
– |
} |
2413 |
– |
|
2414 |
– |
static void dquickSort(double[] a, DoubleComparator cmp, int lo, int hi) { |
2415 |
– |
for (;;) { |
2416 |
– |
if (hi - lo <= INSERTION_SORT_THRESHOLD) { |
2417 |
– |
for (int i = lo + 1; i <= hi; i++) { |
2418 |
– |
double t = a[i]; |
2419 |
– |
int j = i - 1; |
2420 |
– |
while (j >= lo && cmp.compare(t, a[j]) < 0) { |
2421 |
– |
a[j+1] = a[j]; |
2422 |
– |
--j; |
2423 |
– |
} |
2424 |
– |
a[j+1] = t; |
2425 |
– |
} |
2426 |
– |
return; |
2427 |
– |
} |
2428 |
– |
|
2429 |
– |
int mid = (lo + hi) >>> 1; |
2430 |
– |
if (cmp.compare(a[lo], a[mid]) > 0) { |
2431 |
– |
double t = a[lo]; a[lo] = a[mid]; a[mid] = t; |
2432 |
– |
} |
2433 |
– |
if (cmp.compare(a[mid], a[hi]) > 0) { |
2434 |
– |
double t = a[mid]; a[mid] = a[hi]; a[hi] = t; |
2435 |
– |
if (cmp.compare(a[lo], a[mid]) > 0) { |
2436 |
– |
double u = a[lo]; a[lo] = a[mid]; a[mid] = u; |
2437 |
– |
} |
2438 |
– |
} |
2439 |
– |
|
2440 |
– |
double pivot = a[mid]; |
2441 |
– |
int left = lo+1; |
2442 |
– |
int right = hi-1; |
2443 |
– |
for (;;) { |
2444 |
– |
while (cmp.compare(pivot, a[right]) < 0) |
2445 |
– |
--right; |
2446 |
– |
while (left < right && cmp.compare(pivot, a[left]) >= 0) |
2447 |
– |
++left; |
2448 |
– |
if (left < right) { |
2449 |
– |
double t = a[left]; a[left] = a[right]; a[right] = t; |
2450 |
– |
--right; |
2451 |
– |
} |
2452 |
– |
else break; |
2453 |
– |
} |
2454 |
– |
|
2455 |
– |
if (left - lo <= hi - right) { |
2456 |
– |
dquickSort(a, cmp, lo, left); |
2457 |
– |
lo = left + 1; |
2458 |
– |
} |
2459 |
– |
else { |
2460 |
– |
dquickSort(a, cmp, right, hi); |
2461 |
– |
hi = left; |
2462 |
– |
} |
2463 |
– |
} |
2464 |
– |
} |
2465 |
– |
|
2466 |
– |
static void dcquickSort(double[] a, int lo, int hi) { |
2467 |
– |
for (;;) { |
2468 |
– |
if (hi - lo <= INSERTION_SORT_THRESHOLD) { |
2469 |
– |
for (int i = lo + 1; i <= hi; i++) { |
2470 |
– |
double t = a[i]; |
2471 |
– |
int j = i - 1; |
2472 |
– |
while (j >= lo && t < a[j]) { |
2473 |
– |
a[j+1] = a[j]; |
2474 |
– |
--j; |
2475 |
– |
} |
2476 |
– |
a[j+1] = t; |
2477 |
– |
} |
2478 |
– |
return; |
2479 |
– |
} |
2480 |
– |
|
2481 |
– |
int mid = (lo + hi) >>> 1; |
2482 |
– |
if (a[lo] > a[mid]) { |
2483 |
– |
double t = a[lo]; a[lo] = a[mid]; a[mid] = t; |
2484 |
– |
} |
2485 |
– |
if (a[mid] > a[hi]) { |
2486 |
– |
double t = a[mid]; a[mid] = a[hi]; a[hi] = t; |
2487 |
– |
if (a[lo] > a[mid]) { |
2488 |
– |
double u = a[lo]; a[lo] = a[mid]; a[mid] = u; |
2489 |
– |
} |
2490 |
– |
} |
2491 |
– |
|
2492 |
– |
double pivot = a[mid]; |
2493 |
– |
int left = lo+1; |
2494 |
– |
int right = hi-1; |
2495 |
– |
for (;;) { |
2496 |
– |
while (pivot < a[right]) |
2497 |
– |
--right; |
2498 |
– |
while (left < right && pivot >= a[left]) |
2499 |
– |
++left; |
2500 |
– |
if (left < right) { |
2501 |
– |
double t = a[left]; a[left] = a[right]; a[right] = t; |
2502 |
– |
--right; |
2503 |
– |
} |
2504 |
– |
else break; |
2505 |
– |
} |
2506 |
– |
|
2507 |
– |
if (left - lo <= hi - right) { |
2508 |
– |
dcquickSort(a, lo, left); |
2509 |
– |
lo = left + 1; |
2510 |
– |
} |
2511 |
– |
else { |
2512 |
– |
dcquickSort(a, right, hi); |
2513 |
– |
hi = left; |
2514 |
– |
} |
2515 |
– |
} |
2516 |
– |
} |
2517 |
– |
|
2518 |
– |
static void lquickSort(long[] a, LongComparator cmp, int lo, int hi) { |
2519 |
– |
for (;;) { |
2520 |
– |
if (hi - lo <= INSERTION_SORT_THRESHOLD) { |
2521 |
– |
for (int i = lo + 1; i <= hi; i++) { |
2522 |
– |
long t = a[i]; |
2523 |
– |
int j = i - 1; |
2524 |
– |
while (j >= lo && cmp.compare(t, a[j]) < 0) { |
2525 |
– |
a[j+1] = a[j]; |
2526 |
– |
--j; |
2527 |
– |
} |
2528 |
– |
a[j+1] = t; |
2529 |
– |
} |
2530 |
– |
return; |
2531 |
– |
} |
2532 |
– |
|
2533 |
– |
int mid = (lo + hi) >>> 1; |
2534 |
– |
if (cmp.compare(a[lo], a[mid]) > 0) { |
2535 |
– |
long t = a[lo]; a[lo] = a[mid]; a[mid] = t; |
2536 |
– |
} |
2537 |
– |
if (cmp.compare(a[mid], a[hi]) > 0) { |
2538 |
– |
long t = a[mid]; a[mid] = a[hi]; a[hi] = t; |
2539 |
– |
if (cmp.compare(a[lo], a[mid]) > 0) { |
2540 |
– |
long u = a[lo]; a[lo] = a[mid]; a[mid] = u; |
2541 |
– |
} |
2542 |
– |
} |
2543 |
– |
|
2544 |
– |
long pivot = a[mid]; |
2545 |
– |
int left = lo+1; |
2546 |
– |
int right = hi-1; |
2547 |
– |
for (;;) { |
2548 |
– |
while (cmp.compare(pivot, a[right]) < 0) |
2549 |
– |
--right; |
2550 |
– |
while (left < right && cmp.compare(pivot, a[left]) >= 0) |
2551 |
– |
++left; |
2552 |
– |
if (left < right) { |
2553 |
– |
long t = a[left]; a[left] = a[right]; a[right] = t; |
2554 |
– |
--right; |
2555 |
– |
} |
2556 |
– |
else break; |
2557 |
– |
} |
2558 |
– |
|
2559 |
– |
if (left - lo <= hi - right) { |
2560 |
– |
lquickSort(a, cmp, lo, left); |
2561 |
– |
lo = left + 1; |
2562 |
– |
} |
2563 |
– |
else { |
2564 |
– |
lquickSort(a, cmp, right, hi); |
2565 |
– |
hi = left; |
2566 |
– |
} |
2567 |
– |
} |
2568 |
– |
} |
2569 |
– |
|
2570 |
– |
static void lcquickSort(long[] a, int lo, int hi) { |
2571 |
– |
for (;;) { |
2572 |
– |
if (hi - lo <= INSERTION_SORT_THRESHOLD) { |
2573 |
– |
for (int i = lo + 1; i <= hi; i++) { |
2574 |
– |
long t = a[i]; |
2575 |
– |
int j = i - 1; |
2576 |
– |
while (j >= lo && t < a[j]) { |
2577 |
– |
a[j+1] = a[j]; |
2578 |
– |
--j; |
2579 |
– |
} |
2580 |
– |
a[j+1] = t; |
2581 |
– |
} |
2582 |
– |
return; |
2583 |
– |
} |
2584 |
– |
|
2585 |
– |
int mid = (lo + hi) >>> 1; |
2586 |
– |
if (a[lo] > a[mid]) { |
2587 |
– |
long t = a[lo]; a[lo] = a[mid]; a[mid] = t; |
2588 |
– |
} |
2589 |
– |
if (a[mid] > a[hi]) { |
2590 |
– |
long t = a[mid]; a[mid] = a[hi]; a[hi] = t; |
2591 |
– |
if (a[lo] > a[mid]) { |
2592 |
– |
long u = a[lo]; a[lo] = a[mid]; a[mid] = u; |
2593 |
– |
} |
2594 |
– |
} |
2595 |
– |
|
2596 |
– |
long pivot = a[mid]; |
2597 |
– |
int left = lo+1; |
2598 |
– |
int right = hi-1; |
2599 |
– |
for (;;) { |
2600 |
– |
while (pivot < a[right]) |
2601 |
– |
--right; |
2602 |
– |
while (left < right && pivot >= a[left]) |
2603 |
– |
++left; |
2604 |
– |
if (left < right) { |
2605 |
– |
long t = a[left]; a[left] = a[right]; a[right] = t; |
2606 |
– |
--right; |
2607 |
– |
} |
2608 |
– |
else break; |
2609 |
– |
} |
2610 |
– |
|
2611 |
– |
if (left - lo <= hi - right) { |
2612 |
– |
lcquickSort(a, lo, left); |
2613 |
– |
lo = left + 1; |
2614 |
– |
} |
2615 |
– |
else { |
2616 |
– |
lcquickSort(a, right, hi); |
2617 |
– |
hi = left; |
2618 |
– |
} |
2619 |
– |
} |
2620 |
– |
} |
2304 |
|
|
2305 |
|
/** |
2306 |
|
* Cumulative scan |