author | mchung |
Fri, 22 May 2015 16:43:39 -0700 | |
changeset 30789 | 9eca83469588 |
parent 25859 | 3317bb8137f4 |
child 31079 | bf5fabb914b6 |
permissions | -rw-r--r-- |
4170
a94a6faf44e6
6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents:
diff
changeset
|
1 |
/* |
23010
6dadb192ad81
8029235: Update copyright year to match last edit in jdk8 jdk repository for 2013
lana
parents:
17712
diff
changeset
|
2 |
* Copyright (c) 2009, 2013, Oracle and/or its affiliates. All rights reserved. |
4170
a94a6faf44e6
6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents:
diff
changeset
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
a94a6faf44e6
6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents:
diff
changeset
|
4 |
* |
a94a6faf44e6
6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents:
diff
changeset
|
5 |
* This code is free software; you can redistribute it and/or modify it |
a94a6faf44e6
6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents:
diff
changeset
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
4170
a94a6faf44e6
6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents:
diff
changeset
|
8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
4170
a94a6faf44e6
6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents:
diff
changeset
|
10 |
* |
a94a6faf44e6
6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents:
diff
changeset
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
a94a6faf44e6
6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents:
diff
changeset
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
a94a6faf44e6
6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents:
diff
changeset
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
a94a6faf44e6
6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents:
diff
changeset
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
a94a6faf44e6
6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents:
diff
changeset
|
15 |
* accompanied this code). |
a94a6faf44e6
6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents:
diff
changeset
|
16 |
* |
a94a6faf44e6
6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents:
diff
changeset
|
17 |
* You should have received a copy of the GNU General Public License version |
a94a6faf44e6
6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents:
diff
changeset
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
a94a6faf44e6
6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents:
diff
changeset
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
a94a6faf44e6
6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents:
diff
changeset
|
20 |
* |
5506 | 21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
4170
a94a6faf44e6
6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents:
diff
changeset
|
24 |
*/ |
a94a6faf44e6
6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents:
diff
changeset
|
25 |
|
a94a6faf44e6
6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents:
diff
changeset
|
26 |
package java.util; |
a94a6faf44e6
6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents:
diff
changeset
|
27 |
|
a94a6faf44e6
6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents:
diff
changeset
|
28 |
/** |
a94a6faf44e6
6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents:
diff
changeset
|
29 |
* This class implements the Dual-Pivot Quicksort algorithm by |
5995
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
30 |
* Vladimir Yaroslavskiy, Jon Bentley, and Josh Bloch. The algorithm |
4170
a94a6faf44e6
6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents:
diff
changeset
|
31 |
* offers O(n log(n)) performance on many data sets that cause other |
a94a6faf44e6
6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents:
diff
changeset
|
32 |
* quicksorts to degrade to quadratic performance, and is typically |
a94a6faf44e6
6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents:
diff
changeset
|
33 |
* faster than traditional (one-pivot) Quicksort implementations. |
a94a6faf44e6
6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents:
diff
changeset
|
34 |
* |
17712
b56c69500af6
8014076: Arrays parallel and serial sorting improvements
dl
parents:
9035
diff
changeset
|
35 |
* All exposed methods are package-private, designed to be invoked |
b56c69500af6
8014076: Arrays parallel and serial sorting improvements
dl
parents:
9035
diff
changeset
|
36 |
* from public methods (in class Arrays) after performing any |
b56c69500af6
8014076: Arrays parallel and serial sorting improvements
dl
parents:
9035
diff
changeset
|
37 |
* necessary array bounds checks and expanding parameters into the |
b56c69500af6
8014076: Arrays parallel and serial sorting improvements
dl
parents:
9035
diff
changeset
|
38 |
* required forms. |
b56c69500af6
8014076: Arrays parallel and serial sorting improvements
dl
parents:
9035
diff
changeset
|
39 |
* |
4170
a94a6faf44e6
6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents:
diff
changeset
|
40 |
* @author Vladimir Yaroslavskiy |
a94a6faf44e6
6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents:
diff
changeset
|
41 |
* @author Jon Bentley |
a94a6faf44e6
6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents:
diff
changeset
|
42 |
* @author Josh Bloch |
a94a6faf44e6
6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents:
diff
changeset
|
43 |
* |
8193
626b859aabb2
7018258: Dual-pivot updates in 7013585 can fail with ArrayIndexOutOfBoundsException
alanb
parents:
8188
diff
changeset
|
44 |
* @version 2011.02.11 m765.827.12i:5\7pm |
5995
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
45 |
* @since 1.7 |
4170
a94a6faf44e6
6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents:
diff
changeset
|
46 |
*/ |
a94a6faf44e6
6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents:
diff
changeset
|
47 |
final class DualPivotQuicksort { |
a94a6faf44e6
6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents:
diff
changeset
|
48 |
|
4241 | 49 |
/** |
4356 | 50 |
* Prevents instantiation. |
4241 | 51 |
*/ |
4170
a94a6faf44e6
6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents:
diff
changeset
|
52 |
private DualPivotQuicksort() {} |
a94a6faf44e6
6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents:
diff
changeset
|
53 |
|
a94a6faf44e6
6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents:
diff
changeset
|
54 |
/* |
4241 | 55 |
* Tuning parameters. |
4170
a94a6faf44e6
6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents:
diff
changeset
|
56 |
*/ |
a94a6faf44e6
6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents:
diff
changeset
|
57 |
|
a94a6faf44e6
6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents:
diff
changeset
|
58 |
/** |
8188
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
59 |
* The maximum number of runs in merge sort. |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
60 |
*/ |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
61 |
private static final int MAX_RUN_COUNT = 67; |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
62 |
|
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
63 |
/** |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
64 |
* The maximum length of run in merge sort. |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
65 |
*/ |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
66 |
private static final int MAX_RUN_LENGTH = 33; |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
67 |
|
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
68 |
/** |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
69 |
* If the length of an array to be sorted is less than this |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
70 |
* constant, Quicksort is used in preference to merge sort. |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
71 |
*/ |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
72 |
private static final int QUICKSORT_THRESHOLD = 286; |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
73 |
|
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
74 |
/** |
4170
a94a6faf44e6
6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents:
diff
changeset
|
75 |
* If the length of an array to be sorted is less than this |
a94a6faf44e6
6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents:
diff
changeset
|
76 |
* constant, insertion sort is used in preference to Quicksort. |
a94a6faf44e6
6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents:
diff
changeset
|
77 |
*/ |
6896
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
78 |
private static final int INSERTION_SORT_THRESHOLD = 47; |
4170
a94a6faf44e6
6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents:
diff
changeset
|
79 |
|
a94a6faf44e6
6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents:
diff
changeset
|
80 |
/** |
6896
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
81 |
* If the length of a byte array to be sorted is greater than this |
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
82 |
* constant, counting sort is used in preference to insertion sort. |
4170
a94a6faf44e6
6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents:
diff
changeset
|
83 |
*/ |
6896
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
84 |
private static final int COUNTING_SORT_THRESHOLD_FOR_BYTE = 29; |
4170
a94a6faf44e6
6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents:
diff
changeset
|
85 |
|
a94a6faf44e6
6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents:
diff
changeset
|
86 |
/** |
a94a6faf44e6
6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents:
diff
changeset
|
87 |
* If the length of a short or char array to be sorted is greater |
a94a6faf44e6
6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents:
diff
changeset
|
88 |
* than this constant, counting sort is used in preference to Quicksort. |
a94a6faf44e6
6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents:
diff
changeset
|
89 |
*/ |
6896
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
90 |
private static final int COUNTING_SORT_THRESHOLD_FOR_SHORT_OR_CHAR = 3200; |
4170
a94a6faf44e6
6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents:
diff
changeset
|
91 |
|
a94a6faf44e6
6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents:
diff
changeset
|
92 |
/* |
5995
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
93 |
* Sorting methods for seven primitive types. |
4170
a94a6faf44e6
6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents:
diff
changeset
|
94 |
*/ |
a94a6faf44e6
6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents:
diff
changeset
|
95 |
|
a94a6faf44e6
6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents:
diff
changeset
|
96 |
/** |
17712
b56c69500af6
8014076: Arrays parallel and serial sorting improvements
dl
parents:
9035
diff
changeset
|
97 |
* Sorts the specified range of the array using the given |
b56c69500af6
8014076: Arrays parallel and serial sorting improvements
dl
parents:
9035
diff
changeset
|
98 |
* workspace array slice if possible for merging |
4233 | 99 |
* |
100 |
* @param a the array to be sorted |
|
101 |
* @param left the index of the first element, inclusive, to be sorted |
|
102 |
* @param right the index of the last element, inclusive, to be sorted |
|
17712
b56c69500af6
8014076: Arrays parallel and serial sorting improvements
dl
parents:
9035
diff
changeset
|
103 |
* @param work a workspace array (slice) |
b56c69500af6
8014076: Arrays parallel and serial sorting improvements
dl
parents:
9035
diff
changeset
|
104 |
* @param workBase origin of usable space in work array |
b56c69500af6
8014076: Arrays parallel and serial sorting improvements
dl
parents:
9035
diff
changeset
|
105 |
* @param workLen usable size of work array |
6896
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
106 |
*/ |
17712
b56c69500af6
8014076: Arrays parallel and serial sorting improvements
dl
parents:
9035
diff
changeset
|
107 |
static void sort(int[] a, int left, int right, |
b56c69500af6
8014076: Arrays parallel and serial sorting improvements
dl
parents:
9035
diff
changeset
|
108 |
int[] work, int workBase, int workLen) { |
8188
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
109 |
// Use Quicksort on small arrays |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
110 |
if (right - left < QUICKSORT_THRESHOLD) { |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
111 |
sort(a, left, right, true); |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
112 |
return; |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
113 |
} |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
114 |
|
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
115 |
/* |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
116 |
* Index run[i] is the start of i-th run |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
117 |
* (ascending or descending sequence). |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
118 |
*/ |
8193
626b859aabb2
7018258: Dual-pivot updates in 7013585 can fail with ArrayIndexOutOfBoundsException
alanb
parents:
8188
diff
changeset
|
119 |
int[] run = new int[MAX_RUN_COUNT + 1]; |
8188
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
120 |
int count = 0; run[0] = left; |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
121 |
|
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
122 |
// Check if the array is nearly sorted |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
123 |
for (int k = left; k < right; run[count] = k) { |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
124 |
if (a[k] < a[k + 1]) { // ascending |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
125 |
while (++k <= right && a[k - 1] <= a[k]); |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
126 |
} else if (a[k] > a[k + 1]) { // descending |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
127 |
while (++k <= right && a[k - 1] >= a[k]); |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
128 |
for (int lo = run[count] - 1, hi = k; ++lo < --hi; ) { |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
129 |
int t = a[lo]; a[lo] = a[hi]; a[hi] = t; |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
130 |
} |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
131 |
} else { // equal |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
132 |
for (int m = MAX_RUN_LENGTH; ++k <= right && a[k - 1] == a[k]; ) { |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
133 |
if (--m == 0) { |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
134 |
sort(a, left, right, true); |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
135 |
return; |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
136 |
} |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
137 |
} |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
138 |
} |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
139 |
|
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
140 |
/* |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
141 |
* The array is not highly structured, |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
142 |
* use Quicksort instead of merge sort. |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
143 |
*/ |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
144 |
if (++count == MAX_RUN_COUNT) { |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
145 |
sort(a, left, right, true); |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
146 |
return; |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
147 |
} |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
148 |
} |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
149 |
|
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
150 |
// Check special cases |
17712
b56c69500af6
8014076: Arrays parallel and serial sorting improvements
dl
parents:
9035
diff
changeset
|
151 |
// Implementation note: variable "right" is increased by 1. |
8188
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
152 |
if (run[count] == right++) { // The last run contains one element |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
153 |
run[++count] = right; |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
154 |
} else if (count == 1) { // The array is already sorted |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
155 |
return; |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
156 |
} |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
157 |
|
17712
b56c69500af6
8014076: Arrays parallel and serial sorting improvements
dl
parents:
9035
diff
changeset
|
158 |
// Determine alternation base for merge |
b56c69500af6
8014076: Arrays parallel and serial sorting improvements
dl
parents:
9035
diff
changeset
|
159 |
byte odd = 0; |
8188
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
160 |
for (int n = 1; (n <<= 1) < count; odd ^= 1); |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
161 |
|
17712
b56c69500af6
8014076: Arrays parallel and serial sorting improvements
dl
parents:
9035
diff
changeset
|
162 |
// Use or create temporary array b for merging |
b56c69500af6
8014076: Arrays parallel and serial sorting improvements
dl
parents:
9035
diff
changeset
|
163 |
int[] b; // temp array; alternates with a |
b56c69500af6
8014076: Arrays parallel and serial sorting improvements
dl
parents:
9035
diff
changeset
|
164 |
int ao, bo; // array offsets from 'left' |
b56c69500af6
8014076: Arrays parallel and serial sorting improvements
dl
parents:
9035
diff
changeset
|
165 |
int blen = right - left; // space needed for b |
b56c69500af6
8014076: Arrays parallel and serial sorting improvements
dl
parents:
9035
diff
changeset
|
166 |
if (work == null || workLen < blen || workBase + blen > work.length) { |
b56c69500af6
8014076: Arrays parallel and serial sorting improvements
dl
parents:
9035
diff
changeset
|
167 |
work = new int[blen]; |
b56c69500af6
8014076: Arrays parallel and serial sorting improvements
dl
parents:
9035
diff
changeset
|
168 |
workBase = 0; |
b56c69500af6
8014076: Arrays parallel and serial sorting improvements
dl
parents:
9035
diff
changeset
|
169 |
} |
8188
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
170 |
if (odd == 0) { |
17712
b56c69500af6
8014076: Arrays parallel and serial sorting improvements
dl
parents:
9035
diff
changeset
|
171 |
System.arraycopy(a, left, work, workBase, blen); |
b56c69500af6
8014076: Arrays parallel and serial sorting improvements
dl
parents:
9035
diff
changeset
|
172 |
b = a; |
b56c69500af6
8014076: Arrays parallel and serial sorting improvements
dl
parents:
9035
diff
changeset
|
173 |
bo = 0; |
b56c69500af6
8014076: Arrays parallel and serial sorting improvements
dl
parents:
9035
diff
changeset
|
174 |
a = work; |
b56c69500af6
8014076: Arrays parallel and serial sorting improvements
dl
parents:
9035
diff
changeset
|
175 |
ao = workBase - left; |
8188
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
176 |
} else { |
17712
b56c69500af6
8014076: Arrays parallel and serial sorting improvements
dl
parents:
9035
diff
changeset
|
177 |
b = work; |
b56c69500af6
8014076: Arrays parallel and serial sorting improvements
dl
parents:
9035
diff
changeset
|
178 |
ao = 0; |
b56c69500af6
8014076: Arrays parallel and serial sorting improvements
dl
parents:
9035
diff
changeset
|
179 |
bo = workBase - left; |
8188
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
180 |
} |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
181 |
|
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
182 |
// Merging |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
183 |
for (int last; count > 1; count = last) { |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
184 |
for (int k = (last = 0) + 2; k <= count; k += 2) { |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
185 |
int hi = run[k], mi = run[k - 1]; |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
186 |
for (int i = run[k - 2], p = i, q = mi; i < hi; ++i) { |
17712
b56c69500af6
8014076: Arrays parallel and serial sorting improvements
dl
parents:
9035
diff
changeset
|
187 |
if (q >= hi || p < mi && a[p + ao] <= a[q + ao]) { |
b56c69500af6
8014076: Arrays parallel and serial sorting improvements
dl
parents:
9035
diff
changeset
|
188 |
b[i + bo] = a[p++ + ao]; |
8188
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
189 |
} else { |
17712
b56c69500af6
8014076: Arrays parallel and serial sorting improvements
dl
parents:
9035
diff
changeset
|
190 |
b[i + bo] = a[q++ + ao]; |
8188
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
191 |
} |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
192 |
} |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
193 |
run[++last] = hi; |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
194 |
} |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
195 |
if ((count & 1) != 0) { |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
196 |
for (int i = right, lo = run[count - 1]; --i >= lo; |
17712
b56c69500af6
8014076: Arrays parallel and serial sorting improvements
dl
parents:
9035
diff
changeset
|
197 |
b[i + bo] = a[i + ao] |
8188
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
198 |
); |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
199 |
run[++last] = right; |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
200 |
} |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
201 |
int[] t = a; a = b; b = t; |
17712
b56c69500af6
8014076: Arrays parallel and serial sorting improvements
dl
parents:
9035
diff
changeset
|
202 |
int o = ao; ao = bo; bo = o; |
8188
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
203 |
} |
6896
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
204 |
} |
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
205 |
|
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
206 |
/** |
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
207 |
* Sorts the specified range of the array by Dual-Pivot Quicksort. |
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
208 |
* |
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
209 |
* @param a the array to be sorted |
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
210 |
* @param left the index of the first element, inclusive, to be sorted |
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
211 |
* @param right the index of the last element, inclusive, to be sorted |
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
212 |
* @param leftmost indicates if this part is the leftmost in the range |
4233 | 213 |
*/ |
5995
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
214 |
private static void sort(int[] a, int left, int right, boolean leftmost) { |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
215 |
int length = right - left + 1; |
4170
a94a6faf44e6
6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents:
diff
changeset
|
216 |
|
8188
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
217 |
// Use insertion sort on tiny arrays |
5995
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
218 |
if (length < INSERTION_SORT_THRESHOLD) { |
6896
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
219 |
if (leftmost) { |
5995
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
220 |
/* |
6896
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
221 |
* Traditional (without sentinel) insertion sort, |
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
222 |
* optimized for server VM, is used in case of |
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
223 |
* the leftmost part. |
5995
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
224 |
*/ |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
225 |
for (int i = left, j = i; i < right; j = ++i) { |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
226 |
int ai = a[i + 1]; |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
227 |
while (ai < a[j]) { |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
228 |
a[j + 1] = a[j]; |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
229 |
if (j-- == left) { |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
230 |
break; |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
231 |
} |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
232 |
} |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
233 |
a[j + 1] = ai; |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
234 |
} |
6896
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
235 |
} else { |
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
236 |
/* |
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
237 |
* Skip the longest ascending sequence. |
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
238 |
*/ |
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
239 |
do { |
8188
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
240 |
if (left >= right) { |
6896
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
241 |
return; |
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
242 |
} |
8188
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
243 |
} while (a[++left] >= a[left - 1]); |
6896
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
244 |
|
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
245 |
/* |
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
246 |
* Every element from adjoining part plays the role |
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
247 |
* of sentinel, therefore this allows us to avoid the |
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
248 |
* left range check on each iteration. Moreover, we use |
8188
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
249 |
* the more optimized algorithm, so called pair insertion |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
250 |
* sort, which is faster (in the context of Quicksort) |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
251 |
* than traditional implementation of insertion sort. |
6896
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
252 |
*/ |
8188
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
253 |
for (int k = left; ++left <= right; k = ++left) { |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
254 |
int a1 = a[k], a2 = a[left]; |
6896
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
255 |
|
8188
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
256 |
if (a1 < a2) { |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
257 |
a2 = a1; a1 = a[left]; |
6896
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
258 |
} |
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
259 |
while (a1 < a[--k]) { |
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
260 |
a[k + 2] = a[k]; |
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
261 |
} |
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
262 |
a[++k + 1] = a1; |
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
263 |
|
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
264 |
while (a2 < a[--k]) { |
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
265 |
a[k + 1] = a[k]; |
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
266 |
} |
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
267 |
a[k + 1] = a2; |
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
268 |
} |
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
269 |
int last = a[right]; |
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
270 |
|
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
271 |
while (last < a[--right]) { |
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
272 |
a[right + 1] = a[right]; |
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
273 |
} |
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
274 |
a[right + 1] = last; |
5995
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
275 |
} |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
276 |
return; |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
277 |
} |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
278 |
|
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
279 |
// Inexpensive approximation of length / 7 |
6896
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
280 |
int seventh = (length >> 3) + (length >> 6) + 1; |
5995
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
281 |
|
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
282 |
/* |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
283 |
* Sort five evenly spaced elements around (and including) the |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
284 |
* center element in the range. These elements will be used for |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
285 |
* pivot selection as described below. The choice for spacing |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
286 |
* these elements was empirically determined to work well on |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
287 |
* a wide variety of inputs. |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
288 |
*/ |
4170
a94a6faf44e6
6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents:
diff
changeset
|
289 |
int e3 = (left + right) >>> 1; // The midpoint |
5995
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
290 |
int e2 = e3 - seventh; |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
291 |
int e1 = e2 - seventh; |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
292 |
int e4 = e3 + seventh; |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
293 |
int e5 = e4 + seventh; |
4170
a94a6faf44e6
6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents:
diff
changeset
|
294 |
|
5995
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
295 |
// Sort these elements using insertion sort |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
296 |
if (a[e2] < a[e1]) { int t = a[e2]; a[e2] = a[e1]; a[e1] = t; } |
4241 | 297 |
|
5995
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
298 |
if (a[e3] < a[e2]) { int t = a[e3]; a[e3] = a[e2]; a[e2] = t; |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
299 |
if (t < a[e1]) { a[e2] = a[e1]; a[e1] = t; } |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
300 |
} |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
301 |
if (a[e4] < a[e3]) { int t = a[e4]; a[e4] = a[e3]; a[e3] = t; |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
302 |
if (t < a[e2]) { a[e3] = a[e2]; a[e2] = t; |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
303 |
if (t < a[e1]) { a[e2] = a[e1]; a[e1] = t; } |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
304 |
} |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
305 |
} |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
306 |
if (a[e5] < a[e4]) { int t = a[e5]; a[e5] = a[e4]; a[e4] = t; |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
307 |
if (t < a[e3]) { a[e4] = a[e3]; a[e3] = t; |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
308 |
if (t < a[e2]) { a[e3] = a[e2]; a[e2] = t; |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
309 |
if (t < a[e1]) { a[e2] = a[e1]; a[e1] = t; } |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
310 |
} |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
311 |
} |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
312 |
} |
4170
a94a6faf44e6
6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents:
diff
changeset
|
313 |
|
a94a6faf44e6
6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents:
diff
changeset
|
314 |
// Pointers |
5995
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
315 |
int less = left; // The index of the first element of center part |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
316 |
int great = right; // The index before the first element of right part |
4170
a94a6faf44e6
6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents:
diff
changeset
|
317 |
|
8188
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
318 |
if (a[e1] != a[e2] && a[e2] != a[e3] && a[e3] != a[e4] && a[e4] != a[e5]) { |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
319 |
/* |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
320 |
* Use the second and fourth of the five sorted elements as pivots. |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
321 |
* These values are inexpensive approximations of the first and |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
322 |
* second terciles of the array. Note that pivot1 <= pivot2. |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
323 |
*/ |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
324 |
int pivot1 = a[e2]; |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
325 |
int pivot2 = a[e4]; |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
326 |
|
5995
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
327 |
/* |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
328 |
* The first and the last elements to be sorted are moved to the |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
329 |
* locations formerly occupied by the pivots. When partitioning |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
330 |
* is complete, the pivots are swapped back into their final |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
331 |
* positions, and excluded from subsequent sorting. |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
332 |
*/ |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
333 |
a[e2] = a[left]; |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
334 |
a[e4] = a[right]; |
4170
a94a6faf44e6
6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents:
diff
changeset
|
335 |
|
5995
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
336 |
/* |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
337 |
* Skip elements, which are less or greater than pivot values. |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
338 |
*/ |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
339 |
while (a[++less] < pivot1); |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
340 |
while (a[--great] > pivot2); |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
341 |
|
4170
a94a6faf44e6
6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents:
diff
changeset
|
342 |
/* |
4356 | 343 |
* Partitioning: |
344 |
* |
|
5995
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
345 |
* left part center part right part |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
346 |
* +--------------------------------------------------------------+ |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
347 |
* | < pivot1 | pivot1 <= && <= pivot2 | ? | > pivot2 | |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
348 |
* +--------------------------------------------------------------+ |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
349 |
* ^ ^ ^ |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
350 |
* | | | |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
351 |
* less k great |
4356 | 352 |
* |
4170
a94a6faf44e6
6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents:
diff
changeset
|
353 |
* Invariants: |
4356 | 354 |
* |
4170
a94a6faf44e6
6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents:
diff
changeset
|
355 |
* all in (left, less) < pivot1 |
a94a6faf44e6
6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents:
diff
changeset
|
356 |
* pivot1 <= all in [less, k) <= pivot2 |
a94a6faf44e6
6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents:
diff
changeset
|
357 |
* all in (great, right) > pivot2 |
a94a6faf44e6
6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents:
diff
changeset
|
358 |
* |
5995
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
359 |
* Pointer k is the first index of ?-part. |
4170
a94a6faf44e6
6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents:
diff
changeset
|
360 |
*/ |
4241 | 361 |
outer: |
6896
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
362 |
for (int k = less - 1; ++k <= great; ) { |
4170
a94a6faf44e6
6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents:
diff
changeset
|
363 |
int ak = a[k]; |
4356 | 364 |
if (ak < pivot1) { // Move a[k] to left part |
5995
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
365 |
a[k] = a[less]; |
6896
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
366 |
/* |
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
367 |
* Here and below we use "a[i] = b; i++;" instead |
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
368 |
* of "a[i++] = b;" due to performance issue. |
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
369 |
*/ |
5995
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
370 |
a[less] = ak; |
8188
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
371 |
++less; |
4356 | 372 |
} else if (ak > pivot2) { // Move a[k] to right part |
4241 | 373 |
while (a[great] > pivot2) { |
4356 | 374 |
if (great-- == k) { |
4241 | 375 |
break outer; |
376 |
} |
|
4170
a94a6faf44e6
6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents:
diff
changeset
|
377 |
} |
6896
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
378 |
if (a[great] < pivot1) { // a[great] <= pivot2 |
4170
a94a6faf44e6
6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents:
diff
changeset
|
379 |
a[k] = a[less]; |
5995
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
380 |
a[less] = a[great]; |
8188
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
381 |
++less; |
4356 | 382 |
} else { // pivot1 <= a[great] <= pivot2 |
383 |
a[k] = a[great]; |
|
5995
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
384 |
} |
6896
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
385 |
/* |
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
386 |
* Here and below we use "a[i] = b; i--;" instead |
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
387 |
* of "a[i--] = b;" due to performance issue. |
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
388 |
*/ |
5995
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
389 |
a[great] = ak; |
8188
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
390 |
--great; |
5995
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
391 |
} |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
392 |
} |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
393 |
|
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
394 |
// Swap pivots into their final positions |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
395 |
a[left] = a[less - 1]; a[less - 1] = pivot1; |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
396 |
a[right] = a[great + 1]; a[great + 1] = pivot2; |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
397 |
|
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
398 |
// Sort left and right parts recursively, excluding known pivots |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
399 |
sort(a, left, less - 2, leftmost); |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
400 |
sort(a, great + 2, right, false); |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
401 |
|
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
402 |
/* |
6896
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
403 |
* If center part is too large (comprises > 4/7 of the array), |
5995
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
404 |
* swap internal pivot values to ends. |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
405 |
*/ |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
406 |
if (less < e1 && e5 < great) { |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
407 |
/* |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
408 |
* Skip elements, which are equal to pivot values. |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
409 |
*/ |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
410 |
while (a[less] == pivot1) { |
8188
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
411 |
++less; |
5995
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
412 |
} |
8188
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
413 |
|
5995
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
414 |
while (a[great] == pivot2) { |
8188
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
415 |
--great; |
5995
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
416 |
} |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
417 |
|
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
418 |
/* |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
419 |
* Partitioning: |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
420 |
* |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
421 |
* left part center part right part |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
422 |
* +----------------------------------------------------------+ |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
423 |
* | == pivot1 | pivot1 < && < pivot2 | ? | == pivot2 | |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
424 |
* +----------------------------------------------------------+ |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
425 |
* ^ ^ ^ |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
426 |
* | | | |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
427 |
* less k great |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
428 |
* |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
429 |
* Invariants: |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
430 |
* |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
431 |
* all in (*, less) == pivot1 |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
432 |
* pivot1 < all in [less, k) < pivot2 |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
433 |
* all in (great, *) == pivot2 |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
434 |
* |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
435 |
* Pointer k is the first index of ?-part. |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
436 |
*/ |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
437 |
outer: |
6896
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
438 |
for (int k = less - 1; ++k <= great; ) { |
5995
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
439 |
int ak = a[k]; |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
440 |
if (ak == pivot1) { // Move a[k] to left part |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
441 |
a[k] = a[less]; |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
442 |
a[less] = ak; |
8188
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
443 |
++less; |
5995
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
444 |
} else if (ak == pivot2) { // Move a[k] to right part |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
445 |
while (a[great] == pivot2) { |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
446 |
if (great-- == k) { |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
447 |
break outer; |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
448 |
} |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
449 |
} |
6896
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
450 |
if (a[great] == pivot1) { // a[great] < pivot2 |
5995
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
451 |
a[k] = a[less]; |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
452 |
/* |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
453 |
* Even though a[great] equals to pivot1, the |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
454 |
* assignment a[less] = pivot1 may be incorrect, |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
455 |
* if a[great] and pivot1 are floating-point zeros |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
456 |
* of different signs. Therefore in float and |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
457 |
* double sorting methods we have to use more |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
458 |
* accurate assignment a[less] = a[great]. |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
459 |
*/ |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
460 |
a[less] = pivot1; |
8188
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
461 |
++less; |
5995
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
462 |
} else { // pivot1 < a[great] < pivot2 |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
463 |
a[k] = a[great]; |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
464 |
} |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
465 |
a[great] = ak; |
8188
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
466 |
--great; |
4170
a94a6faf44e6
6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents:
diff
changeset
|
467 |
} |
a94a6faf44e6
6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents:
diff
changeset
|
468 |
} |
a94a6faf44e6
6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents:
diff
changeset
|
469 |
} |
5995
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
470 |
|
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
471 |
// Sort center part recursively |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
472 |
sort(a, less, great, false); |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
473 |
|
8188
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
474 |
} else { // Partitioning with one pivot |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
475 |
/* |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
476 |
* Use the third of the five sorted elements as pivot. |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
477 |
* This value is inexpensive approximation of the median. |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
478 |
*/ |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
479 |
int pivot = a[e3]; |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
480 |
|
4170
a94a6faf44e6
6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents:
diff
changeset
|
481 |
/* |
6896
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
482 |
* Partitioning degenerates to the traditional 3-way |
5995
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
483 |
* (or "Dutch National Flag") schema: |
4170
a94a6faf44e6
6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents:
diff
changeset
|
484 |
* |
5995
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
485 |
* left part center part right part |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
486 |
* +-------------------------------------------------+ |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
487 |
* | < pivot | == pivot | ? | > pivot | |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
488 |
* +-------------------------------------------------+ |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
489 |
* ^ ^ ^ |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
490 |
* | | | |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
491 |
* less k great |
4170
a94a6faf44e6
6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents:
diff
changeset
|
492 |
* |
a94a6faf44e6
6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents:
diff
changeset
|
493 |
* Invariants: |
a94a6faf44e6
6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents:
diff
changeset
|
494 |
* |
a94a6faf44e6
6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents:
diff
changeset
|
495 |
* all in (left, less) < pivot |
a94a6faf44e6
6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents:
diff
changeset
|
496 |
* all in [less, k) == pivot |
a94a6faf44e6
6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents:
diff
changeset
|
497 |
* all in (great, right) > pivot |
a94a6faf44e6
6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents:
diff
changeset
|
498 |
* |
5995
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
499 |
* Pointer k is the first index of ?-part. |
4170
a94a6faf44e6
6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents:
diff
changeset
|
500 |
*/ |
6896
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
501 |
for (int k = less; k <= great; ++k) { |
8188
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
502 |
if (a[k] == pivot) { |
4241 | 503 |
continue; |
4170
a94a6faf44e6
6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents:
diff
changeset
|
504 |
} |
5995
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
505 |
int ak = a[k]; |
8188
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
506 |
if (ak < pivot) { // Move a[k] to left part |
5995
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
507 |
a[k] = a[less]; |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
508 |
a[less] = ak; |
8188
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
509 |
++less; |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
510 |
} else { // a[k] > pivot - Move a[k] to right part |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
511 |
while (a[great] > pivot) { |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
512 |
--great; |
4170
a94a6faf44e6
6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents:
diff
changeset
|
513 |
} |
8188
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
514 |
if (a[great] < pivot) { // a[great] <= pivot |
4170
a94a6faf44e6
6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents:
diff
changeset
|
515 |
a[k] = a[less]; |
5995
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
516 |
a[less] = a[great]; |
8188
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
517 |
++less; |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
518 |
} else { // a[great] == pivot |
5995
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
519 |
/* |
8188
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
520 |
* Even though a[great] equals to pivot, the |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
521 |
* assignment a[k] = pivot may be incorrect, |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
522 |
* if a[great] and pivot are floating-point |
5995
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
523 |
* zeros of different signs. Therefore in float |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
524 |
* and double sorting methods we have to use |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
525 |
* more accurate assignment a[k] = a[great]. |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
526 |
*/ |
8188
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
527 |
a[k] = pivot; |
4170
a94a6faf44e6
6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents:
diff
changeset
|
528 |
} |
5995
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
529 |
a[great] = ak; |
8188
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
530 |
--great; |
4170
a94a6faf44e6
6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents:
diff
changeset
|
531 |
} |
a94a6faf44e6
6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents:
diff
changeset
|
532 |
} |
4356 | 533 |
|
6896
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
534 |
/* |
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
535 |
* Sort left and right parts recursively. |
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
536 |
* All elements from center part are equal |
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
537 |
* and, therefore, already sorted. |
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
538 |
*/ |
5995
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
539 |
sort(a, left, less - 1, leftmost); |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
540 |
sort(a, great + 1, right, false); |
4170
a94a6faf44e6
6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents:
diff
changeset
|
541 |
} |
4233 | 542 |
} |
543 |
||
544 |
/** |
|
17712
b56c69500af6
8014076: Arrays parallel and serial sorting improvements
dl
parents:
9035
diff
changeset
|
545 |
* Sorts the specified range of the array using the given |
b56c69500af6
8014076: Arrays parallel and serial sorting improvements
dl
parents:
9035
diff
changeset
|
546 |
* workspace array slice if possible for merging |
4233 | 547 |
* |
548 |
* @param a the array to be sorted |
|
549 |
* @param left the index of the first element, inclusive, to be sorted |
|
550 |
* @param right the index of the last element, inclusive, to be sorted |
|
17712
b56c69500af6
8014076: Arrays parallel and serial sorting improvements
dl
parents:
9035
diff
changeset
|
551 |
* @param work a workspace array (slice) |
b56c69500af6
8014076: Arrays parallel and serial sorting improvements
dl
parents:
9035
diff
changeset
|
552 |
* @param workBase origin of usable space in work array |
b56c69500af6
8014076: Arrays parallel and serial sorting improvements
dl
parents:
9035
diff
changeset
|
553 |
* @param workLen usable size of work array |
6896
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
554 |
*/ |
17712
b56c69500af6
8014076: Arrays parallel and serial sorting improvements
dl
parents:
9035
diff
changeset
|
555 |
static void sort(long[] a, int left, int right, |
b56c69500af6
8014076: Arrays parallel and serial sorting improvements
dl
parents:
9035
diff
changeset
|
556 |
long[] work, int workBase, int workLen) { |
8188
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
557 |
// Use Quicksort on small arrays |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
558 |
if (right - left < QUICKSORT_THRESHOLD) { |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
559 |
sort(a, left, right, true); |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
560 |
return; |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
561 |
} |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
562 |
|
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
563 |
/* |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
564 |
* Index run[i] is the start of i-th run |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
565 |
* (ascending or descending sequence). |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
566 |
*/ |
8193
626b859aabb2
7018258: Dual-pivot updates in 7013585 can fail with ArrayIndexOutOfBoundsException
alanb
parents:
8188
diff
changeset
|
567 |
int[] run = new int[MAX_RUN_COUNT + 1]; |
8188
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
568 |
int count = 0; run[0] = left; |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
569 |
|
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
570 |
// Check if the array is nearly sorted |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
571 |
for (int k = left; k < right; run[count] = k) { |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
572 |
if (a[k] < a[k + 1]) { // ascending |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
573 |
while (++k <= right && a[k - 1] <= a[k]); |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
574 |
} else if (a[k] > a[k + 1]) { // descending |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
575 |
while (++k <= right && a[k - 1] >= a[k]); |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
576 |
for (int lo = run[count] - 1, hi = k; ++lo < --hi; ) { |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
577 |
long t = a[lo]; a[lo] = a[hi]; a[hi] = t; |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
578 |
} |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
579 |
} else { // equal |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
580 |
for (int m = MAX_RUN_LENGTH; ++k <= right && a[k - 1] == a[k]; ) { |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
581 |
if (--m == 0) { |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
582 |
sort(a, left, right, true); |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
583 |
return; |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
584 |
} |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
585 |
} |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
586 |
} |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
587 |
|
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
588 |
/* |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
589 |
* The array is not highly structured, |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
590 |
* use Quicksort instead of merge sort. |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
591 |
*/ |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
592 |
if (++count == MAX_RUN_COUNT) { |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
593 |
sort(a, left, right, true); |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
594 |
return; |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
595 |
} |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
596 |
} |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
597 |
|
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
598 |
// Check special cases |
17712
b56c69500af6
8014076: Arrays parallel and serial sorting improvements
dl
parents:
9035
diff
changeset
|
599 |
// Implementation note: variable "right" is increased by 1. |
8188
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
600 |
if (run[count] == right++) { // The last run contains one element |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
601 |
run[++count] = right; |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
602 |
} else if (count == 1) { // The array is already sorted |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
603 |
return; |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
604 |
} |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
605 |
|
17712
b56c69500af6
8014076: Arrays parallel and serial sorting improvements
dl
parents:
9035
diff
changeset
|
606 |
// Determine alternation base for merge |
b56c69500af6
8014076: Arrays parallel and serial sorting improvements
dl
parents:
9035
diff
changeset
|
607 |
byte odd = 0; |
8188
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
608 |
for (int n = 1; (n <<= 1) < count; odd ^= 1); |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
609 |
|
17712
b56c69500af6
8014076: Arrays parallel and serial sorting improvements
dl
parents:
9035
diff
changeset
|
610 |
// Use or create temporary array b for merging |
b56c69500af6
8014076: Arrays parallel and serial sorting improvements
dl
parents:
9035
diff
changeset
|
611 |
long[] b; // temp array; alternates with a |
b56c69500af6
8014076: Arrays parallel and serial sorting improvements
dl
parents:
9035
diff
changeset
|
612 |
int ao, bo; // array offsets from 'left' |
b56c69500af6
8014076: Arrays parallel and serial sorting improvements
dl
parents:
9035
diff
changeset
|
613 |
int blen = right - left; // space needed for b |
b56c69500af6
8014076: Arrays parallel and serial sorting improvements
dl
parents:
9035
diff
changeset
|
614 |
if (work == null || workLen < blen || workBase + blen > work.length) { |
b56c69500af6
8014076: Arrays parallel and serial sorting improvements
dl
parents:
9035
diff
changeset
|
615 |
work = new long[blen]; |
b56c69500af6
8014076: Arrays parallel and serial sorting improvements
dl
parents:
9035
diff
changeset
|
616 |
workBase = 0; |
b56c69500af6
8014076: Arrays parallel and serial sorting improvements
dl
parents:
9035
diff
changeset
|
617 |
} |
8188
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
618 |
if (odd == 0) { |
17712
b56c69500af6
8014076: Arrays parallel and serial sorting improvements
dl
parents:
9035
diff
changeset
|
619 |
System.arraycopy(a, left, work, workBase, blen); |
b56c69500af6
8014076: Arrays parallel and serial sorting improvements
dl
parents:
9035
diff
changeset
|
620 |
b = a; |
b56c69500af6
8014076: Arrays parallel and serial sorting improvements
dl
parents:
9035
diff
changeset
|
621 |
bo = 0; |
b56c69500af6
8014076: Arrays parallel and serial sorting improvements
dl
parents:
9035
diff
changeset
|
622 |
a = work; |
b56c69500af6
8014076: Arrays parallel and serial sorting improvements
dl
parents:
9035
diff
changeset
|
623 |
ao = workBase - left; |
8188
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
624 |
} else { |
17712
b56c69500af6
8014076: Arrays parallel and serial sorting improvements
dl
parents:
9035
diff
changeset
|
625 |
b = work; |
b56c69500af6
8014076: Arrays parallel and serial sorting improvements
dl
parents:
9035
diff
changeset
|
626 |
ao = 0; |
b56c69500af6
8014076: Arrays parallel and serial sorting improvements
dl
parents:
9035
diff
changeset
|
627 |
bo = workBase - left; |
8188
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
628 |
} |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
629 |
|
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
630 |
// Merging |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
631 |
for (int last; count > 1; count = last) { |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
632 |
for (int k = (last = 0) + 2; k <= count; k += 2) { |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
633 |
int hi = run[k], mi = run[k - 1]; |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
634 |
for (int i = run[k - 2], p = i, q = mi; i < hi; ++i) { |
17712
b56c69500af6
8014076: Arrays parallel and serial sorting improvements
dl
parents:
9035
diff
changeset
|
635 |
if (q >= hi || p < mi && a[p + ao] <= a[q + ao]) { |
b56c69500af6
8014076: Arrays parallel and serial sorting improvements
dl
parents:
9035
diff
changeset
|
636 |
b[i + bo] = a[p++ + ao]; |
8188
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
637 |
} else { |
17712
b56c69500af6
8014076: Arrays parallel and serial sorting improvements
dl
parents:
9035
diff
changeset
|
638 |
b[i + bo] = a[q++ + ao]; |
8188
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
639 |
} |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
640 |
} |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
641 |
run[++last] = hi; |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
642 |
} |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
643 |
if ((count & 1) != 0) { |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
644 |
for (int i = right, lo = run[count - 1]; --i >= lo; |
17712
b56c69500af6
8014076: Arrays parallel and serial sorting improvements
dl
parents:
9035
diff
changeset
|
645 |
b[i + bo] = a[i + ao] |
8188
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
646 |
); |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
647 |
run[++last] = right; |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
648 |
} |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
649 |
long[] t = a; a = b; b = t; |
17712
b56c69500af6
8014076: Arrays parallel and serial sorting improvements
dl
parents:
9035
diff
changeset
|
650 |
int o = ao; ao = bo; bo = o; |
8188
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
651 |
} |
6896
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
652 |
} |
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
653 |
|
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
654 |
/** |
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
655 |
* Sorts the specified range of the array by Dual-Pivot Quicksort. |
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
656 |
* |
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
657 |
* @param a the array to be sorted |
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
658 |
* @param left the index of the first element, inclusive, to be sorted |
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
659 |
* @param right the index of the last element, inclusive, to be sorted |
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
660 |
* @param leftmost indicates if this part is the leftmost in the range |
4233 | 661 |
*/ |
5995
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
662 |
private static void sort(long[] a, int left, int right, boolean leftmost) { |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
663 |
int length = right - left + 1; |
4170
a94a6faf44e6
6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents:
diff
changeset
|
664 |
|
8188
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
665 |
// Use insertion sort on tiny arrays |
5995
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
666 |
if (length < INSERTION_SORT_THRESHOLD) { |
6896
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
667 |
if (leftmost) { |
5995
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
668 |
/* |
6896
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
669 |
* Traditional (without sentinel) insertion sort, |
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
670 |
* optimized for server VM, is used in case of |
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
671 |
* the leftmost part. |
5995
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
672 |
*/ |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
673 |
for (int i = left, j = i; i < right; j = ++i) { |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
674 |
long ai = a[i + 1]; |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
675 |
while (ai < a[j]) { |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
676 |
a[j + 1] = a[j]; |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
677 |
if (j-- == left) { |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
678 |
break; |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
679 |
} |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
680 |
} |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
681 |
a[j + 1] = ai; |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
682 |
} |
6896
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
683 |
} else { |
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
684 |
/* |
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
685 |
* Skip the longest ascending sequence. |
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
686 |
*/ |
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
687 |
do { |
8188
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
688 |
if (left >= right) { |
6896
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
689 |
return; |
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
690 |
} |
8188
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
691 |
} while (a[++left] >= a[left - 1]); |
6896
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
692 |
|
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
693 |
/* |
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
694 |
* Every element from adjoining part plays the role |
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
695 |
* of sentinel, therefore this allows us to avoid the |
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
696 |
* left range check on each iteration. Moreover, we use |
8188
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
697 |
* the more optimized algorithm, so called pair insertion |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
698 |
* sort, which is faster (in the context of Quicksort) |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
699 |
* than traditional implementation of insertion sort. |
6896
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
700 |
*/ |
8188
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
701 |
for (int k = left; ++left <= right; k = ++left) { |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
702 |
long a1 = a[k], a2 = a[left]; |
6896
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
703 |
|
8188
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
704 |
if (a1 < a2) { |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
705 |
a2 = a1; a1 = a[left]; |
6896
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
706 |
} |
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
707 |
while (a1 < a[--k]) { |
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
708 |
a[k + 2] = a[k]; |
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
709 |
} |
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
710 |
a[++k + 1] = a1; |
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
711 |
|
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
712 |
while (a2 < a[--k]) { |
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
713 |
a[k + 1] = a[k]; |
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
714 |
} |
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
715 |
a[k + 1] = a2; |
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
716 |
} |
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
717 |
long last = a[right]; |
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
718 |
|
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
719 |
while (last < a[--right]) { |
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
720 |
a[right + 1] = a[right]; |
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
721 |
} |
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
722 |
a[right + 1] = last; |
5995
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
723 |
} |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
724 |
return; |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
725 |
} |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
726 |
|
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
727 |
// Inexpensive approximation of length / 7 |
6896
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
728 |
int seventh = (length >> 3) + (length >> 6) + 1; |
5995
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
729 |
|
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
730 |
/* |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
731 |
* Sort five evenly spaced elements around (and including) the |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
732 |
* center element in the range. These elements will be used for |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
733 |
* pivot selection as described below. The choice for spacing |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
734 |
* these elements was empirically determined to work well on |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
735 |
* a wide variety of inputs. |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
736 |
*/ |
4170
a94a6faf44e6
6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents:
diff
changeset
|
737 |
int e3 = (left + right) >>> 1; // The midpoint |
5995
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
738 |
int e2 = e3 - seventh; |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
739 |
int e1 = e2 - seventh; |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
740 |
int e4 = e3 + seventh; |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
741 |
int e5 = e4 + seventh; |
4170
a94a6faf44e6
6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents:
diff
changeset
|
742 |
|
5995
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
743 |
// Sort these elements using insertion sort |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
744 |
if (a[e2] < a[e1]) { long t = a[e2]; a[e2] = a[e1]; a[e1] = t; } |
4241 | 745 |
|
5995
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
746 |
if (a[e3] < a[e2]) { long t = a[e3]; a[e3] = a[e2]; a[e2] = t; |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
747 |
if (t < a[e1]) { a[e2] = a[e1]; a[e1] = t; } |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
748 |
} |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
749 |
if (a[e4] < a[e3]) { long t = a[e4]; a[e4] = a[e3]; a[e3] = t; |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
750 |
if (t < a[e2]) { a[e3] = a[e2]; a[e2] = t; |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
751 |
if (t < a[e1]) { a[e2] = a[e1]; a[e1] = t; } |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
752 |
} |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
753 |
} |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
754 |
if (a[e5] < a[e4]) { long t = a[e5]; a[e5] = a[e4]; a[e4] = t; |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
755 |
if (t < a[e3]) { a[e4] = a[e3]; a[e3] = t; |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
756 |
if (t < a[e2]) { a[e3] = a[e2]; a[e2] = t; |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
757 |
if (t < a[e1]) { a[e2] = a[e1]; a[e1] = t; } |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
758 |
} |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
759 |
} |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
760 |
} |
4170
a94a6faf44e6
6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents:
diff
changeset
|
761 |
|
a94a6faf44e6
6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents:
diff
changeset
|
762 |
// Pointers |
5995
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
763 |
int less = left; // The index of the first element of center part |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
764 |
int great = right; // The index before the first element of right part |
4170
a94a6faf44e6
6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents:
diff
changeset
|
765 |
|
8188
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
766 |
if (a[e1] != a[e2] && a[e2] != a[e3] && a[e3] != a[e4] && a[e4] != a[e5]) { |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
767 |
/* |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
768 |
* Use the second and fourth of the five sorted elements as pivots. |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
769 |
* These values are inexpensive approximations of the first and |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
770 |
* second terciles of the array. Note that pivot1 <= pivot2. |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
771 |
*/ |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
772 |
long pivot1 = a[e2]; |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
773 |
long pivot2 = a[e4]; |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
774 |
|
5995
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
775 |
/* |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
776 |
* The first and the last elements to be sorted are moved to the |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
777 |
* locations formerly occupied by the pivots. When partitioning |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
778 |
* is complete, the pivots are swapped back into their final |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
779 |
* positions, and excluded from subsequent sorting. |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
780 |
*/ |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
781 |
a[e2] = a[left]; |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
782 |
a[e4] = a[right]; |
4170
a94a6faf44e6
6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents:
diff
changeset
|
783 |
|
5995
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
784 |
/* |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
785 |
* Skip elements, which are less or greater than pivot values. |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
786 |
*/ |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
787 |
while (a[++less] < pivot1); |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
788 |
while (a[--great] > pivot2); |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
789 |
|
4170
a94a6faf44e6
6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents:
diff
changeset
|
790 |
/* |
4356 | 791 |
* Partitioning: |
792 |
* |
|
5995
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
793 |
* left part center part right part |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
794 |
* +--------------------------------------------------------------+ |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
795 |
* | < pivot1 | pivot1 <= && <= pivot2 | ? | > pivot2 | |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
796 |
* +--------------------------------------------------------------+ |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
797 |
* ^ ^ ^ |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
798 |
* | | | |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
799 |
* less k great |
4356 | 800 |
* |
4170
a94a6faf44e6
6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents:
diff
changeset
|
801 |
* Invariants: |
4356 | 802 |
* |
4170
a94a6faf44e6
6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents:
diff
changeset
|
803 |
* all in (left, less) < pivot1 |
a94a6faf44e6
6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents:
diff
changeset
|
804 |
* pivot1 <= all in [less, k) <= pivot2 |
a94a6faf44e6
6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents:
diff
changeset
|
805 |
* all in (great, right) > pivot2 |
a94a6faf44e6
6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents:
diff
changeset
|
806 |
* |
5995
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
807 |
* Pointer k is the first index of ?-part. |
4170
a94a6faf44e6
6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents:
diff
changeset
|
808 |
*/ |
4241 | 809 |
outer: |
6896
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
810 |
for (int k = less - 1; ++k <= great; ) { |
4170
a94a6faf44e6
6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents:
diff
changeset
|
811 |
long ak = a[k]; |
4356 | 812 |
if (ak < pivot1) { // Move a[k] to left part |
5995
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
813 |
a[k] = a[less]; |
6896
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
814 |
/* |
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
815 |
* Here and below we use "a[i] = b; i++;" instead |
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
816 |
* of "a[i++] = b;" due to performance issue. |
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
817 |
*/ |
5995
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
818 |
a[less] = ak; |
8188
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
819 |
++less; |
4356 | 820 |
} else if (ak > pivot2) { // Move a[k] to right part |
4241 | 821 |
while (a[great] > pivot2) { |
4356 | 822 |
if (great-- == k) { |
4241 | 823 |
break outer; |
824 |
} |
|
4170
a94a6faf44e6
6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents:
diff
changeset
|
825 |
} |
6896
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
826 |
if (a[great] < pivot1) { // a[great] <= pivot2 |
4170
a94a6faf44e6
6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents:
diff
changeset
|
827 |
a[k] = a[less]; |
5995
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
828 |
a[less] = a[great]; |
8188
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
829 |
++less; |
4356 | 830 |
} else { // pivot1 <= a[great] <= pivot2 |
831 |
a[k] = a[great]; |
|
5995
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
832 |
} |
6896
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
833 |
/* |
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
834 |
* Here and below we use "a[i] = b; i--;" instead |
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
835 |
* of "a[i--] = b;" due to performance issue. |
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
836 |
*/ |
5995
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
837 |
a[great] = ak; |
8188
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
838 |
--great; |
5995
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
839 |
} |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
840 |
} |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
841 |
|
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
842 |
// Swap pivots into their final positions |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
843 |
a[left] = a[less - 1]; a[less - 1] = pivot1; |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
844 |
a[right] = a[great + 1]; a[great + 1] = pivot2; |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
845 |
|
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
846 |
// Sort left and right parts recursively, excluding known pivots |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
847 |
sort(a, left, less - 2, leftmost); |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
848 |
sort(a, great + 2, right, false); |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
849 |
|
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
850 |
/* |
6896
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
851 |
* If center part is too large (comprises > 4/7 of the array), |
5995
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
852 |
* swap internal pivot values to ends. |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
853 |
*/ |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
854 |
if (less < e1 && e5 < great) { |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
855 |
/* |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
856 |
* Skip elements, which are equal to pivot values. |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
857 |
*/ |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
858 |
while (a[less] == pivot1) { |
8188
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
859 |
++less; |
5995
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
860 |
} |
8188
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
861 |
|
5995
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
862 |
while (a[great] == pivot2) { |
8188
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
863 |
--great; |
5995
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
864 |
} |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
865 |
|
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
866 |
/* |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
867 |
* Partitioning: |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
868 |
* |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
869 |
* left part center part right part |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
870 |
* +----------------------------------------------------------+ |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
871 |
* | == pivot1 | pivot1 < && < pivot2 | ? | == pivot2 | |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
872 |
* +----------------------------------------------------------+ |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
873 |
* ^ ^ ^ |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
874 |
* | | | |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
875 |
* less k great |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
876 |
* |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
877 |
* Invariants: |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
878 |
* |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
879 |
* all in (*, less) == pivot1 |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
880 |
* pivot1 < all in [less, k) < pivot2 |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
881 |
* all in (great, *) == pivot2 |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
882 |
* |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
883 |
* Pointer k is the first index of ?-part. |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
884 |
*/ |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
885 |
outer: |
6896
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
886 |
for (int k = less - 1; ++k <= great; ) { |
5995
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
887 |
long ak = a[k]; |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
888 |
if (ak == pivot1) { // Move a[k] to left part |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
889 |
a[k] = a[less]; |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
890 |
a[less] = ak; |
8188
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
891 |
++less; |
5995
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
892 |
} else if (ak == pivot2) { // Move a[k] to right part |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
893 |
while (a[great] == pivot2) { |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
894 |
if (great-- == k) { |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
895 |
break outer; |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
896 |
} |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
897 |
} |
6896
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
898 |
if (a[great] == pivot1) { // a[great] < pivot2 |
5995
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
899 |
a[k] = a[less]; |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
900 |
/* |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
901 |
* Even though a[great] equals to pivot1, the |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
902 |
* assignment a[less] = pivot1 may be incorrect, |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
903 |
* if a[great] and pivot1 are floating-point zeros |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
904 |
* of different signs. Therefore in float and |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
905 |
* double sorting methods we have to use more |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
906 |
* accurate assignment a[less] = a[great]. |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
907 |
*/ |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
908 |
a[less] = pivot1; |
8188
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
909 |
++less; |
5995
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
910 |
} else { // pivot1 < a[great] < pivot2 |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
911 |
a[k] = a[great]; |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
912 |
} |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
913 |
a[great] = ak; |
8188
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
914 |
--great; |
4170
a94a6faf44e6
6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents:
diff
changeset
|
915 |
} |
a94a6faf44e6
6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents:
diff
changeset
|
916 |
} |
a94a6faf44e6
6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents:
diff
changeset
|
917 |
} |
5995
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
918 |
|
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
919 |
// Sort center part recursively |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
920 |
sort(a, less, great, false); |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
921 |
|
8188
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
922 |
} else { // Partitioning with one pivot |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
923 |
/* |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
924 |
* Use the third of the five sorted elements as pivot. |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
925 |
* This value is inexpensive approximation of the median. |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
926 |
*/ |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
927 |
long pivot = a[e3]; |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
928 |
|
4170
a94a6faf44e6
6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents:
diff
changeset
|
929 |
/* |
6896
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
930 |
* Partitioning degenerates to the traditional 3-way |
5995
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
931 |
* (or "Dutch National Flag") schema: |
4170
a94a6faf44e6
6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents:
diff
changeset
|
932 |
* |
5995
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
933 |
* left part center part right part |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
934 |
* +-------------------------------------------------+ |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
935 |
* | < pivot | == pivot | ? | > pivot | |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
936 |
* +-------------------------------------------------+ |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
937 |
* ^ ^ ^ |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
938 |
* | | | |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
939 |
* less k great |
4170
a94a6faf44e6
6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents:
diff
changeset
|
940 |
* |
a94a6faf44e6
6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents:
diff
changeset
|
941 |
* Invariants: |
a94a6faf44e6
6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents:
diff
changeset
|
942 |
* |
a94a6faf44e6
6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents:
diff
changeset
|
943 |
* all in (left, less) < pivot |
a94a6faf44e6
6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents:
diff
changeset
|
944 |
* all in [less, k) == pivot |
a94a6faf44e6
6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents:
diff
changeset
|
945 |
* all in (great, right) > pivot |
a94a6faf44e6
6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents:
diff
changeset
|
946 |
* |
5995
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
947 |
* Pointer k is the first index of ?-part. |
4170
a94a6faf44e6
6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents:
diff
changeset
|
948 |
*/ |
6896
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
949 |
for (int k = less; k <= great; ++k) { |
8188
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
950 |
if (a[k] == pivot) { |
4241 | 951 |
continue; |
4170
a94a6faf44e6
6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents:
diff
changeset
|
952 |
} |
5995
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
953 |
long ak = a[k]; |
8188
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
954 |
if (ak < pivot) { // Move a[k] to left part |
5995
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
955 |
a[k] = a[less]; |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
956 |
a[less] = ak; |
8188
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
957 |
++less; |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
958 |
} else { // a[k] > pivot - Move a[k] to right part |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
959 |
while (a[great] > pivot) { |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
960 |
--great; |
4170
a94a6faf44e6
6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents:
diff
changeset
|
961 |
} |
8188
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
962 |
if (a[great] < pivot) { // a[great] <= pivot |
4170
a94a6faf44e6
6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents:
diff
changeset
|
963 |
a[k] = a[less]; |
5995
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
964 |
a[less] = a[great]; |
8188
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
965 |
++less; |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
966 |
} else { // a[great] == pivot |
5995
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
967 |
/* |
8188
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
968 |
* Even though a[great] equals to pivot, the |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
969 |
* assignment a[k] = pivot may be incorrect, |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
970 |
* if a[great] and pivot are floating-point |
5995
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
971 |
* zeros of different signs. Therefore in float |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
972 |
* and double sorting methods we have to use |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
973 |
* more accurate assignment a[k] = a[great]. |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
974 |
*/ |
8188
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
975 |
a[k] = pivot; |
4170
a94a6faf44e6
6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents:
diff
changeset
|
976 |
} |
5995
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
977 |
a[great] = ak; |
8188
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
978 |
--great; |
4170
a94a6faf44e6
6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents:
diff
changeset
|
979 |
} |
a94a6faf44e6
6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents:
diff
changeset
|
980 |
} |
4356 | 981 |
|
6896
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
982 |
/* |
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
983 |
* Sort left and right parts recursively. |
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
984 |
* All elements from center part are equal |
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
985 |
* and, therefore, already sorted. |
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
986 |
*/ |
5995
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
987 |
sort(a, left, less - 1, leftmost); |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
988 |
sort(a, great + 1, right, false); |
4170
a94a6faf44e6
6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents:
diff
changeset
|
989 |
} |
4233 | 990 |
} |
991 |
||
992 |
/** |
|
17712
b56c69500af6
8014076: Arrays parallel and serial sorting improvements
dl
parents:
9035
diff
changeset
|
993 |
* Sorts the specified range of the array using the given |
b56c69500af6
8014076: Arrays parallel and serial sorting improvements
dl
parents:
9035
diff
changeset
|
994 |
* workspace array slice if possible for merging |
4233 | 995 |
* |
996 |
* @param a the array to be sorted |
|
6896
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
997 |
* @param left the index of the first element, inclusive, to be sorted |
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
998 |
* @param right the index of the last element, inclusive, to be sorted |
17712
b56c69500af6
8014076: Arrays parallel and serial sorting improvements
dl
parents:
9035
diff
changeset
|
999 |
* @param work a workspace array (slice) |
b56c69500af6
8014076: Arrays parallel and serial sorting improvements
dl
parents:
9035
diff
changeset
|
1000 |
* @param workBase origin of usable space in work array |
b56c69500af6
8014076: Arrays parallel and serial sorting improvements
dl
parents:
9035
diff
changeset
|
1001 |
* @param workLen usable size of work array |
4233 | 1002 |
*/ |
17712
b56c69500af6
8014076: Arrays parallel and serial sorting improvements
dl
parents:
9035
diff
changeset
|
1003 |
static void sort(short[] a, int left, int right, |
b56c69500af6
8014076: Arrays parallel and serial sorting improvements
dl
parents:
9035
diff
changeset
|
1004 |
short[] work, int workBase, int workLen) { |
6896
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
1005 |
// Use counting sort on large arrays |
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
1006 |
if (right - left > COUNTING_SORT_THRESHOLD_FOR_SHORT_OR_CHAR) { |
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
1007 |
int[] count = new int[NUM_SHORT_VALUES]; |
5995
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1008 |
|
8188
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
1009 |
for (int i = left - 1; ++i <= right; |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
1010 |
count[a[i] - Short.MIN_VALUE]++ |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
1011 |
); |
6896
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
1012 |
for (int i = NUM_SHORT_VALUES, k = right + 1; k > left; ) { |
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
1013 |
while (count[--i] == 0); |
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
1014 |
short value = (short) (i + Short.MIN_VALUE); |
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
1015 |
int s = count[i]; |
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
1016 |
|
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
1017 |
do { |
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
1018 |
a[--k] = value; |
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
1019 |
} while (--s > 0); |
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
1020 |
} |
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
1021 |
} else { // Use Dual-Pivot Quicksort on small arrays |
17712
b56c69500af6
8014076: Arrays parallel and serial sorting improvements
dl
parents:
9035
diff
changeset
|
1022 |
doSort(a, left, right, work, workBase, workLen); |
5995
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1023 |
} |
4233 | 1024 |
} |
1025 |
||
1026 |
/** The number of distinct short values. */ |
|
4170
a94a6faf44e6
6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents:
diff
changeset
|
1027 |
private static final int NUM_SHORT_VALUES = 1 << 16; |
a94a6faf44e6
6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents:
diff
changeset
|
1028 |
|
a94a6faf44e6
6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents:
diff
changeset
|
1029 |
/** |
8188
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
1030 |
* Sorts the specified range of the array. |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
1031 |
* |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
1032 |
* @param a the array to be sorted |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
1033 |
* @param left the index of the first element, inclusive, to be sorted |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
1034 |
* @param right the index of the last element, inclusive, to be sorted |
17712
b56c69500af6
8014076: Arrays parallel and serial sorting improvements
dl
parents:
9035
diff
changeset
|
1035 |
* @param work a workspace array (slice) |
b56c69500af6
8014076: Arrays parallel and serial sorting improvements
dl
parents:
9035
diff
changeset
|
1036 |
* @param workBase origin of usable space in work array |
b56c69500af6
8014076: Arrays parallel and serial sorting improvements
dl
parents:
9035
diff
changeset
|
1037 |
* @param workLen usable size of work array |
8188
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
1038 |
*/ |
17712
b56c69500af6
8014076: Arrays parallel and serial sorting improvements
dl
parents:
9035
diff
changeset
|
1039 |
private static void doSort(short[] a, int left, int right, |
b56c69500af6
8014076: Arrays parallel and serial sorting improvements
dl
parents:
9035
diff
changeset
|
1040 |
short[] work, int workBase, int workLen) { |
8188
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
1041 |
// Use Quicksort on small arrays |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
1042 |
if (right - left < QUICKSORT_THRESHOLD) { |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
1043 |
sort(a, left, right, true); |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
1044 |
return; |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
1045 |
} |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
1046 |
|
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
1047 |
/* |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
1048 |
* Index run[i] is the start of i-th run |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
1049 |
* (ascending or descending sequence). |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
1050 |
*/ |
8193
626b859aabb2
7018258: Dual-pivot updates in 7013585 can fail with ArrayIndexOutOfBoundsException
alanb
parents:
8188
diff
changeset
|
1051 |
int[] run = new int[MAX_RUN_COUNT + 1]; |
8188
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
1052 |
int count = 0; run[0] = left; |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
1053 |
|
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
1054 |
// Check if the array is nearly sorted |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
1055 |
for (int k = left; k < right; run[count] = k) { |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
1056 |
if (a[k] < a[k + 1]) { // ascending |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
1057 |
while (++k <= right && a[k - 1] <= a[k]); |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
1058 |
} else if (a[k] > a[k + 1]) { // descending |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
1059 |
while (++k <= right && a[k - 1] >= a[k]); |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
1060 |
for (int lo = run[count] - 1, hi = k; ++lo < --hi; ) { |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
1061 |
short t = a[lo]; a[lo] = a[hi]; a[hi] = t; |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
1062 |
} |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
1063 |
} else { // equal |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
1064 |
for (int m = MAX_RUN_LENGTH; ++k <= right && a[k - 1] == a[k]; ) { |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
1065 |
if (--m == 0) { |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
1066 |
sort(a, left, right, true); |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
1067 |
return; |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
1068 |
} |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
1069 |
} |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
1070 |
} |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
1071 |
|
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
1072 |
/* |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
1073 |
* The array is not highly structured, |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
1074 |
* use Quicksort instead of merge sort. |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
1075 |
*/ |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
1076 |
if (++count == MAX_RUN_COUNT) { |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
1077 |
sort(a, left, right, true); |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
1078 |
return; |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
1079 |
} |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
1080 |
} |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
1081 |
|
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
1082 |
// Check special cases |
17712
b56c69500af6
8014076: Arrays parallel and serial sorting improvements
dl
parents:
9035
diff
changeset
|
1083 |
// Implementation note: variable "right" is increased by 1. |
8188
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
1084 |
if (run[count] == right++) { // The last run contains one element |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
1085 |
run[++count] = right; |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
1086 |
} else if (count == 1) { // The array is already sorted |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
1087 |
return; |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
1088 |
} |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
1089 |
|
17712
b56c69500af6
8014076: Arrays parallel and serial sorting improvements
dl
parents:
9035
diff
changeset
|
1090 |
// Determine alternation base for merge |
b56c69500af6
8014076: Arrays parallel and serial sorting improvements
dl
parents:
9035
diff
changeset
|
1091 |
byte odd = 0; |
8188
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
1092 |
for (int n = 1; (n <<= 1) < count; odd ^= 1); |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
1093 |
|
17712
b56c69500af6
8014076: Arrays parallel and serial sorting improvements
dl
parents:
9035
diff
changeset
|
1094 |
// Use or create temporary array b for merging |
b56c69500af6
8014076: Arrays parallel and serial sorting improvements
dl
parents:
9035
diff
changeset
|
1095 |
short[] b; // temp array; alternates with a |
b56c69500af6
8014076: Arrays parallel and serial sorting improvements
dl
parents:
9035
diff
changeset
|
1096 |
int ao, bo; // array offsets from 'left' |
b56c69500af6
8014076: Arrays parallel and serial sorting improvements
dl
parents:
9035
diff
changeset
|
1097 |
int blen = right - left; // space needed for b |
b56c69500af6
8014076: Arrays parallel and serial sorting improvements
dl
parents:
9035
diff
changeset
|
1098 |
if (work == null || workLen < blen || workBase + blen > work.length) { |
b56c69500af6
8014076: Arrays parallel and serial sorting improvements
dl
parents:
9035
diff
changeset
|
1099 |
work = new short[blen]; |
b56c69500af6
8014076: Arrays parallel and serial sorting improvements
dl
parents:
9035
diff
changeset
|
1100 |
workBase = 0; |
b56c69500af6
8014076: Arrays parallel and serial sorting improvements
dl
parents:
9035
diff
changeset
|
1101 |
} |
8188
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
1102 |
if (odd == 0) { |
17712
b56c69500af6
8014076: Arrays parallel and serial sorting improvements
dl
parents:
9035
diff
changeset
|
1103 |
System.arraycopy(a, left, work, workBase, blen); |
b56c69500af6
8014076: Arrays parallel and serial sorting improvements
dl
parents:
9035
diff
changeset
|
1104 |
b = a; |
b56c69500af6
8014076: Arrays parallel and serial sorting improvements
dl
parents:
9035
diff
changeset
|
1105 |
bo = 0; |
b56c69500af6
8014076: Arrays parallel and serial sorting improvements
dl
parents:
9035
diff
changeset
|
1106 |
a = work; |
b56c69500af6
8014076: Arrays parallel and serial sorting improvements
dl
parents:
9035
diff
changeset
|
1107 |
ao = workBase - left; |
8188
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
1108 |
} else { |
17712
b56c69500af6
8014076: Arrays parallel and serial sorting improvements
dl
parents:
9035
diff
changeset
|
1109 |
b = work; |
b56c69500af6
8014076: Arrays parallel and serial sorting improvements
dl
parents:
9035
diff
changeset
|
1110 |
ao = 0; |
b56c69500af6
8014076: Arrays parallel and serial sorting improvements
dl
parents:
9035
diff
changeset
|
1111 |
bo = workBase - left; |
8188
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
1112 |
} |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
1113 |
|
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
1114 |
// Merging |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
1115 |
for (int last; count > 1; count = last) { |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
1116 |
for (int k = (last = 0) + 2; k <= count; k += 2) { |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
1117 |
int hi = run[k], mi = run[k - 1]; |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
1118 |
for (int i = run[k - 2], p = i, q = mi; i < hi; ++i) { |
17712
b56c69500af6
8014076: Arrays parallel and serial sorting improvements
dl
parents:
9035
diff
changeset
|
1119 |
if (q >= hi || p < mi && a[p + ao] <= a[q + ao]) { |
b56c69500af6
8014076: Arrays parallel and serial sorting improvements
dl
parents:
9035
diff
changeset
|
1120 |
b[i + bo] = a[p++ + ao]; |
8188
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
1121 |
} else { |
17712
b56c69500af6
8014076: Arrays parallel and serial sorting improvements
dl
parents:
9035
diff
changeset
|
1122 |
b[i + bo] = a[q++ + ao]; |
8188
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
1123 |
} |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
1124 |
} |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
1125 |
run[++last] = hi; |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
1126 |
} |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
1127 |
if ((count & 1) != 0) { |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
1128 |
for (int i = right, lo = run[count - 1]; --i >= lo; |
17712
b56c69500af6
8014076: Arrays parallel and serial sorting improvements
dl
parents:
9035
diff
changeset
|
1129 |
b[i + bo] = a[i + ao] |
8188
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
1130 |
); |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
1131 |
run[++last] = right; |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
1132 |
} |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
1133 |
short[] t = a; a = b; b = t; |
17712
b56c69500af6
8014076: Arrays parallel and serial sorting improvements
dl
parents:
9035
diff
changeset
|
1134 |
int o = ao; ao = bo; bo = o; |
8188
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
1135 |
} |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
1136 |
} |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
1137 |
|
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
1138 |
/** |
6896
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
1139 |
* Sorts the specified range of the array by Dual-Pivot Quicksort. |
4170
a94a6faf44e6
6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents:
diff
changeset
|
1140 |
* |
a94a6faf44e6
6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents:
diff
changeset
|
1141 |
* @param a the array to be sorted |
4233 | 1142 |
* @param left the index of the first element, inclusive, to be sorted |
1143 |
* @param right the index of the last element, inclusive, to be sorted |
|
6896
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
1144 |
* @param leftmost indicates if this part is the leftmost in the range |
5995
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1145 |
*/ |
8188
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
1146 |
private static void sort(short[] a, int left, int right, boolean leftmost) { |
5995
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1147 |
int length = right - left + 1; |
4241 | 1148 |
|
8188
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
1149 |
// Use insertion sort on tiny arrays |
5995
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1150 |
if (length < INSERTION_SORT_THRESHOLD) { |
6896
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
1151 |
if (leftmost) { |
5995
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1152 |
/* |
6896
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
1153 |
* Traditional (without sentinel) insertion sort, |
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
1154 |
* optimized for server VM, is used in case of |
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
1155 |
* the leftmost part. |
5995
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1156 |
*/ |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1157 |
for (int i = left, j = i; i < right; j = ++i) { |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1158 |
short ai = a[i + 1]; |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1159 |
while (ai < a[j]) { |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1160 |
a[j + 1] = a[j]; |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1161 |
if (j-- == left) { |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1162 |
break; |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1163 |
} |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1164 |
} |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1165 |
a[j + 1] = ai; |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1166 |
} |
6896
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
1167 |
} else { |
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
1168 |
/* |
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
1169 |
* Skip the longest ascending sequence. |
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
1170 |
*/ |
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
1171 |
do { |
8188
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
1172 |
if (left >= right) { |
6896
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
1173 |
return; |
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
1174 |
} |
8188
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
1175 |
} while (a[++left] >= a[left - 1]); |
6896
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
1176 |
|
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
1177 |
/* |
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
1178 |
* Every element from adjoining part plays the role |
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
1179 |
* of sentinel, therefore this allows us to avoid the |
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
1180 |
* left range check on each iteration. Moreover, we use |
8188
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
1181 |
* the more optimized algorithm, so called pair insertion |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
1182 |
* sort, which is faster (in the context of Quicksort) |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
1183 |
* than traditional implementation of insertion sort. |
6896
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
1184 |
*/ |
8188
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
1185 |
for (int k = left; ++left <= right; k = ++left) { |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
1186 |
short a1 = a[k], a2 = a[left]; |
6896
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
1187 |
|
8188
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
1188 |
if (a1 < a2) { |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
1189 |
a2 = a1; a1 = a[left]; |
6896
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
1190 |
} |
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
1191 |
while (a1 < a[--k]) { |
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
1192 |
a[k + 2] = a[k]; |
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
1193 |
} |
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
1194 |
a[++k + 1] = a1; |
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
1195 |
|
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
1196 |
while (a2 < a[--k]) { |
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
1197 |
a[k + 1] = a[k]; |
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
1198 |
} |
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
1199 |
a[k + 1] = a2; |
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
1200 |
} |
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
1201 |
short last = a[right]; |
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
1202 |
|
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
1203 |
while (last < a[--right]) { |
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
1204 |
a[right + 1] = a[right]; |
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
1205 |
} |
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
1206 |
a[right + 1] = last; |
5995
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1207 |
} |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1208 |
return; |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1209 |
} |
4241 | 1210 |
|
5995
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1211 |
// Inexpensive approximation of length / 7 |
6896
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
1212 |
int seventh = (length >> 3) + (length >> 6) + 1; |
5995
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1213 |
|
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1214 |
/* |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1215 |
* Sort five evenly spaced elements around (and including) the |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1216 |
* center element in the range. These elements will be used for |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1217 |
* pivot selection as described below. The choice for spacing |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1218 |
* these elements was empirically determined to work well on |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1219 |
* a wide variety of inputs. |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1220 |
*/ |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1221 |
int e3 = (left + right) >>> 1; // The midpoint |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1222 |
int e2 = e3 - seventh; |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1223 |
int e1 = e2 - seventh; |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1224 |
int e4 = e3 + seventh; |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1225 |
int e5 = e4 + seventh; |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1226 |
|
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1227 |
// Sort these elements using insertion sort |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1228 |
if (a[e2] < a[e1]) { short t = a[e2]; a[e2] = a[e1]; a[e1] = t; } |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1229 |
|
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1230 |
if (a[e3] < a[e2]) { short t = a[e3]; a[e3] = a[e2]; a[e2] = t; |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1231 |
if (t < a[e1]) { a[e2] = a[e1]; a[e1] = t; } |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1232 |
} |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1233 |
if (a[e4] < a[e3]) { short t = a[e4]; a[e4] = a[e3]; a[e3] = t; |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1234 |
if (t < a[e2]) { a[e3] = a[e2]; a[e2] = t; |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1235 |
if (t < a[e1]) { a[e2] = a[e1]; a[e1] = t; } |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1236 |
} |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1237 |
} |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1238 |
if (a[e5] < a[e4]) { short t = a[e5]; a[e5] = a[e4]; a[e4] = t; |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1239 |
if (t < a[e3]) { a[e4] = a[e3]; a[e3] = t; |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1240 |
if (t < a[e2]) { a[e3] = a[e2]; a[e2] = t; |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1241 |
if (t < a[e1]) { a[e2] = a[e1]; a[e1] = t; } |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1242 |
} |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1243 |
} |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1244 |
} |
4170
a94a6faf44e6
6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents:
diff
changeset
|
1245 |
|
a94a6faf44e6
6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents:
diff
changeset
|
1246 |
// Pointers |
5995
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1247 |
int less = left; // The index of the first element of center part |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1248 |
int great = right; // The index before the first element of right part |
4170
a94a6faf44e6
6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents:
diff
changeset
|
1249 |
|
8188
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
1250 |
if (a[e1] != a[e2] && a[e2] != a[e3] && a[e3] != a[e4] && a[e4] != a[e5]) { |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
1251 |
/* |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
1252 |
* Use the second and fourth of the five sorted elements as pivots. |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
1253 |
* These values are inexpensive approximations of the first and |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
1254 |
* second terciles of the array. Note that pivot1 <= pivot2. |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
1255 |
*/ |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
1256 |
short pivot1 = a[e2]; |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
1257 |
short pivot2 = a[e4]; |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
1258 |
|
5995
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1259 |
/* |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1260 |
* The first and the last elements to be sorted are moved to the |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1261 |
* locations formerly occupied by the pivots. When partitioning |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1262 |
* is complete, the pivots are swapped back into their final |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1263 |
* positions, and excluded from subsequent sorting. |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1264 |
*/ |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1265 |
a[e2] = a[left]; |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1266 |
a[e4] = a[right]; |
4170
a94a6faf44e6
6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents:
diff
changeset
|
1267 |
|
5995
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1268 |
/* |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1269 |
* Skip elements, which are less or greater than pivot values. |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1270 |
*/ |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1271 |
while (a[++less] < pivot1); |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1272 |
while (a[--great] > pivot2); |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1273 |
|
4170
a94a6faf44e6
6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents:
diff
changeset
|
1274 |
/* |
4356 | 1275 |
* Partitioning: |
1276 |
* |
|
5995
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1277 |
* left part center part right part |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1278 |
* +--------------------------------------------------------------+ |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1279 |
* | < pivot1 | pivot1 <= && <= pivot2 | ? | > pivot2 | |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1280 |
* +--------------------------------------------------------------+ |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1281 |
* ^ ^ ^ |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1282 |
* | | | |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1283 |
* less k great |
4356 | 1284 |
* |
4170
a94a6faf44e6
6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents:
diff
changeset
|
1285 |
* Invariants: |
4356 | 1286 |
* |
4170
a94a6faf44e6
6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents:
diff
changeset
|
1287 |
* all in (left, less) < pivot1 |
a94a6faf44e6
6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents:
diff
changeset
|
1288 |
* pivot1 <= all in [less, k) <= pivot2 |
a94a6faf44e6
6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents:
diff
changeset
|
1289 |
* all in (great, right) > pivot2 |
a94a6faf44e6
6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents:
diff
changeset
|
1290 |
* |
5995
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1291 |
* Pointer k is the first index of ?-part. |
4170
a94a6faf44e6
6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents:
diff
changeset
|
1292 |
*/ |
4241 | 1293 |
outer: |
6896
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
1294 |
for (int k = less - 1; ++k <= great; ) { |
4170
a94a6faf44e6
6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents:
diff
changeset
|
1295 |
short ak = a[k]; |
4356 | 1296 |
if (ak < pivot1) { // Move a[k] to left part |
5995
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1297 |
a[k] = a[less]; |
6896
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
1298 |
/* |
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
1299 |
* Here and below we use "a[i] = b; i++;" instead |
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
1300 |
* of "a[i++] = b;" due to performance issue. |
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
1301 |
*/ |
5995
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1302 |
a[less] = ak; |
8188
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
1303 |
++less; |
4356 | 1304 |
} else if (ak > pivot2) { // Move a[k] to right part |
4241 | 1305 |
while (a[great] > pivot2) { |
4356 | 1306 |
if (great-- == k) { |
4241 | 1307 |
break outer; |
1308 |
} |
|
4170
a94a6faf44e6
6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents:
diff
changeset
|
1309 |
} |
6896
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
1310 |
if (a[great] < pivot1) { // a[great] <= pivot2 |
4170
a94a6faf44e6
6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents:
diff
changeset
|
1311 |
a[k] = a[less]; |
5995
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1312 |
a[less] = a[great]; |
8188
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
1313 |
++less; |
4356 | 1314 |
} else { // pivot1 <= a[great] <= pivot2 |
1315 |
a[k] = a[great]; |
|
5995
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1316 |
} |
6896
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
1317 |
/* |
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
1318 |
* Here and below we use "a[i] = b; i--;" instead |
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
1319 |
* of "a[i--] = b;" due to performance issue. |
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
1320 |
*/ |
5995
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1321 |
a[great] = ak; |
8188
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
1322 |
--great; |
5995
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1323 |
} |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1324 |
} |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1325 |
|
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1326 |
// Swap pivots into their final positions |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1327 |
a[left] = a[less - 1]; a[less - 1] = pivot1; |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1328 |
a[right] = a[great + 1]; a[great + 1] = pivot2; |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1329 |
|
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1330 |
// Sort left and right parts recursively, excluding known pivots |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1331 |
sort(a, left, less - 2, leftmost); |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1332 |
sort(a, great + 2, right, false); |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1333 |
|
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1334 |
/* |
6896
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
1335 |
* If center part is too large (comprises > 4/7 of the array), |
5995
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1336 |
* swap internal pivot values to ends. |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1337 |
*/ |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1338 |
if (less < e1 && e5 < great) { |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1339 |
/* |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1340 |
* Skip elements, which are equal to pivot values. |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1341 |
*/ |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1342 |
while (a[less] == pivot1) { |
8188
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
1343 |
++less; |
5995
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1344 |
} |
8188
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
1345 |
|
5995
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1346 |
while (a[great] == pivot2) { |
8188
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
1347 |
--great; |
5995
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1348 |
} |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1349 |
|
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1350 |
/* |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1351 |
* Partitioning: |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1352 |
* |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1353 |
* left part center part right part |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1354 |
* +----------------------------------------------------------+ |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1355 |
* | == pivot1 | pivot1 < && < pivot2 | ? | == pivot2 | |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1356 |
* +----------------------------------------------------------+ |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1357 |
* ^ ^ ^ |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1358 |
* | | | |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1359 |
* less k great |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1360 |
* |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1361 |
* Invariants: |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1362 |
* |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1363 |
* all in (*, less) == pivot1 |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1364 |
* pivot1 < all in [less, k) < pivot2 |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1365 |
* all in (great, *) == pivot2 |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1366 |
* |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1367 |
* Pointer k is the first index of ?-part. |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1368 |
*/ |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1369 |
outer: |
6896
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
1370 |
for (int k = less - 1; ++k <= great; ) { |
5995
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1371 |
short ak = a[k]; |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1372 |
if (ak == pivot1) { // Move a[k] to left part |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1373 |
a[k] = a[less]; |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1374 |
a[less] = ak; |
8188
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
1375 |
++less; |
5995
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1376 |
} else if (ak == pivot2) { // Move a[k] to right part |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1377 |
while (a[great] == pivot2) { |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1378 |
if (great-- == k) { |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1379 |
break outer; |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1380 |
} |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1381 |
} |
6896
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
1382 |
if (a[great] == pivot1) { // a[great] < pivot2 |
5995
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1383 |
a[k] = a[less]; |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1384 |
/* |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1385 |
* Even though a[great] equals to pivot1, the |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1386 |
* assignment a[less] = pivot1 may be incorrect, |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1387 |
* if a[great] and pivot1 are floating-point zeros |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1388 |
* of different signs. Therefore in float and |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1389 |
* double sorting methods we have to use more |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1390 |
* accurate assignment a[less] = a[great]. |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1391 |
*/ |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1392 |
a[less] = pivot1; |
8188
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
1393 |
++less; |
5995
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1394 |
} else { // pivot1 < a[great] < pivot2 |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1395 |
a[k] = a[great]; |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1396 |
} |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1397 |
a[great] = ak; |
8188
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
1398 |
--great; |
4170
a94a6faf44e6
6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents:
diff
changeset
|
1399 |
} |
a94a6faf44e6
6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents:
diff
changeset
|
1400 |
} |
a94a6faf44e6
6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents:
diff
changeset
|
1401 |
} |
5995
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1402 |
|
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1403 |
// Sort center part recursively |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1404 |
sort(a, less, great, false); |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1405 |
|
8188
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
1406 |
} else { // Partitioning with one pivot |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
1407 |
/* |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
1408 |
* Use the third of the five sorted elements as pivot. |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
1409 |
* This value is inexpensive approximation of the median. |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
1410 |
*/ |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
1411 |
short pivot = a[e3]; |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
1412 |
|
4170
a94a6faf44e6
6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents:
diff
changeset
|
1413 |
/* |
6896
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
1414 |
* Partitioning degenerates to the traditional 3-way |
5995
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1415 |
* (or "Dutch National Flag") schema: |
4170
a94a6faf44e6
6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents:
diff
changeset
|
1416 |
* |
5995
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1417 |
* left part center part right part |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1418 |
* +-------------------------------------------------+ |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1419 |
* | < pivot | == pivot | ? | > pivot | |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1420 |
* +-------------------------------------------------+ |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1421 |
* ^ ^ ^ |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1422 |
* | | | |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1423 |
* less k great |
4170
a94a6faf44e6
6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents:
diff
changeset
|
1424 |
* |
a94a6faf44e6
6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents:
diff
changeset
|
1425 |
* Invariants: |
a94a6faf44e6
6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents:
diff
changeset
|
1426 |
* |
a94a6faf44e6
6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents:
diff
changeset
|
1427 |
* all in (left, less) < pivot |
a94a6faf44e6
6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents:
diff
changeset
|
1428 |
* all in [less, k) == pivot |
a94a6faf44e6
6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents:
diff
changeset
|
1429 |
* all in (great, right) > pivot |
a94a6faf44e6
6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents:
diff
changeset
|
1430 |
* |
5995
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1431 |
* Pointer k is the first index of ?-part. |
4170
a94a6faf44e6
6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents:
diff
changeset
|
1432 |
*/ |
6896
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
1433 |
for (int k = less; k <= great; ++k) { |
8188
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
1434 |
if (a[k] == pivot) { |
4241 | 1435 |
continue; |
4170
a94a6faf44e6
6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents:
diff
changeset
|
1436 |
} |
5995
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1437 |
short ak = a[k]; |
8188
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
1438 |
if (ak < pivot) { // Move a[k] to left part |
5995
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1439 |
a[k] = a[less]; |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1440 |
a[less] = ak; |
8188
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
1441 |
++less; |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
1442 |
} else { // a[k] > pivot - Move a[k] to right part |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
1443 |
while (a[great] > pivot) { |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
1444 |
--great; |
4170
a94a6faf44e6
6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents:
diff
changeset
|
1445 |
} |
8188
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
1446 |
if (a[great] < pivot) { // a[great] <= pivot |
4170
a94a6faf44e6
6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents:
diff
changeset
|
1447 |
a[k] = a[less]; |
5995
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1448 |
a[less] = a[great]; |
8188
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
1449 |
++less; |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
1450 |
} else { // a[great] == pivot |
5995
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1451 |
/* |
8188
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
1452 |
* Even though a[great] equals to pivot, the |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
1453 |
* assignment a[k] = pivot may be incorrect, |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
1454 |
* if a[great] and pivot are floating-point |
5995
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1455 |
* zeros of different signs. Therefore in float |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1456 |
* and double sorting methods we have to use |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1457 |
* more accurate assignment a[k] = a[great]. |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1458 |
*/ |
8188
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
1459 |
a[k] = pivot; |
4170
a94a6faf44e6
6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents:
diff
changeset
|
1460 |
} |
5995
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1461 |
a[great] = ak; |
8188
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
1462 |
--great; |
4170
a94a6faf44e6
6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents:
diff
changeset
|
1463 |
} |
a94a6faf44e6
6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents:
diff
changeset
|
1464 |
} |
4356 | 1465 |
|
6896
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
1466 |
/* |
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
1467 |
* Sort left and right parts recursively. |
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
1468 |
* All elements from center part are equal |
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
1469 |
* and, therefore, already sorted. |
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
1470 |
*/ |
5995
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1471 |
sort(a, left, less - 1, leftmost); |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1472 |
sort(a, great + 1, right, false); |
4170
a94a6faf44e6
6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents:
diff
changeset
|
1473 |
} |
a94a6faf44e6
6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents:
diff
changeset
|
1474 |
} |
a94a6faf44e6
6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents:
diff
changeset
|
1475 |
|
a94a6faf44e6
6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents:
diff
changeset
|
1476 |
/** |
17712
b56c69500af6
8014076: Arrays parallel and serial sorting improvements
dl
parents:
9035
diff
changeset
|
1477 |
* Sorts the specified range of the array using the given |
b56c69500af6
8014076: Arrays parallel and serial sorting improvements
dl
parents:
9035
diff
changeset
|
1478 |
* workspace array slice if possible for merging |
4170
a94a6faf44e6
6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents:
diff
changeset
|
1479 |
* |
a94a6faf44e6
6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents:
diff
changeset
|
1480 |
* @param a the array to be sorted |
6896
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
1481 |
* @param left the index of the first element, inclusive, to be sorted |
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
1482 |
* @param right the index of the last element, inclusive, to be sorted |
17712
b56c69500af6
8014076: Arrays parallel and serial sorting improvements
dl
parents:
9035
diff
changeset
|
1483 |
* @param work a workspace array (slice) |
b56c69500af6
8014076: Arrays parallel and serial sorting improvements
dl
parents:
9035
diff
changeset
|
1484 |
* @param workBase origin of usable space in work array |
b56c69500af6
8014076: Arrays parallel and serial sorting improvements
dl
parents:
9035
diff
changeset
|
1485 |
* @param workLen usable size of work array |
4170
a94a6faf44e6
6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents:
diff
changeset
|
1486 |
*/ |
17712
b56c69500af6
8014076: Arrays parallel and serial sorting improvements
dl
parents:
9035
diff
changeset
|
1487 |
static void sort(char[] a, int left, int right, |
b56c69500af6
8014076: Arrays parallel and serial sorting improvements
dl
parents:
9035
diff
changeset
|
1488 |
char[] work, int workBase, int workLen) { |
6896
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
1489 |
// Use counting sort on large arrays |
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
1490 |
if (right - left > COUNTING_SORT_THRESHOLD_FOR_SHORT_OR_CHAR) { |
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
1491 |
int[] count = new int[NUM_CHAR_VALUES]; |
5995
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1492 |
|
8188
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
1493 |
for (int i = left - 1; ++i <= right; |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
1494 |
count[a[i]]++ |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
1495 |
); |
6896
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
1496 |
for (int i = NUM_CHAR_VALUES, k = right + 1; k > left; ) { |
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
1497 |
while (count[--i] == 0); |
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
1498 |
char value = (char) i; |
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
1499 |
int s = count[i]; |
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
1500 |
|
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
1501 |
do { |
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
1502 |
a[--k] = value; |
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
1503 |
} while (--s > 0); |
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
1504 |
} |
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
1505 |
} else { // Use Dual-Pivot Quicksort on small arrays |
17712
b56c69500af6
8014076: Arrays parallel and serial sorting improvements
dl
parents:
9035
diff
changeset
|
1506 |
doSort(a, left, right, work, workBase, workLen); |
5995
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1507 |
} |
4170
a94a6faf44e6
6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents:
diff
changeset
|
1508 |
} |
a94a6faf44e6
6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents:
diff
changeset
|
1509 |
|
4233 | 1510 |
/** The number of distinct char values. */ |
4170
a94a6faf44e6
6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents:
diff
changeset
|
1511 |
private static final int NUM_CHAR_VALUES = 1 << 16; |
a94a6faf44e6
6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents:
diff
changeset
|
1512 |
|
a94a6faf44e6
6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents:
diff
changeset
|
1513 |
/** |
8188
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
1514 |
* Sorts the specified range of the array. |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
1515 |
* |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
1516 |
* @param a the array to be sorted |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
1517 |
* @param left the index of the first element, inclusive, to be sorted |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
1518 |
* @param right the index of the last element, inclusive, to be sorted |
17712
b56c69500af6
8014076: Arrays parallel and serial sorting improvements
dl
parents:
9035
diff
changeset
|
1519 |
* @param work a workspace array (slice) |
b56c69500af6
8014076: Arrays parallel and serial sorting improvements
dl
parents:
9035
diff
changeset
|
1520 |
* @param workBase origin of usable space in work array |
b56c69500af6
8014076: Arrays parallel and serial sorting improvements
dl
parents:
9035
diff
changeset
|
1521 |
* @param workLen usable size of work array |
8188
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
1522 |
*/ |
17712
b56c69500af6
8014076: Arrays parallel and serial sorting improvements
dl
parents:
9035
diff
changeset
|
1523 |
private static void doSort(char[] a, int left, int right, |
b56c69500af6
8014076: Arrays parallel and serial sorting improvements
dl
parents:
9035
diff
changeset
|
1524 |
char[] work, int workBase, int workLen) { |
8188
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
1525 |
// Use Quicksort on small arrays |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
1526 |
if (right - left < QUICKSORT_THRESHOLD) { |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
1527 |
sort(a, left, right, true); |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
1528 |
return; |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
1529 |
} |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
1530 |
|
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
1531 |
/* |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
1532 |
* Index run[i] is the start of i-th run |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
1533 |
* (ascending or descending sequence). |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
1534 |
*/ |
8193
626b859aabb2
7018258: Dual-pivot updates in 7013585 can fail with ArrayIndexOutOfBoundsException
alanb
parents:
8188
diff
changeset
|
1535 |
int[] run = new int[MAX_RUN_COUNT + 1]; |
8188
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
1536 |
int count = 0; run[0] = left; |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
1537 |
|
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
1538 |
// Check if the array is nearly sorted |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
1539 |
for (int k = left; k < right; run[count] = k) { |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
1540 |
if (a[k] < a[k + 1]) { // ascending |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
1541 |
while (++k <= right && a[k - 1] <= a[k]); |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
1542 |
} else if (a[k] > a[k + 1]) { // descending |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
1543 |
while (++k <= right && a[k - 1] >= a[k]); |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
1544 |
for (int lo = run[count] - 1, hi = k; ++lo < --hi; ) { |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
1545 |
char t = a[lo]; a[lo] = a[hi]; a[hi] = t; |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
1546 |
} |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
1547 |
} else { // equal |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
1548 |
for (int m = MAX_RUN_LENGTH; ++k <= right && a[k - 1] == a[k]; ) { |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
1549 |
if (--m == 0) { |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
1550 |
sort(a, left, right, true); |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
1551 |
return; |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
1552 |
} |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
1553 |
} |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
1554 |
} |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
1555 |
|
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
1556 |
/* |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
1557 |
* The array is not highly structured, |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
1558 |
* use Quicksort instead of merge sort. |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
1559 |
*/ |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
1560 |
if (++count == MAX_RUN_COUNT) { |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
1561 |
sort(a, left, right, true); |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
1562 |
return; |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
1563 |
} |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
1564 |
} |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
1565 |
|
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
1566 |
// Check special cases |
17712
b56c69500af6
8014076: Arrays parallel and serial sorting improvements
dl
parents:
9035
diff
changeset
|
1567 |
// Implementation note: variable "right" is increased by 1. |
8188
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
1568 |
if (run[count] == right++) { // The last run contains one element |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
1569 |
run[++count] = right; |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
1570 |
} else if (count == 1) { // The array is already sorted |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
1571 |
return; |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
1572 |
} |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
1573 |
|
17712
b56c69500af6
8014076: Arrays parallel and serial sorting improvements
dl
parents:
9035
diff
changeset
|
1574 |
// Determine alternation base for merge |
b56c69500af6
8014076: Arrays parallel and serial sorting improvements
dl
parents:
9035
diff
changeset
|
1575 |
byte odd = 0; |
8188
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
1576 |
for (int n = 1; (n <<= 1) < count; odd ^= 1); |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
1577 |
|
17712
b56c69500af6
8014076: Arrays parallel and serial sorting improvements
dl
parents:
9035
diff
changeset
|
1578 |
// Use or create temporary array b for merging |
b56c69500af6
8014076: Arrays parallel and serial sorting improvements
dl
parents:
9035
diff
changeset
|
1579 |
char[] b; // temp array; alternates with a |
b56c69500af6
8014076: Arrays parallel and serial sorting improvements
dl
parents:
9035
diff
changeset
|
1580 |
int ao, bo; // array offsets from 'left' |
b56c69500af6
8014076: Arrays parallel and serial sorting improvements
dl
parents:
9035
diff
changeset
|
1581 |
int blen = right - left; // space needed for b |
b56c69500af6
8014076: Arrays parallel and serial sorting improvements
dl
parents:
9035
diff
changeset
|
1582 |
if (work == null || workLen < blen || workBase + blen > work.length) { |
b56c69500af6
8014076: Arrays parallel and serial sorting improvements
dl
parents:
9035
diff
changeset
|
1583 |
work = new char[blen]; |
b56c69500af6
8014076: Arrays parallel and serial sorting improvements
dl
parents:
9035
diff
changeset
|
1584 |
workBase = 0; |
b56c69500af6
8014076: Arrays parallel and serial sorting improvements
dl
parents:
9035
diff
changeset
|
1585 |
} |
8188
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
1586 |
if (odd == 0) { |
17712
b56c69500af6
8014076: Arrays parallel and serial sorting improvements
dl
parents:
9035
diff
changeset
|
1587 |
System.arraycopy(a, left, work, workBase, blen); |
b56c69500af6
8014076: Arrays parallel and serial sorting improvements
dl
parents:
9035
diff
changeset
|
1588 |
b = a; |
b56c69500af6
8014076: Arrays parallel and serial sorting improvements
dl
parents:
9035
diff
changeset
|
1589 |
bo = 0; |
b56c69500af6
8014076: Arrays parallel and serial sorting improvements
dl
parents:
9035
diff
changeset
|
1590 |
a = work; |
b56c69500af6
8014076: Arrays parallel and serial sorting improvements
dl
parents:
9035
diff
changeset
|
1591 |
ao = workBase - left; |
8188
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
1592 |
} else { |
17712
b56c69500af6
8014076: Arrays parallel and serial sorting improvements
dl
parents:
9035
diff
changeset
|
1593 |
b = work; |
b56c69500af6
8014076: Arrays parallel and serial sorting improvements
dl
parents:
9035
diff
changeset
|
1594 |
ao = 0; |
b56c69500af6
8014076: Arrays parallel and serial sorting improvements
dl
parents:
9035
diff
changeset
|
1595 |
bo = workBase - left; |
8188
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
1596 |
} |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
1597 |
|
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
1598 |
// Merging |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
1599 |
for (int last; count > 1; count = last) { |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
1600 |
for (int k = (last = 0) + 2; k <= count; k += 2) { |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
1601 |
int hi = run[k], mi = run[k - 1]; |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
1602 |
for (int i = run[k - 2], p = i, q = mi; i < hi; ++i) { |
17712
b56c69500af6
8014076: Arrays parallel and serial sorting improvements
dl
parents:
9035
diff
changeset
|
1603 |
if (q >= hi || p < mi && a[p + ao] <= a[q + ao]) { |
b56c69500af6
8014076: Arrays parallel and serial sorting improvements
dl
parents:
9035
diff
changeset
|
1604 |
b[i + bo] = a[p++ + ao]; |
8188
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
1605 |
} else { |
17712
b56c69500af6
8014076: Arrays parallel and serial sorting improvements
dl
parents:
9035
diff
changeset
|
1606 |
b[i + bo] = a[q++ + ao]; |
8188
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
1607 |
} |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
1608 |
} |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
1609 |
run[++last] = hi; |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
1610 |
} |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
1611 |
if ((count & 1) != 0) { |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
1612 |
for (int i = right, lo = run[count - 1]; --i >= lo; |
17712
b56c69500af6
8014076: Arrays parallel and serial sorting improvements
dl
parents:
9035
diff
changeset
|
1613 |
b[i + bo] = a[i + ao] |
8188
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
1614 |
); |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
1615 |
run[++last] = right; |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
1616 |
} |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
1617 |
char[] t = a; a = b; b = t; |
17712
b56c69500af6
8014076: Arrays parallel and serial sorting improvements
dl
parents:
9035
diff
changeset
|
1618 |
int o = ao; ao = bo; bo = o; |
8188
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
1619 |
} |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
1620 |
} |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
1621 |
|
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
1622 |
/** |
6896
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
1623 |
* Sorts the specified range of the array by Dual-Pivot Quicksort. |
4170
a94a6faf44e6
6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents:
diff
changeset
|
1624 |
* |
a94a6faf44e6
6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents:
diff
changeset
|
1625 |
* @param a the array to be sorted |
4233 | 1626 |
* @param left the index of the first element, inclusive, to be sorted |
1627 |
* @param right the index of the last element, inclusive, to be sorted |
|
6896
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
1628 |
* @param leftmost indicates if this part is the leftmost in the range |
5995
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1629 |
*/ |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1630 |
private static void sort(char[] a, int left, int right, boolean leftmost) { |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1631 |
int length = right - left + 1; |
4241 | 1632 |
|
8188
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
1633 |
// Use insertion sort on tiny arrays |
5995
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1634 |
if (length < INSERTION_SORT_THRESHOLD) { |
6896
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
1635 |
if (leftmost) { |
5995
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1636 |
/* |
6896
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
1637 |
* Traditional (without sentinel) insertion sort, |
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
1638 |
* optimized for server VM, is used in case of |
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
1639 |
* the leftmost part. |
5995
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1640 |
*/ |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1641 |
for (int i = left, j = i; i < right; j = ++i) { |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1642 |
char ai = a[i + 1]; |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1643 |
while (ai < a[j]) { |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1644 |
a[j + 1] = a[j]; |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1645 |
if (j-- == left) { |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1646 |
break; |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1647 |
} |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1648 |
} |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1649 |
a[j + 1] = ai; |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1650 |
} |
6896
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
1651 |
} else { |
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
1652 |
/* |
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
1653 |
* Skip the longest ascending sequence. |
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
1654 |
*/ |
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
1655 |
do { |
8188
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
1656 |
if (left >= right) { |
6896
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
1657 |
return; |
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
1658 |
} |
8188
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
1659 |
} while (a[++left] >= a[left - 1]); |
6896
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
1660 |
|
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
1661 |
/* |
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
1662 |
* Every element from adjoining part plays the role |
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
1663 |
* of sentinel, therefore this allows us to avoid the |
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
1664 |
* left range check on each iteration. Moreover, we use |
8188
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
1665 |
* the more optimized algorithm, so called pair insertion |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
1666 |
* sort, which is faster (in the context of Quicksort) |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
1667 |
* than traditional implementation of insertion sort. |
6896
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
1668 |
*/ |
8188
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
1669 |
for (int k = left; ++left <= right; k = ++left) { |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
1670 |
char a1 = a[k], a2 = a[left]; |
6896
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
1671 |
|
8188
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
1672 |
if (a1 < a2) { |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
1673 |
a2 = a1; a1 = a[left]; |
6896
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
1674 |
} |
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
1675 |
while (a1 < a[--k]) { |
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
1676 |
a[k + 2] = a[k]; |
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
1677 |
} |
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
1678 |
a[++k + 1] = a1; |
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
1679 |
|
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
1680 |
while (a2 < a[--k]) { |
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
1681 |
a[k + 1] = a[k]; |
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
1682 |
} |
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
1683 |
a[k + 1] = a2; |
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
1684 |
} |
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
1685 |
char last = a[right]; |
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
1686 |
|
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
1687 |
while (last < a[--right]) { |
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
1688 |
a[right + 1] = a[right]; |
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
1689 |
} |
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
1690 |
a[right + 1] = last; |
5995
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1691 |
} |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1692 |
return; |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1693 |
} |
4241 | 1694 |
|
5995
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1695 |
// Inexpensive approximation of length / 7 |
6896
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
1696 |
int seventh = (length >> 3) + (length >> 6) + 1; |
5995
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1697 |
|
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1698 |
/* |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1699 |
* Sort five evenly spaced elements around (and including) the |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1700 |
* center element in the range. These elements will be used for |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1701 |
* pivot selection as described below. The choice for spacing |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1702 |
* these elements was empirically determined to work well on |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1703 |
* a wide variety of inputs. |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1704 |
*/ |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1705 |
int e3 = (left + right) >>> 1; // The midpoint |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1706 |
int e2 = e3 - seventh; |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1707 |
int e1 = e2 - seventh; |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1708 |
int e4 = e3 + seventh; |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1709 |
int e5 = e4 + seventh; |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1710 |
|
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1711 |
// Sort these elements using insertion sort |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1712 |
if (a[e2] < a[e1]) { char t = a[e2]; a[e2] = a[e1]; a[e1] = t; } |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1713 |
|
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1714 |
if (a[e3] < a[e2]) { char t = a[e3]; a[e3] = a[e2]; a[e2] = t; |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1715 |
if (t < a[e1]) { a[e2] = a[e1]; a[e1] = t; } |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1716 |
} |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1717 |
if (a[e4] < a[e3]) { char t = a[e4]; a[e4] = a[e3]; a[e3] = t; |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1718 |
if (t < a[e2]) { a[e3] = a[e2]; a[e2] = t; |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1719 |
if (t < a[e1]) { a[e2] = a[e1]; a[e1] = t; } |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1720 |
} |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1721 |
} |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1722 |
if (a[e5] < a[e4]) { char t = a[e5]; a[e5] = a[e4]; a[e4] = t; |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1723 |
if (t < a[e3]) { a[e4] = a[e3]; a[e3] = t; |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1724 |
if (t < a[e2]) { a[e3] = a[e2]; a[e2] = t; |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1725 |
if (t < a[e1]) { a[e2] = a[e1]; a[e1] = t; } |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1726 |
} |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1727 |
} |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1728 |
} |
4170
a94a6faf44e6
6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents:
diff
changeset
|
1729 |
|
a94a6faf44e6
6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents:
diff
changeset
|
1730 |
// Pointers |
5995
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1731 |
int less = left; // The index of the first element of center part |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1732 |
int great = right; // The index before the first element of right part |
4170
a94a6faf44e6
6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents:
diff
changeset
|
1733 |
|
8188
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
1734 |
if (a[e1] != a[e2] && a[e2] != a[e3] && a[e3] != a[e4] && a[e4] != a[e5]) { |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
1735 |
/* |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
1736 |
* Use the second and fourth of the five sorted elements as pivots. |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
1737 |
* These values are inexpensive approximations of the first and |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
1738 |
* second terciles of the array. Note that pivot1 <= pivot2. |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
1739 |
*/ |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
1740 |
char pivot1 = a[e2]; |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
1741 |
char pivot2 = a[e4]; |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
1742 |
|
5995
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1743 |
/* |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1744 |
* The first and the last elements to be sorted are moved to the |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1745 |
* locations formerly occupied by the pivots. When partitioning |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1746 |
* is complete, the pivots are swapped back into their final |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1747 |
* positions, and excluded from subsequent sorting. |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1748 |
*/ |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1749 |
a[e2] = a[left]; |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1750 |
a[e4] = a[right]; |
4170
a94a6faf44e6
6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents:
diff
changeset
|
1751 |
|
5995
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1752 |
/* |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1753 |
* Skip elements, which are less or greater than pivot values. |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1754 |
*/ |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1755 |
while (a[++less] < pivot1); |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1756 |
while (a[--great] > pivot2); |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1757 |
|
4170
a94a6faf44e6
6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents:
diff
changeset
|
1758 |
/* |
4356 | 1759 |
* Partitioning: |
1760 |
* |
|
5995
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1761 |
* left part center part right part |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1762 |
* +--------------------------------------------------------------+ |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1763 |
* | < pivot1 | pivot1 <= && <= pivot2 | ? | > pivot2 | |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1764 |
* +--------------------------------------------------------------+ |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1765 |
* ^ ^ ^ |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1766 |
* | | | |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1767 |
* less k great |
4356 | 1768 |
* |
4170
a94a6faf44e6
6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents:
diff
changeset
|
1769 |
* Invariants: |
4356 | 1770 |
* |
4170
a94a6faf44e6
6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents:
diff
changeset
|
1771 |
* all in (left, less) < pivot1 |
a94a6faf44e6
6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents:
diff
changeset
|
1772 |
* pivot1 <= all in [less, k) <= pivot2 |
a94a6faf44e6
6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents:
diff
changeset
|
1773 |
* all in (great, right) > pivot2 |
a94a6faf44e6
6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents:
diff
changeset
|
1774 |
* |
5995
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1775 |
* Pointer k is the first index of ?-part. |
4170
a94a6faf44e6
6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents:
diff
changeset
|
1776 |
*/ |
4241 | 1777 |
outer: |
6896
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
1778 |
for (int k = less - 1; ++k <= great; ) { |
4170
a94a6faf44e6
6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents:
diff
changeset
|
1779 |
char ak = a[k]; |
4356 | 1780 |
if (ak < pivot1) { // Move a[k] to left part |
5995
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1781 |
a[k] = a[less]; |
6896
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
1782 |
/* |
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
1783 |
* Here and below we use "a[i] = b; i++;" instead |
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
1784 |
* of "a[i++] = b;" due to performance issue. |
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
1785 |
*/ |
5995
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1786 |
a[less] = ak; |
8188
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
1787 |
++less; |
4356 | 1788 |
} else if (ak > pivot2) { // Move a[k] to right part |
4241 | 1789 |
while (a[great] > pivot2) { |
4356 | 1790 |
if (great-- == k) { |
4241 | 1791 |
break outer; |
1792 |
} |
|
4170
a94a6faf44e6
6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents:
diff
changeset
|
1793 |
} |
6896
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
1794 |
if (a[great] < pivot1) { // a[great] <= pivot2 |
4170
a94a6faf44e6
6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents:
diff
changeset
|
1795 |
a[k] = a[less]; |
5995
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1796 |
a[less] = a[great]; |
8188
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
1797 |
++less; |
4356 | 1798 |
} else { // pivot1 <= a[great] <= pivot2 |
1799 |
a[k] = a[great]; |
|
5995
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1800 |
} |
6896
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
1801 |
/* |
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
1802 |
* Here and below we use "a[i] = b; i--;" instead |
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
1803 |
* of "a[i--] = b;" due to performance issue. |
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
1804 |
*/ |
5995
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1805 |
a[great] = ak; |
8188
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
1806 |
--great; |
5995
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1807 |
} |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1808 |
} |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1809 |
|
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1810 |
// Swap pivots into their final positions |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1811 |
a[left] = a[less - 1]; a[less - 1] = pivot1; |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1812 |
a[right] = a[great + 1]; a[great + 1] = pivot2; |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1813 |
|
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1814 |
// Sort left and right parts recursively, excluding known pivots |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1815 |
sort(a, left, less - 2, leftmost); |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1816 |
sort(a, great + 2, right, false); |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1817 |
|
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1818 |
/* |
6896
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
1819 |
* If center part is too large (comprises > 4/7 of the array), |
5995
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1820 |
* swap internal pivot values to ends. |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1821 |
*/ |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1822 |
if (less < e1 && e5 < great) { |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1823 |
/* |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1824 |
* Skip elements, which are equal to pivot values. |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1825 |
*/ |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1826 |
while (a[less] == pivot1) { |
8188
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
1827 |
++less; |
5995
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1828 |
} |
8188
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
1829 |
|
5995
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1830 |
while (a[great] == pivot2) { |
8188
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
1831 |
--great; |
5995
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1832 |
} |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1833 |
|
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1834 |
/* |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1835 |
* Partitioning: |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1836 |
* |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1837 |
* left part center part right part |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1838 |
* +----------------------------------------------------------+ |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1839 |
* | == pivot1 | pivot1 < && < pivot2 | ? | == pivot2 | |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1840 |
* +----------------------------------------------------------+ |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1841 |
* ^ ^ ^ |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1842 |
* | | | |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1843 |
* less k great |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1844 |
* |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1845 |
* Invariants: |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1846 |
* |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1847 |
* all in (*, less) == pivot1 |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1848 |
* pivot1 < all in [less, k) < pivot2 |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1849 |
* all in (great, *) == pivot2 |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1850 |
* |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1851 |
* Pointer k is the first index of ?-part. |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1852 |
*/ |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1853 |
outer: |
6896
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
1854 |
for (int k = less - 1; ++k <= great; ) { |
5995
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1855 |
char ak = a[k]; |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1856 |
if (ak == pivot1) { // Move a[k] to left part |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1857 |
a[k] = a[less]; |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1858 |
a[less] = ak; |
8188
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
1859 |
++less; |
5995
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1860 |
} else if (ak == pivot2) { // Move a[k] to right part |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1861 |
while (a[great] == pivot2) { |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1862 |
if (great-- == k) { |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1863 |
break outer; |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1864 |
} |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1865 |
} |
6896
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
1866 |
if (a[great] == pivot1) { // a[great] < pivot2 |
5995
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1867 |
a[k] = a[less]; |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1868 |
/* |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1869 |
* Even though a[great] equals to pivot1, the |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1870 |
* assignment a[less] = pivot1 may be incorrect, |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1871 |
* if a[great] and pivot1 are floating-point zeros |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1872 |
* of different signs. Therefore in float and |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1873 |
* double sorting methods we have to use more |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1874 |
* accurate assignment a[less] = a[great]. |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1875 |
*/ |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1876 |
a[less] = pivot1; |
8188
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
1877 |
++less; |
5995
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1878 |
} else { // pivot1 < a[great] < pivot2 |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1879 |
a[k] = a[great]; |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1880 |
} |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1881 |
a[great] = ak; |
8188
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
1882 |
--great; |
4170
a94a6faf44e6
6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents:
diff
changeset
|
1883 |
} |
a94a6faf44e6
6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents:
diff
changeset
|
1884 |
} |
a94a6faf44e6
6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents:
diff
changeset
|
1885 |
} |
5995
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1886 |
|
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1887 |
// Sort center part recursively |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1888 |
sort(a, less, great, false); |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1889 |
|
8188
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
1890 |
} else { // Partitioning with one pivot |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
1891 |
/* |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
1892 |
* Use the third of the five sorted elements as pivot. |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
1893 |
* This value is inexpensive approximation of the median. |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
1894 |
*/ |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
1895 |
char pivot = a[e3]; |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
1896 |
|
4170
a94a6faf44e6
6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents:
diff
changeset
|
1897 |
/* |
6896
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
1898 |
* Partitioning degenerates to the traditional 3-way |
5995
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1899 |
* (or "Dutch National Flag") schema: |
4170
a94a6faf44e6
6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents:
diff
changeset
|
1900 |
* |
5995
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1901 |
* left part center part right part |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1902 |
* +-------------------------------------------------+ |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1903 |
* | < pivot | == pivot | ? | > pivot | |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1904 |
* +-------------------------------------------------+ |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1905 |
* ^ ^ ^ |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1906 |
* | | | |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1907 |
* less k great |
4170
a94a6faf44e6
6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents:
diff
changeset
|
1908 |
* |
a94a6faf44e6
6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents:
diff
changeset
|
1909 |
* Invariants: |
a94a6faf44e6
6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents:
diff
changeset
|
1910 |
* |
a94a6faf44e6
6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents:
diff
changeset
|
1911 |
* all in (left, less) < pivot |
a94a6faf44e6
6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents:
diff
changeset
|
1912 |
* all in [less, k) == pivot |
a94a6faf44e6
6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents:
diff
changeset
|
1913 |
* all in (great, right) > pivot |
a94a6faf44e6
6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents:
diff
changeset
|
1914 |
* |
5995
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1915 |
* Pointer k is the first index of ?-part. |
4170
a94a6faf44e6
6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents:
diff
changeset
|
1916 |
*/ |
6896
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
1917 |
for (int k = less; k <= great; ++k) { |
8188
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
1918 |
if (a[k] == pivot) { |
4241 | 1919 |
continue; |
4170
a94a6faf44e6
6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents:
diff
changeset
|
1920 |
} |
5995
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1921 |
char ak = a[k]; |
8188
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
1922 |
if (ak < pivot) { // Move a[k] to left part |
5995
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1923 |
a[k] = a[less]; |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1924 |
a[less] = ak; |
8188
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
1925 |
++less; |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
1926 |
} else { // a[k] > pivot - Move a[k] to right part |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
1927 |
while (a[great] > pivot) { |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
1928 |
--great; |
4170
a94a6faf44e6
6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents:
diff
changeset
|
1929 |
} |
8188
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
1930 |
if (a[great] < pivot) { // a[great] <= pivot |
4170
a94a6faf44e6
6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents:
diff
changeset
|
1931 |
a[k] = a[less]; |
5995
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1932 |
a[less] = a[great]; |
8188
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
1933 |
++less; |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
1934 |
} else { // a[great] == pivot |
5995
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1935 |
/* |
8188
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
1936 |
* Even though a[great] equals to pivot, the |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
1937 |
* assignment a[k] = pivot may be incorrect, |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
1938 |
* if a[great] and pivot are floating-point |
5995
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1939 |
* zeros of different signs. Therefore in float |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1940 |
* and double sorting methods we have to use |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1941 |
* more accurate assignment a[k] = a[great]. |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1942 |
*/ |
8188
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
1943 |
a[k] = pivot; |
4170
a94a6faf44e6
6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents:
diff
changeset
|
1944 |
} |
5995
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1945 |
a[great] = ak; |
8188
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
1946 |
--great; |
4170
a94a6faf44e6
6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents:
diff
changeset
|
1947 |
} |
a94a6faf44e6
6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents:
diff
changeset
|
1948 |
} |
4356 | 1949 |
|
6896
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
1950 |
/* |
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
1951 |
* Sort left and right parts recursively. |
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
1952 |
* All elements from center part are equal |
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
1953 |
* and, therefore, already sorted. |
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
1954 |
*/ |
5995
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1955 |
sort(a, left, less - 1, leftmost); |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1956 |
sort(a, great + 1, right, false); |
4170
a94a6faf44e6
6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents:
diff
changeset
|
1957 |
} |
4233 | 1958 |
} |
1959 |
||
1960 |
/** The number of distinct byte values. */ |
|
1961 |
private static final int NUM_BYTE_VALUES = 1 << 8; |
|
1962 |
||
1963 |
/** |
|
6896
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
1964 |
* Sorts the specified range of the array. |
4170
a94a6faf44e6
6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents:
diff
changeset
|
1965 |
* |
a94a6faf44e6
6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents:
diff
changeset
|
1966 |
* @param a the array to be sorted |
4233 | 1967 |
* @param left the index of the first element, inclusive, to be sorted |
1968 |
* @param right the index of the last element, inclusive, to be sorted |
|
4170
a94a6faf44e6
6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents:
diff
changeset
|
1969 |
*/ |
17712
b56c69500af6
8014076: Arrays parallel and serial sorting improvements
dl
parents:
9035
diff
changeset
|
1970 |
static void sort(byte[] a, int left, int right) { |
6896
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
1971 |
// Use counting sort on large arrays |
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
1972 |
if (right - left > COUNTING_SORT_THRESHOLD_FOR_BYTE) { |
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
1973 |
int[] count = new int[NUM_BYTE_VALUES]; |
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
1974 |
|
8188
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
1975 |
for (int i = left - 1; ++i <= right; |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
1976 |
count[a[i] - Byte.MIN_VALUE]++ |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
1977 |
); |
6896
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
1978 |
for (int i = NUM_BYTE_VALUES, k = right + 1; k > left; ) { |
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
1979 |
while (count[--i] == 0); |
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
1980 |
byte value = (byte) (i + Byte.MIN_VALUE); |
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
1981 |
int s = count[i]; |
5995
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1982 |
|
6896
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
1983 |
do { |
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
1984 |
a[--k] = value; |
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
1985 |
} while (--s > 0); |
5995
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1986 |
} |
6896
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
1987 |
} else { // Use insertion sort on small arrays |
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
1988 |
for (int i = left, j = i; i < right; j = ++i) { |
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
1989 |
byte ai = a[i + 1]; |
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
1990 |
while (ai < a[j]) { |
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
1991 |
a[j + 1] = a[j]; |
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
1992 |
if (j-- == left) { |
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
1993 |
break; |
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
1994 |
} |
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
1995 |
} |
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
1996 |
a[j + 1] = ai; |
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
1997 |
} |
5995
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1998 |
} |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
1999 |
} |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2000 |
|
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2001 |
/** |
17712
b56c69500af6
8014076: Arrays parallel and serial sorting improvements
dl
parents:
9035
diff
changeset
|
2002 |
* Sorts the specified range of the array using the given |
b56c69500af6
8014076: Arrays parallel and serial sorting improvements
dl
parents:
9035
diff
changeset
|
2003 |
* workspace array slice if possible for merging |
4233 | 2004 |
* |
2005 |
* @param a the array to be sorted |
|
2006 |
* @param left the index of the first element, inclusive, to be sorted |
|
2007 |
* @param right the index of the last element, inclusive, to be sorted |
|
17712
b56c69500af6
8014076: Arrays parallel and serial sorting improvements
dl
parents:
9035
diff
changeset
|
2008 |
* @param work a workspace array (slice) |
b56c69500af6
8014076: Arrays parallel and serial sorting improvements
dl
parents:
9035
diff
changeset
|
2009 |
* @param workBase origin of usable space in work array |
b56c69500af6
8014076: Arrays parallel and serial sorting improvements
dl
parents:
9035
diff
changeset
|
2010 |
* @param workLen usable size of work array |
4233 | 2011 |
*/ |
17712
b56c69500af6
8014076: Arrays parallel and serial sorting improvements
dl
parents:
9035
diff
changeset
|
2012 |
static void sort(float[] a, int left, int right, |
b56c69500af6
8014076: Arrays parallel and serial sorting improvements
dl
parents:
9035
diff
changeset
|
2013 |
float[] work, int workBase, int workLen) { |
4233 | 2014 |
/* |
5995
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2015 |
* Phase 1: Move NaNs to the end of the array. |
4233 | 2016 |
*/ |
5995
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2017 |
while (left <= right && Float.isNaN(a[right])) { |
8188
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
2018 |
--right; |
5995
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2019 |
} |
6896
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
2020 |
for (int k = right; --k >= left; ) { |
4233 | 2021 |
float ak = a[k]; |
5995
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2022 |
if (ak != ak) { // a[k] is NaN |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2023 |
a[k] = a[right]; |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2024 |
a[right] = ak; |
8188
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
2025 |
--right; |
4233 | 2026 |
} |
2027 |
} |
|
2028 |
||
2029 |
/* |
|
5995
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2030 |
* Phase 2: Sort everything except NaNs (which are already in place). |
4233 | 2031 |
*/ |
17712
b56c69500af6
8014076: Arrays parallel and serial sorting improvements
dl
parents:
9035
diff
changeset
|
2032 |
doSort(a, left, right, work, workBase, workLen); |
4233 | 2033 |
|
2034 |
/* |
|
5995
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2035 |
* Phase 3: Place negative zeros before positive zeros. |
4233 | 2036 |
*/ |
5995
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2037 |
int hi = right; |
4233 | 2038 |
|
5995
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2039 |
/* |
6896
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
2040 |
* Find the first zero, or first positive, or last negative element. |
5995
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2041 |
*/ |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2042 |
while (left < hi) { |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2043 |
int middle = (left + hi) >>> 1; |
4233 | 2044 |
float middleValue = a[middle]; |
2045 |
||
2046 |
if (middleValue < 0.0f) { |
|
5995
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2047 |
left = middle + 1; |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2048 |
} else { |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2049 |
hi = middle; |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2050 |
} |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2051 |
} |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2052 |
|
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2053 |
/* |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2054 |
* Skip the last negative value (if any) or all leading negative zeros. |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2055 |
*/ |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2056 |
while (left <= right && Float.floatToRawIntBits(a[left]) < 0) { |
8188
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
2057 |
++left; |
5995
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2058 |
} |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2059 |
|
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2060 |
/* |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2061 |
* Move negative zeros to the beginning of the sub-range. |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2062 |
* |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2063 |
* Partitioning: |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2064 |
* |
6896
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
2065 |
* +----------------------------------------------------+ |
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
2066 |
* | < 0.0 | -0.0 | 0.0 | ? ( >= 0.0 ) | |
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
2067 |
* +----------------------------------------------------+ |
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
2068 |
* ^ ^ ^ |
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
2069 |
* | | | |
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
2070 |
* left p k |
5995
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2071 |
* |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2072 |
* Invariants: |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2073 |
* |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2074 |
* all in (*, left) < 0.0 |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2075 |
* all in [left, p) == -0.0 |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2076 |
* all in [p, k) == 0.0 |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2077 |
* all in [k, right] >= 0.0 |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2078 |
* |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2079 |
* Pointer k is the first index of ?-part. |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2080 |
*/ |
6896
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
2081 |
for (int k = left, p = left - 1; ++k <= right; ) { |
5995
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2082 |
float ak = a[k]; |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2083 |
if (ak != 0.0f) { |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2084 |
break; |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2085 |
} |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2086 |
if (Float.floatToRawIntBits(ak) < 0) { // ak is -0.0f |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2087 |
a[k] = 0.0f; |
6896
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
2088 |
a[++p] = -0.0f; |
4233 | 2089 |
} |
2090 |
} |
|
2091 |
} |
|
2092 |
||
2093 |
/** |
|
8188
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
2094 |
* Sorts the specified range of the array. |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
2095 |
* |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
2096 |
* @param a the array to be sorted |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
2097 |
* @param left the index of the first element, inclusive, to be sorted |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
2098 |
* @param right the index of the last element, inclusive, to be sorted |
17712
b56c69500af6
8014076: Arrays parallel and serial sorting improvements
dl
parents:
9035
diff
changeset
|
2099 |
* @param work a workspace array (slice) |
b56c69500af6
8014076: Arrays parallel and serial sorting improvements
dl
parents:
9035
diff
changeset
|
2100 |
* @param workBase origin of usable space in work array |
b56c69500af6
8014076: Arrays parallel and serial sorting improvements
dl
parents:
9035
diff
changeset
|
2101 |
* @param workLen usable size of work array |
8188
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
2102 |
*/ |
17712
b56c69500af6
8014076: Arrays parallel and serial sorting improvements
dl
parents:
9035
diff
changeset
|
2103 |
private static void doSort(float[] a, int left, int right, |
b56c69500af6
8014076: Arrays parallel and serial sorting improvements
dl
parents:
9035
diff
changeset
|
2104 |
float[] work, int workBase, int workLen) { |
8188
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
2105 |
// Use Quicksort on small arrays |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
2106 |
if (right - left < QUICKSORT_THRESHOLD) { |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
2107 |
sort(a, left, right, true); |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
2108 |
return; |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
2109 |
} |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
2110 |
|
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
2111 |
/* |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
2112 |
* Index run[i] is the start of i-th run |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
2113 |
* (ascending or descending sequence). |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
2114 |
*/ |
8193
626b859aabb2
7018258: Dual-pivot updates in 7013585 can fail with ArrayIndexOutOfBoundsException
alanb
parents:
8188
diff
changeset
|
2115 |
int[] run = new int[MAX_RUN_COUNT + 1]; |
8188
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
2116 |
int count = 0; run[0] = left; |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
2117 |
|
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
2118 |
// Check if the array is nearly sorted |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
2119 |
for (int k = left; k < right; run[count] = k) { |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
2120 |
if (a[k] < a[k + 1]) { // ascending |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
2121 |
while (++k <= right && a[k - 1] <= a[k]); |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
2122 |
} else if (a[k] > a[k + 1]) { // descending |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
2123 |
while (++k <= right && a[k - 1] >= a[k]); |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
2124 |
for (int lo = run[count] - 1, hi = k; ++lo < --hi; ) { |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
2125 |
float t = a[lo]; a[lo] = a[hi]; a[hi] = t; |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
2126 |
} |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
2127 |
} else { // equal |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
2128 |
for (int m = MAX_RUN_LENGTH; ++k <= right && a[k - 1] == a[k]; ) { |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
2129 |
if (--m == 0) { |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
2130 |
sort(a, left, right, true); |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
2131 |
return; |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
2132 |
} |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
2133 |
} |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
2134 |
} |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
2135 |
|
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
2136 |
/* |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
2137 |
* The array is not highly structured, |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
2138 |
* use Quicksort instead of merge sort. |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
2139 |
*/ |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
2140 |
if (++count == MAX_RUN_COUNT) { |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
2141 |
sort(a, left, right, true); |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
2142 |
return; |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
2143 |
} |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
2144 |
} |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
2145 |
|
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
2146 |
// Check special cases |
17712
b56c69500af6
8014076: Arrays parallel and serial sorting improvements
dl
parents:
9035
diff
changeset
|
2147 |
// Implementation note: variable "right" is increased by 1. |
8188
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
2148 |
if (run[count] == right++) { // The last run contains one element |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
2149 |
run[++count] = right; |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
2150 |
} else if (count == 1) { // The array is already sorted |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
2151 |
return; |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
2152 |
} |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
2153 |
|
17712
b56c69500af6
8014076: Arrays parallel and serial sorting improvements
dl
parents:
9035
diff
changeset
|
2154 |
// Determine alternation base for merge |
b56c69500af6
8014076: Arrays parallel and serial sorting improvements
dl
parents:
9035
diff
changeset
|
2155 |
byte odd = 0; |
8188
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
2156 |
for (int n = 1; (n <<= 1) < count; odd ^= 1); |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
2157 |
|
17712
b56c69500af6
8014076: Arrays parallel and serial sorting improvements
dl
parents:
9035
diff
changeset
|
2158 |
// Use or create temporary array b for merging |
b56c69500af6
8014076: Arrays parallel and serial sorting improvements
dl
parents:
9035
diff
changeset
|
2159 |
float[] b; // temp array; alternates with a |
b56c69500af6
8014076: Arrays parallel and serial sorting improvements
dl
parents:
9035
diff
changeset
|
2160 |
int ao, bo; // array offsets from 'left' |
b56c69500af6
8014076: Arrays parallel and serial sorting improvements
dl
parents:
9035
diff
changeset
|
2161 |
int blen = right - left; // space needed for b |
b56c69500af6
8014076: Arrays parallel and serial sorting improvements
dl
parents:
9035
diff
changeset
|
2162 |
if (work == null || workLen < blen || workBase + blen > work.length) { |
b56c69500af6
8014076: Arrays parallel and serial sorting improvements
dl
parents:
9035
diff
changeset
|
2163 |
work = new float[blen]; |
b56c69500af6
8014076: Arrays parallel and serial sorting improvements
dl
parents:
9035
diff
changeset
|
2164 |
workBase = 0; |
b56c69500af6
8014076: Arrays parallel and serial sorting improvements
dl
parents:
9035
diff
changeset
|
2165 |
} |
8188
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
2166 |
if (odd == 0) { |
17712
b56c69500af6
8014076: Arrays parallel and serial sorting improvements
dl
parents:
9035
diff
changeset
|
2167 |
System.arraycopy(a, left, work, workBase, blen); |
b56c69500af6
8014076: Arrays parallel and serial sorting improvements
dl
parents:
9035
diff
changeset
|
2168 |
b = a; |
b56c69500af6
8014076: Arrays parallel and serial sorting improvements
dl
parents:
9035
diff
changeset
|
2169 |
bo = 0; |
b56c69500af6
8014076: Arrays parallel and serial sorting improvements
dl
parents:
9035
diff
changeset
|
2170 |
a = work; |
b56c69500af6
8014076: Arrays parallel and serial sorting improvements
dl
parents:
9035
diff
changeset
|
2171 |
ao = workBase - left; |
8188
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
2172 |
} else { |
17712
b56c69500af6
8014076: Arrays parallel and serial sorting improvements
dl
parents:
9035
diff
changeset
|
2173 |
b = work; |
b56c69500af6
8014076: Arrays parallel and serial sorting improvements
dl
parents:
9035
diff
changeset
|
2174 |
ao = 0; |
b56c69500af6
8014076: Arrays parallel and serial sorting improvements
dl
parents:
9035
diff
changeset
|
2175 |
bo = workBase - left; |
8188
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
2176 |
} |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
2177 |
|
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
2178 |
// Merging |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
2179 |
for (int last; count > 1; count = last) { |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
2180 |
for (int k = (last = 0) + 2; k <= count; k += 2) { |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
2181 |
int hi = run[k], mi = run[k - 1]; |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
2182 |
for (int i = run[k - 2], p = i, q = mi; i < hi; ++i) { |
17712
b56c69500af6
8014076: Arrays parallel and serial sorting improvements
dl
parents:
9035
diff
changeset
|
2183 |
if (q >= hi || p < mi && a[p + ao] <= a[q + ao]) { |
b56c69500af6
8014076: Arrays parallel and serial sorting improvements
dl
parents:
9035
diff
changeset
|
2184 |
b[i + bo] = a[p++ + ao]; |
8188
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
2185 |
} else { |
17712
b56c69500af6
8014076: Arrays parallel and serial sorting improvements
dl
parents:
9035
diff
changeset
|
2186 |
b[i + bo] = a[q++ + ao]; |
8188
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
2187 |
} |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
2188 |
} |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
2189 |
run[++last] = hi; |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
2190 |
} |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
2191 |
if ((count & 1) != 0) { |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
2192 |
for (int i = right, lo = run[count - 1]; --i >= lo; |
17712
b56c69500af6
8014076: Arrays parallel and serial sorting improvements
dl
parents:
9035
diff
changeset
|
2193 |
b[i + bo] = a[i + ao] |
8188
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
2194 |
); |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
2195 |
run[++last] = right; |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
2196 |
} |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
2197 |
float[] t = a; a = b; b = t; |
17712
b56c69500af6
8014076: Arrays parallel and serial sorting improvements
dl
parents:
9035
diff
changeset
|
2198 |
int o = ao; ao = bo; bo = o; |
8188
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
2199 |
} |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
2200 |
} |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
2201 |
|
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
2202 |
/** |
6896
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
2203 |
* Sorts the specified range of the array by Dual-Pivot Quicksort. |
4170
a94a6faf44e6
6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents:
diff
changeset
|
2204 |
* |
a94a6faf44e6
6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents:
diff
changeset
|
2205 |
* @param a the array to be sorted |
4233 | 2206 |
* @param left the index of the first element, inclusive, to be sorted |
2207 |
* @param right the index of the last element, inclusive, to be sorted |
|
6896
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
2208 |
* @param leftmost indicates if this part is the leftmost in the range |
4170
a94a6faf44e6
6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents:
diff
changeset
|
2209 |
*/ |
8188
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
2210 |
private static void sort(float[] a, int left, int right, boolean leftmost) { |
5995
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2211 |
int length = right - left + 1; |
4170
a94a6faf44e6
6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents:
diff
changeset
|
2212 |
|
8188
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
2213 |
// Use insertion sort on tiny arrays |
5995
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2214 |
if (length < INSERTION_SORT_THRESHOLD) { |
6896
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
2215 |
if (leftmost) { |
5995
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2216 |
/* |
6896
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
2217 |
* Traditional (without sentinel) insertion sort, |
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
2218 |
* optimized for server VM, is used in case of |
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
2219 |
* the leftmost part. |
5995
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2220 |
*/ |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2221 |
for (int i = left, j = i; i < right; j = ++i) { |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2222 |
float ai = a[i + 1]; |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2223 |
while (ai < a[j]) { |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2224 |
a[j + 1] = a[j]; |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2225 |
if (j-- == left) { |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2226 |
break; |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2227 |
} |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2228 |
} |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2229 |
a[j + 1] = ai; |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2230 |
} |
6896
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
2231 |
} else { |
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
2232 |
/* |
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
2233 |
* Skip the longest ascending sequence. |
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
2234 |
*/ |
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
2235 |
do { |
8188
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
2236 |
if (left >= right) { |
6896
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
2237 |
return; |
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
2238 |
} |
8188
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
2239 |
} while (a[++left] >= a[left - 1]); |
6896
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
2240 |
|
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
2241 |
/* |
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
2242 |
* Every element from adjoining part plays the role |
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
2243 |
* of sentinel, therefore this allows us to avoid the |
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
2244 |
* left range check on each iteration. Moreover, we use |
8188
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
2245 |
* the more optimized algorithm, so called pair insertion |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
2246 |
* sort, which is faster (in the context of Quicksort) |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
2247 |
* than traditional implementation of insertion sort. |
6896
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
2248 |
*/ |
8188
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
2249 |
for (int k = left; ++left <= right; k = ++left) { |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
2250 |
float a1 = a[k], a2 = a[left]; |
6896
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
2251 |
|
8188
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
2252 |
if (a1 < a2) { |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
2253 |
a2 = a1; a1 = a[left]; |
6896
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
2254 |
} |
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
2255 |
while (a1 < a[--k]) { |
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
2256 |
a[k + 2] = a[k]; |
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
2257 |
} |
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
2258 |
a[++k + 1] = a1; |
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
2259 |
|
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
2260 |
while (a2 < a[--k]) { |
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
2261 |
a[k + 1] = a[k]; |
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
2262 |
} |
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
2263 |
a[k + 1] = a2; |
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
2264 |
} |
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
2265 |
float last = a[right]; |
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
2266 |
|
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
2267 |
while (last < a[--right]) { |
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
2268 |
a[right + 1] = a[right]; |
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
2269 |
} |
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
2270 |
a[right + 1] = last; |
5995
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2271 |
} |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2272 |
return; |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2273 |
} |
4241 | 2274 |
|
5995
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2275 |
// Inexpensive approximation of length / 7 |
6896
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
2276 |
int seventh = (length >> 3) + (length >> 6) + 1; |
5995
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2277 |
|
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2278 |
/* |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2279 |
* Sort five evenly spaced elements around (and including) the |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2280 |
* center element in the range. These elements will be used for |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2281 |
* pivot selection as described below. The choice for spacing |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2282 |
* these elements was empirically determined to work well on |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2283 |
* a wide variety of inputs. |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2284 |
*/ |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2285 |
int e3 = (left + right) >>> 1; // The midpoint |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2286 |
int e2 = e3 - seventh; |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2287 |
int e1 = e2 - seventh; |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2288 |
int e4 = e3 + seventh; |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2289 |
int e5 = e4 + seventh; |
4241 | 2290 |
|
5995
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2291 |
// Sort these elements using insertion sort |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2292 |
if (a[e2] < a[e1]) { float t = a[e2]; a[e2] = a[e1]; a[e1] = t; } |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2293 |
|
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2294 |
if (a[e3] < a[e2]) { float t = a[e3]; a[e3] = a[e2]; a[e2] = t; |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2295 |
if (t < a[e1]) { a[e2] = a[e1]; a[e1] = t; } |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2296 |
} |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2297 |
if (a[e4] < a[e3]) { float t = a[e4]; a[e4] = a[e3]; a[e3] = t; |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2298 |
if (t < a[e2]) { a[e3] = a[e2]; a[e2] = t; |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2299 |
if (t < a[e1]) { a[e2] = a[e1]; a[e1] = t; } |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2300 |
} |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2301 |
} |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2302 |
if (a[e5] < a[e4]) { float t = a[e5]; a[e5] = a[e4]; a[e4] = t; |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2303 |
if (t < a[e3]) { a[e4] = a[e3]; a[e3] = t; |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2304 |
if (t < a[e2]) { a[e3] = a[e2]; a[e2] = t; |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2305 |
if (t < a[e1]) { a[e2] = a[e1]; a[e1] = t; } |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2306 |
} |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2307 |
} |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2308 |
} |
4170
a94a6faf44e6
6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents:
diff
changeset
|
2309 |
|
a94a6faf44e6
6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents:
diff
changeset
|
2310 |
// Pointers |
5995
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2311 |
int less = left; // The index of the first element of center part |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2312 |
int great = right; // The index before the first element of right part |
4170
a94a6faf44e6
6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents:
diff
changeset
|
2313 |
|
8188
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
2314 |
if (a[e1] != a[e2] && a[e2] != a[e3] && a[e3] != a[e4] && a[e4] != a[e5]) { |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
2315 |
/* |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
2316 |
* Use the second and fourth of the five sorted elements as pivots. |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
2317 |
* These values are inexpensive approximations of the first and |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
2318 |
* second terciles of the array. Note that pivot1 <= pivot2. |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
2319 |
*/ |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
2320 |
float pivot1 = a[e2]; |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
2321 |
float pivot2 = a[e4]; |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
2322 |
|
5995
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2323 |
/* |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2324 |
* The first and the last elements to be sorted are moved to the |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2325 |
* locations formerly occupied by the pivots. When partitioning |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2326 |
* is complete, the pivots are swapped back into their final |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2327 |
* positions, and excluded from subsequent sorting. |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2328 |
*/ |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2329 |
a[e2] = a[left]; |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2330 |
a[e4] = a[right]; |
4170
a94a6faf44e6
6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents:
diff
changeset
|
2331 |
|
5995
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2332 |
/* |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2333 |
* Skip elements, which are less or greater than pivot values. |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2334 |
*/ |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2335 |
while (a[++less] < pivot1); |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2336 |
while (a[--great] > pivot2); |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2337 |
|
4170
a94a6faf44e6
6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents:
diff
changeset
|
2338 |
/* |
4356 | 2339 |
* Partitioning: |
2340 |
* |
|
5995
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2341 |
* left part center part right part |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2342 |
* +--------------------------------------------------------------+ |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2343 |
* | < pivot1 | pivot1 <= && <= pivot2 | ? | > pivot2 | |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2344 |
* +--------------------------------------------------------------+ |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2345 |
* ^ ^ ^ |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2346 |
* | | | |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2347 |
* less k great |
4356 | 2348 |
* |
4170
a94a6faf44e6
6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents:
diff
changeset
|
2349 |
* Invariants: |
4356 | 2350 |
* |
4170
a94a6faf44e6
6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents:
diff
changeset
|
2351 |
* all in (left, less) < pivot1 |
a94a6faf44e6
6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents:
diff
changeset
|
2352 |
* pivot1 <= all in [less, k) <= pivot2 |
a94a6faf44e6
6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents:
diff
changeset
|
2353 |
* all in (great, right) > pivot2 |
a94a6faf44e6
6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents:
diff
changeset
|
2354 |
* |
5995
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2355 |
* Pointer k is the first index of ?-part. |
4170
a94a6faf44e6
6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents:
diff
changeset
|
2356 |
*/ |
4241 | 2357 |
outer: |
6896
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
2358 |
for (int k = less - 1; ++k <= great; ) { |
4170
a94a6faf44e6
6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents:
diff
changeset
|
2359 |
float ak = a[k]; |
4356 | 2360 |
if (ak < pivot1) { // Move a[k] to left part |
5995
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2361 |
a[k] = a[less]; |
6896
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
2362 |
/* |
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
2363 |
* Here and below we use "a[i] = b; i++;" instead |
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
2364 |
* of "a[i++] = b;" due to performance issue. |
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
2365 |
*/ |
5995
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2366 |
a[less] = ak; |
8188
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
2367 |
++less; |
4356 | 2368 |
} else if (ak > pivot2) { // Move a[k] to right part |
4241 | 2369 |
while (a[great] > pivot2) { |
4356 | 2370 |
if (great-- == k) { |
4241 | 2371 |
break outer; |
2372 |
} |
|
4170
a94a6faf44e6
6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents:
diff
changeset
|
2373 |
} |
6896
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
2374 |
if (a[great] < pivot1) { // a[great] <= pivot2 |
4170
a94a6faf44e6
6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents:
diff
changeset
|
2375 |
a[k] = a[less]; |
5995
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2376 |
a[less] = a[great]; |
8188
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
2377 |
++less; |
4356 | 2378 |
} else { // pivot1 <= a[great] <= pivot2 |
2379 |
a[k] = a[great]; |
|
5995
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2380 |
} |
6896
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
2381 |
/* |
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
2382 |
* Here and below we use "a[i] = b; i--;" instead |
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
2383 |
* of "a[i--] = b;" due to performance issue. |
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
2384 |
*/ |
5995
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2385 |
a[great] = ak; |
8188
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
2386 |
--great; |
5995
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2387 |
} |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2388 |
} |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2389 |
|
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2390 |
// Swap pivots into their final positions |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2391 |
a[left] = a[less - 1]; a[less - 1] = pivot1; |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2392 |
a[right] = a[great + 1]; a[great + 1] = pivot2; |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2393 |
|
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2394 |
// Sort left and right parts recursively, excluding known pivots |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2395 |
sort(a, left, less - 2, leftmost); |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2396 |
sort(a, great + 2, right, false); |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2397 |
|
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2398 |
/* |
6896
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
2399 |
* If center part is too large (comprises > 4/7 of the array), |
5995
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2400 |
* swap internal pivot values to ends. |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2401 |
*/ |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2402 |
if (less < e1 && e5 < great) { |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2403 |
/* |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2404 |
* Skip elements, which are equal to pivot values. |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2405 |
*/ |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2406 |
while (a[less] == pivot1) { |
8188
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
2407 |
++less; |
5995
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2408 |
} |
8188
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
2409 |
|
5995
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2410 |
while (a[great] == pivot2) { |
8188
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
2411 |
--great; |
5995
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2412 |
} |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2413 |
|
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2414 |
/* |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2415 |
* Partitioning: |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2416 |
* |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2417 |
* left part center part right part |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2418 |
* +----------------------------------------------------------+ |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2419 |
* | == pivot1 | pivot1 < && < pivot2 | ? | == pivot2 | |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2420 |
* +----------------------------------------------------------+ |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2421 |
* ^ ^ ^ |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2422 |
* | | | |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2423 |
* less k great |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2424 |
* |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2425 |
* Invariants: |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2426 |
* |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2427 |
* all in (*, less) == pivot1 |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2428 |
* pivot1 < all in [less, k) < pivot2 |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2429 |
* all in (great, *) == pivot2 |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2430 |
* |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2431 |
* Pointer k is the first index of ?-part. |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2432 |
*/ |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2433 |
outer: |
6896
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
2434 |
for (int k = less - 1; ++k <= great; ) { |
5995
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2435 |
float ak = a[k]; |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2436 |
if (ak == pivot1) { // Move a[k] to left part |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2437 |
a[k] = a[less]; |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2438 |
a[less] = ak; |
8188
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
2439 |
++less; |
5995
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2440 |
} else if (ak == pivot2) { // Move a[k] to right part |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2441 |
while (a[great] == pivot2) { |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2442 |
if (great-- == k) { |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2443 |
break outer; |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2444 |
} |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2445 |
} |
6896
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
2446 |
if (a[great] == pivot1) { // a[great] < pivot2 |
5995
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2447 |
a[k] = a[less]; |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2448 |
/* |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2449 |
* Even though a[great] equals to pivot1, the |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2450 |
* assignment a[less] = pivot1 may be incorrect, |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2451 |
* if a[great] and pivot1 are floating-point zeros |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2452 |
* of different signs. Therefore in float and |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2453 |
* double sorting methods we have to use more |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2454 |
* accurate assignment a[less] = a[great]. |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2455 |
*/ |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2456 |
a[less] = a[great]; |
8188
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
2457 |
++less; |
5995
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2458 |
} else { // pivot1 < a[great] < pivot2 |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2459 |
a[k] = a[great]; |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2460 |
} |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2461 |
a[great] = ak; |
8188
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
2462 |
--great; |
4170
a94a6faf44e6
6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents:
diff
changeset
|
2463 |
} |
a94a6faf44e6
6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents:
diff
changeset
|
2464 |
} |
a94a6faf44e6
6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents:
diff
changeset
|
2465 |
} |
5995
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2466 |
|
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2467 |
// Sort center part recursively |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2468 |
sort(a, less, great, false); |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2469 |
|
8188
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
2470 |
} else { // Partitioning with one pivot |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
2471 |
/* |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
2472 |
* Use the third of the five sorted elements as pivot. |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
2473 |
* This value is inexpensive approximation of the median. |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
2474 |
*/ |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
2475 |
float pivot = a[e3]; |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
2476 |
|
4170
a94a6faf44e6
6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents:
diff
changeset
|
2477 |
/* |
6896
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
2478 |
* Partitioning degenerates to the traditional 3-way |
5995
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2479 |
* (or "Dutch National Flag") schema: |
4170
a94a6faf44e6
6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents:
diff
changeset
|
2480 |
* |
5995
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2481 |
* left part center part right part |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2482 |
* +-------------------------------------------------+ |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2483 |
* | < pivot | == pivot | ? | > pivot | |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2484 |
* +-------------------------------------------------+ |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2485 |
* ^ ^ ^ |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2486 |
* | | | |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2487 |
* less k great |
4170
a94a6faf44e6
6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents:
diff
changeset
|
2488 |
* |
a94a6faf44e6
6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents:
diff
changeset
|
2489 |
* Invariants: |
a94a6faf44e6
6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents:
diff
changeset
|
2490 |
* |
a94a6faf44e6
6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents:
diff
changeset
|
2491 |
* all in (left, less) < pivot |
a94a6faf44e6
6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents:
diff
changeset
|
2492 |
* all in [less, k) == pivot |
a94a6faf44e6
6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents:
diff
changeset
|
2493 |
* all in (great, right) > pivot |
a94a6faf44e6
6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents:
diff
changeset
|
2494 |
* |
5995
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2495 |
* Pointer k is the first index of ?-part. |
4170
a94a6faf44e6
6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents:
diff
changeset
|
2496 |
*/ |
6896
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
2497 |
for (int k = less; k <= great; ++k) { |
8188
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
2498 |
if (a[k] == pivot) { |
4241 | 2499 |
continue; |
4170
a94a6faf44e6
6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents:
diff
changeset
|
2500 |
} |
5995
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2501 |
float ak = a[k]; |
8188
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
2502 |
if (ak < pivot) { // Move a[k] to left part |
5995
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2503 |
a[k] = a[less]; |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2504 |
a[less] = ak; |
8188
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
2505 |
++less; |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
2506 |
} else { // a[k] > pivot - Move a[k] to right part |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
2507 |
while (a[great] > pivot) { |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
2508 |
--great; |
4170
a94a6faf44e6
6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents:
diff
changeset
|
2509 |
} |
8188
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
2510 |
if (a[great] < pivot) { // a[great] <= pivot |
4170
a94a6faf44e6
6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents:
diff
changeset
|
2511 |
a[k] = a[less]; |
5995
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2512 |
a[less] = a[great]; |
8188
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
2513 |
++less; |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
2514 |
} else { // a[great] == pivot |
5995
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2515 |
/* |
8188
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
2516 |
* Even though a[great] equals to pivot, the |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
2517 |
* assignment a[k] = pivot may be incorrect, |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
2518 |
* if a[great] and pivot are floating-point |
5995
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2519 |
* zeros of different signs. Therefore in float |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2520 |
* and double sorting methods we have to use |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2521 |
* more accurate assignment a[k] = a[great]. |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2522 |
*/ |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2523 |
a[k] = a[great]; |
4170
a94a6faf44e6
6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents:
diff
changeset
|
2524 |
} |
5995
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2525 |
a[great] = ak; |
8188
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
2526 |
--great; |
4170
a94a6faf44e6
6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents:
diff
changeset
|
2527 |
} |
a94a6faf44e6
6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents:
diff
changeset
|
2528 |
} |
4356 | 2529 |
|
6896
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
2530 |
/* |
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
2531 |
* Sort left and right parts recursively. |
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
2532 |
* All elements from center part are equal |
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
2533 |
* and, therefore, already sorted. |
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
2534 |
*/ |
5995
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2535 |
sort(a, left, less - 1, leftmost); |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2536 |
sort(a, great + 1, right, false); |
4170
a94a6faf44e6
6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents:
diff
changeset
|
2537 |
} |
4233 | 2538 |
} |
2539 |
||
2540 |
/** |
|
17712
b56c69500af6
8014076: Arrays parallel and serial sorting improvements
dl
parents:
9035
diff
changeset
|
2541 |
* Sorts the specified range of the array using the given |
b56c69500af6
8014076: Arrays parallel and serial sorting improvements
dl
parents:
9035
diff
changeset
|
2542 |
* workspace array slice if possible for merging |
4170
a94a6faf44e6
6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents:
diff
changeset
|
2543 |
* |
a94a6faf44e6
6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents:
diff
changeset
|
2544 |
* @param a the array to be sorted |
4233 | 2545 |
* @param left the index of the first element, inclusive, to be sorted |
2546 |
* @param right the index of the last element, inclusive, to be sorted |
|
17712
b56c69500af6
8014076: Arrays parallel and serial sorting improvements
dl
parents:
9035
diff
changeset
|
2547 |
* @param work a workspace array (slice) |
b56c69500af6
8014076: Arrays parallel and serial sorting improvements
dl
parents:
9035
diff
changeset
|
2548 |
* @param workBase origin of usable space in work array |
b56c69500af6
8014076: Arrays parallel and serial sorting improvements
dl
parents:
9035
diff
changeset
|
2549 |
* @param workLen usable size of work array |
4170
a94a6faf44e6
6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents:
diff
changeset
|
2550 |
*/ |
17712
b56c69500af6
8014076: Arrays parallel and serial sorting improvements
dl
parents:
9035
diff
changeset
|
2551 |
static void sort(double[] a, int left, int right, |
b56c69500af6
8014076: Arrays parallel and serial sorting improvements
dl
parents:
9035
diff
changeset
|
2552 |
double[] work, int workBase, int workLen) { |
4233 | 2553 |
/* |
5995
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2554 |
* Phase 1: Move NaNs to the end of the array. |
4233 | 2555 |
*/ |
5995
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2556 |
while (left <= right && Double.isNaN(a[right])) { |
8188
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
2557 |
--right; |
5995
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2558 |
} |
6896
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
2559 |
for (int k = right; --k >= left; ) { |
4233 | 2560 |
double ak = a[k]; |
5995
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2561 |
if (ak != ak) { // a[k] is NaN |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2562 |
a[k] = a[right]; |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2563 |
a[right] = ak; |
8188
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
2564 |
--right; |
4233 | 2565 |
} |
2566 |
} |
|
2567 |
||
2568 |
/* |
|
5995
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2569 |
* Phase 2: Sort everything except NaNs (which are already in place). |
4233 | 2570 |
*/ |
17712
b56c69500af6
8014076: Arrays parallel and serial sorting improvements
dl
parents:
9035
diff
changeset
|
2571 |
doSort(a, left, right, work, workBase, workLen); |
4233 | 2572 |
|
2573 |
/* |
|
5995
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2574 |
* Phase 3: Place negative zeros before positive zeros. |
4233 | 2575 |
*/ |
5995
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2576 |
int hi = right; |
4233 | 2577 |
|
5995
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2578 |
/* |
6896
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
2579 |
* Find the first zero, or first positive, or last negative element. |
5995
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2580 |
*/ |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2581 |
while (left < hi) { |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2582 |
int middle = (left + hi) >>> 1; |
4233 | 2583 |
double middleValue = a[middle]; |
2584 |
||
2585 |
if (middleValue < 0.0d) { |
|
5995
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2586 |
left = middle + 1; |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2587 |
} else { |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2588 |
hi = middle; |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2589 |
} |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2590 |
} |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2591 |
|
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2592 |
/* |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2593 |
* Skip the last negative value (if any) or all leading negative zeros. |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2594 |
*/ |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2595 |
while (left <= right && Double.doubleToRawLongBits(a[left]) < 0) { |
8188
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
2596 |
++left; |
5995
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2597 |
} |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2598 |
|
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2599 |
/* |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2600 |
* Move negative zeros to the beginning of the sub-range. |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2601 |
* |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2602 |
* Partitioning: |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2603 |
* |
6896
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
2604 |
* +----------------------------------------------------+ |
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
2605 |
* | < 0.0 | -0.0 | 0.0 | ? ( >= 0.0 ) | |
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
2606 |
* +----------------------------------------------------+ |
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
2607 |
* ^ ^ ^ |
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
2608 |
* | | | |
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
2609 |
* left p k |
5995
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2610 |
* |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2611 |
* Invariants: |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2612 |
* |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2613 |
* all in (*, left) < 0.0 |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2614 |
* all in [left, p) == -0.0 |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2615 |
* all in [p, k) == 0.0 |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2616 |
* all in [k, right] >= 0.0 |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2617 |
* |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2618 |
* Pointer k is the first index of ?-part. |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2619 |
*/ |
6896
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
2620 |
for (int k = left, p = left - 1; ++k <= right; ) { |
5995
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2621 |
double ak = a[k]; |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2622 |
if (ak != 0.0d) { |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2623 |
break; |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2624 |
} |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2625 |
if (Double.doubleToRawLongBits(ak) < 0) { // ak is -0.0d |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2626 |
a[k] = 0.0d; |
6896
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
2627 |
a[++p] = -0.0d; |
4233 | 2628 |
} |
2629 |
} |
|
2630 |
} |
|
2631 |
||
2632 |
/** |
|
8188
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
2633 |
* Sorts the specified range of the array. |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
2634 |
* |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
2635 |
* @param a the array to be sorted |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
2636 |
* @param left the index of the first element, inclusive, to be sorted |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
2637 |
* @param right the index of the last element, inclusive, to be sorted |
17712
b56c69500af6
8014076: Arrays parallel and serial sorting improvements
dl
parents:
9035
diff
changeset
|
2638 |
* @param work a workspace array (slice) |
b56c69500af6
8014076: Arrays parallel and serial sorting improvements
dl
parents:
9035
diff
changeset
|
2639 |
* @param workBase origin of usable space in work array |
b56c69500af6
8014076: Arrays parallel and serial sorting improvements
dl
parents:
9035
diff
changeset
|
2640 |
* @param workLen usable size of work array |
8188
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
2641 |
*/ |
17712
b56c69500af6
8014076: Arrays parallel and serial sorting improvements
dl
parents:
9035
diff
changeset
|
2642 |
private static void doSort(double[] a, int left, int right, |
b56c69500af6
8014076: Arrays parallel and serial sorting improvements
dl
parents:
9035
diff
changeset
|
2643 |
double[] work, int workBase, int workLen) { |
8188
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
2644 |
// Use Quicksort on small arrays |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
2645 |
if (right - left < QUICKSORT_THRESHOLD) { |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
2646 |
sort(a, left, right, true); |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
2647 |
return; |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
2648 |
} |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
2649 |
|
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
2650 |
/* |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
2651 |
* Index run[i] is the start of i-th run |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
2652 |
* (ascending or descending sequence). |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
2653 |
*/ |
8193
626b859aabb2
7018258: Dual-pivot updates in 7013585 can fail with ArrayIndexOutOfBoundsException
alanb
parents:
8188
diff
changeset
|
2654 |
int[] run = new int[MAX_RUN_COUNT + 1]; |
8188
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
2655 |
int count = 0; run[0] = left; |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
2656 |
|
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
2657 |
// Check if the array is nearly sorted |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
2658 |
for (int k = left; k < right; run[count] = k) { |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
2659 |
if (a[k] < a[k + 1]) { // ascending |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
2660 |
while (++k <= right && a[k - 1] <= a[k]); |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
2661 |
} else if (a[k] > a[k + 1]) { // descending |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
2662 |
while (++k <= right && a[k - 1] >= a[k]); |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
2663 |
for (int lo = run[count] - 1, hi = k; ++lo < --hi; ) { |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
2664 |
double t = a[lo]; a[lo] = a[hi]; a[hi] = t; |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
2665 |
} |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
2666 |
} else { // equal |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
2667 |
for (int m = MAX_RUN_LENGTH; ++k <= right && a[k - 1] == a[k]; ) { |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
2668 |
if (--m == 0) { |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
2669 |
sort(a, left, right, true); |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
2670 |
return; |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
2671 |
} |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
2672 |
} |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
2673 |
} |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
2674 |
|
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
2675 |
/* |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
2676 |
* The array is not highly structured, |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
2677 |
* use Quicksort instead of merge sort. |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
2678 |
*/ |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
2679 |
if (++count == MAX_RUN_COUNT) { |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
2680 |
sort(a, left, right, true); |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
2681 |
return; |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
2682 |
} |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
2683 |
} |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
2684 |
|
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
2685 |
// Check special cases |
17712
b56c69500af6
8014076: Arrays parallel and serial sorting improvements
dl
parents:
9035
diff
changeset
|
2686 |
// Implementation note: variable "right" is increased by 1. |
8188
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
2687 |
if (run[count] == right++) { // The last run contains one element |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
2688 |
run[++count] = right; |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
2689 |
} else if (count == 1) { // The array is already sorted |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
2690 |
return; |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
2691 |
} |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
2692 |
|
17712
b56c69500af6
8014076: Arrays parallel and serial sorting improvements
dl
parents:
9035
diff
changeset
|
2693 |
// Determine alternation base for merge |
b56c69500af6
8014076: Arrays parallel and serial sorting improvements
dl
parents:
9035
diff
changeset
|
2694 |
byte odd = 0; |
8188
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
2695 |
for (int n = 1; (n <<= 1) < count; odd ^= 1); |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
2696 |
|
17712
b56c69500af6
8014076: Arrays parallel and serial sorting improvements
dl
parents:
9035
diff
changeset
|
2697 |
// Use or create temporary array b for merging |
b56c69500af6
8014076: Arrays parallel and serial sorting improvements
dl
parents:
9035
diff
changeset
|
2698 |
double[] b; // temp array; alternates with a |
b56c69500af6
8014076: Arrays parallel and serial sorting improvements
dl
parents:
9035
diff
changeset
|
2699 |
int ao, bo; // array offsets from 'left' |
b56c69500af6
8014076: Arrays parallel and serial sorting improvements
dl
parents:
9035
diff
changeset
|
2700 |
int blen = right - left; // space needed for b |
b56c69500af6
8014076: Arrays parallel and serial sorting improvements
dl
parents:
9035
diff
changeset
|
2701 |
if (work == null || workLen < blen || workBase + blen > work.length) { |
b56c69500af6
8014076: Arrays parallel and serial sorting improvements
dl
parents:
9035
diff
changeset
|
2702 |
work = new double[blen]; |
b56c69500af6
8014076: Arrays parallel and serial sorting improvements
dl
parents:
9035
diff
changeset
|
2703 |
workBase = 0; |
b56c69500af6
8014076: Arrays parallel and serial sorting improvements
dl
parents:
9035
diff
changeset
|
2704 |
} |
8188
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
2705 |
if (odd == 0) { |
17712
b56c69500af6
8014076: Arrays parallel and serial sorting improvements
dl
parents:
9035
diff
changeset
|
2706 |
System.arraycopy(a, left, work, workBase, blen); |
b56c69500af6
8014076: Arrays parallel and serial sorting improvements
dl
parents:
9035
diff
changeset
|
2707 |
b = a; |
b56c69500af6
8014076: Arrays parallel and serial sorting improvements
dl
parents:
9035
diff
changeset
|
2708 |
bo = 0; |
b56c69500af6
8014076: Arrays parallel and serial sorting improvements
dl
parents:
9035
diff
changeset
|
2709 |
a = work; |
b56c69500af6
8014076: Arrays parallel and serial sorting improvements
dl
parents:
9035
diff
changeset
|
2710 |
ao = workBase - left; |
8188
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
2711 |
} else { |
17712
b56c69500af6
8014076: Arrays parallel and serial sorting improvements
dl
parents:
9035
diff
changeset
|
2712 |
b = work; |
b56c69500af6
8014076: Arrays parallel and serial sorting improvements
dl
parents:
9035
diff
changeset
|
2713 |
ao = 0; |
b56c69500af6
8014076: Arrays parallel and serial sorting improvements
dl
parents:
9035
diff
changeset
|
2714 |
bo = workBase - left; |
8188
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
2715 |
} |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
2716 |
|
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
2717 |
// Merging |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
2718 |
for (int last; count > 1; count = last) { |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
2719 |
for (int k = (last = 0) + 2; k <= count; k += 2) { |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
2720 |
int hi = run[k], mi = run[k - 1]; |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
2721 |
for (int i = run[k - 2], p = i, q = mi; i < hi; ++i) { |
17712
b56c69500af6
8014076: Arrays parallel and serial sorting improvements
dl
parents:
9035
diff
changeset
|
2722 |
if (q >= hi || p < mi && a[p + ao] <= a[q + ao]) { |
b56c69500af6
8014076: Arrays parallel and serial sorting improvements
dl
parents:
9035
diff
changeset
|
2723 |
b[i + bo] = a[p++ + ao]; |
8188
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
2724 |
} else { |
17712
b56c69500af6
8014076: Arrays parallel and serial sorting improvements
dl
parents:
9035
diff
changeset
|
2725 |
b[i + bo] = a[q++ + ao]; |
8188
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
2726 |
} |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
2727 |
} |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
2728 |
run[++last] = hi; |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
2729 |
} |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
2730 |
if ((count & 1) != 0) { |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
2731 |
for (int i = right, lo = run[count - 1]; --i >= lo; |
17712
b56c69500af6
8014076: Arrays parallel and serial sorting improvements
dl
parents:
9035
diff
changeset
|
2732 |
b[i + bo] = a[i + ao] |
8188
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
2733 |
); |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
2734 |
run[++last] = right; |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
2735 |
} |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
2736 |
double[] t = a; a = b; b = t; |
17712
b56c69500af6
8014076: Arrays parallel and serial sorting improvements
dl
parents:
9035
diff
changeset
|
2737 |
int o = ao; ao = bo; bo = o; |
8188
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
2738 |
} |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
2739 |
} |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
2740 |
|
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
2741 |
/** |
6896
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
2742 |
* Sorts the specified range of the array by Dual-Pivot Quicksort. |
4170
a94a6faf44e6
6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents:
diff
changeset
|
2743 |
* |
a94a6faf44e6
6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents:
diff
changeset
|
2744 |
* @param a the array to be sorted |
4233 | 2745 |
* @param left the index of the first element, inclusive, to be sorted |
2746 |
* @param right the index of the last element, inclusive, to be sorted |
|
6896
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
2747 |
* @param leftmost indicates if this part is the leftmost in the range |
4170
a94a6faf44e6
6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents:
diff
changeset
|
2748 |
*/ |
8188
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
2749 |
private static void sort(double[] a, int left, int right, boolean leftmost) { |
5995
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2750 |
int length = right - left + 1; |
4170
a94a6faf44e6
6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents:
diff
changeset
|
2751 |
|
8188
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
2752 |
// Use insertion sort on tiny arrays |
5995
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2753 |
if (length < INSERTION_SORT_THRESHOLD) { |
6896
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
2754 |
if (leftmost) { |
5995
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2755 |
/* |
6896
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
2756 |
* Traditional (without sentinel) insertion sort, |
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
2757 |
* optimized for server VM, is used in case of |
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
2758 |
* the leftmost part. |
5995
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2759 |
*/ |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2760 |
for (int i = left, j = i; i < right; j = ++i) { |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2761 |
double ai = a[i + 1]; |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2762 |
while (ai < a[j]) { |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2763 |
a[j + 1] = a[j]; |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2764 |
if (j-- == left) { |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2765 |
break; |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2766 |
} |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2767 |
} |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2768 |
a[j + 1] = ai; |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2769 |
} |
6896
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
2770 |
} else { |
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
2771 |
/* |
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
2772 |
* Skip the longest ascending sequence. |
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
2773 |
*/ |
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
2774 |
do { |
8188
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
2775 |
if (left >= right) { |
6896
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
2776 |
return; |
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
2777 |
} |
8188
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
2778 |
} while (a[++left] >= a[left - 1]); |
6896
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
2779 |
|
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
2780 |
/* |
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
2781 |
* Every element from adjoining part plays the role |
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
2782 |
* of sentinel, therefore this allows us to avoid the |
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
2783 |
* left range check on each iteration. Moreover, we use |
8188
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
2784 |
* the more optimized algorithm, so called pair insertion |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
2785 |
* sort, which is faster (in the context of Quicksort) |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
2786 |
* than traditional implementation of insertion sort. |
6896
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
2787 |
*/ |
8188
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
2788 |
for (int k = left; ++left <= right; k = ++left) { |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
2789 |
double a1 = a[k], a2 = a[left]; |
6896
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
2790 |
|
8188
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
2791 |
if (a1 < a2) { |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
2792 |
a2 = a1; a1 = a[left]; |
6896
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
2793 |
} |
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
2794 |
while (a1 < a[--k]) { |
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
2795 |
a[k + 2] = a[k]; |
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
2796 |
} |
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
2797 |
a[++k + 1] = a1; |
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
2798 |
|
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
2799 |
while (a2 < a[--k]) { |
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
2800 |
a[k + 1] = a[k]; |
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
2801 |
} |
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
2802 |
a[k + 1] = a2; |
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
2803 |
} |
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
2804 |
double last = a[right]; |
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
2805 |
|
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
2806 |
while (last < a[--right]) { |
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
2807 |
a[right + 1] = a[right]; |
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
2808 |
} |
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
2809 |
a[right + 1] = last; |
5995
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2810 |
} |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2811 |
return; |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2812 |
} |
4241 | 2813 |
|
5995
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2814 |
// Inexpensive approximation of length / 7 |
6896
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
2815 |
int seventh = (length >> 3) + (length >> 6) + 1; |
5995
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2816 |
|
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2817 |
/* |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2818 |
* Sort five evenly spaced elements around (and including) the |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2819 |
* center element in the range. These elements will be used for |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2820 |
* pivot selection as described below. The choice for spacing |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2821 |
* these elements was empirically determined to work well on |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2822 |
* a wide variety of inputs. |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2823 |
*/ |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2824 |
int e3 = (left + right) >>> 1; // The midpoint |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2825 |
int e2 = e3 - seventh; |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2826 |
int e1 = e2 - seventh; |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2827 |
int e4 = e3 + seventh; |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2828 |
int e5 = e4 + seventh; |
4241 | 2829 |
|
5995
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2830 |
// Sort these elements using insertion sort |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2831 |
if (a[e2] < a[e1]) { double t = a[e2]; a[e2] = a[e1]; a[e1] = t; } |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2832 |
|
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2833 |
if (a[e3] < a[e2]) { double t = a[e3]; a[e3] = a[e2]; a[e2] = t; |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2834 |
if (t < a[e1]) { a[e2] = a[e1]; a[e1] = t; } |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2835 |
} |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2836 |
if (a[e4] < a[e3]) { double t = a[e4]; a[e4] = a[e3]; a[e3] = t; |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2837 |
if (t < a[e2]) { a[e3] = a[e2]; a[e2] = t; |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2838 |
if (t < a[e1]) { a[e2] = a[e1]; a[e1] = t; } |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2839 |
} |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2840 |
} |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2841 |
if (a[e5] < a[e4]) { double t = a[e5]; a[e5] = a[e4]; a[e4] = t; |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2842 |
if (t < a[e3]) { a[e4] = a[e3]; a[e3] = t; |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2843 |
if (t < a[e2]) { a[e3] = a[e2]; a[e2] = t; |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2844 |
if (t < a[e1]) { a[e2] = a[e1]; a[e1] = t; } |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2845 |
} |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2846 |
} |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2847 |
} |
4170
a94a6faf44e6
6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents:
diff
changeset
|
2848 |
|
a94a6faf44e6
6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents:
diff
changeset
|
2849 |
// Pointers |
5995
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2850 |
int less = left; // The index of the first element of center part |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2851 |
int great = right; // The index before the first element of right part |
4170
a94a6faf44e6
6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents:
diff
changeset
|
2852 |
|
8188
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
2853 |
if (a[e1] != a[e2] && a[e2] != a[e3] && a[e3] != a[e4] && a[e4] != a[e5]) { |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
2854 |
/* |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
2855 |
* Use the second and fourth of the five sorted elements as pivots. |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
2856 |
* These values are inexpensive approximations of the first and |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
2857 |
* second terciles of the array. Note that pivot1 <= pivot2. |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
2858 |
*/ |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
2859 |
double pivot1 = a[e2]; |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
2860 |
double pivot2 = a[e4]; |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
2861 |
|
5995
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2862 |
/* |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2863 |
* The first and the last elements to be sorted are moved to the |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2864 |
* locations formerly occupied by the pivots. When partitioning |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2865 |
* is complete, the pivots are swapped back into their final |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2866 |
* positions, and excluded from subsequent sorting. |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2867 |
*/ |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2868 |
a[e2] = a[left]; |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2869 |
a[e4] = a[right]; |
4170
a94a6faf44e6
6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents:
diff
changeset
|
2870 |
|
5995
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2871 |
/* |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2872 |
* Skip elements, which are less or greater than pivot values. |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2873 |
*/ |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2874 |
while (a[++less] < pivot1); |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2875 |
while (a[--great] > pivot2); |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2876 |
|
4170
a94a6faf44e6
6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents:
diff
changeset
|
2877 |
/* |
4356 | 2878 |
* Partitioning: |
2879 |
* |
|
5995
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2880 |
* left part center part right part |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2881 |
* +--------------------------------------------------------------+ |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2882 |
* | < pivot1 | pivot1 <= && <= pivot2 | ? | > pivot2 | |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2883 |
* +--------------------------------------------------------------+ |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2884 |
* ^ ^ ^ |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2885 |
* | | | |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2886 |
* less k great |
4356 | 2887 |
* |
4170
a94a6faf44e6
6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents:
diff
changeset
|
2888 |
* Invariants: |
4356 | 2889 |
* |
4170
a94a6faf44e6
6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents:
diff
changeset
|
2890 |
* all in (left, less) < pivot1 |
a94a6faf44e6
6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents:
diff
changeset
|
2891 |
* pivot1 <= all in [less, k) <= pivot2 |
a94a6faf44e6
6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents:
diff
changeset
|
2892 |
* all in (great, right) > pivot2 |
a94a6faf44e6
6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents:
diff
changeset
|
2893 |
* |
5995
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2894 |
* Pointer k is the first index of ?-part. |
4170
a94a6faf44e6
6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents:
diff
changeset
|
2895 |
*/ |
4241 | 2896 |
outer: |
6896
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
2897 |
for (int k = less - 1; ++k <= great; ) { |
4170
a94a6faf44e6
6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents:
diff
changeset
|
2898 |
double ak = a[k]; |
4356 | 2899 |
if (ak < pivot1) { // Move a[k] to left part |
5995
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2900 |
a[k] = a[less]; |
6896
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
2901 |
/* |
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
2902 |
* Here and below we use "a[i] = b; i++;" instead |
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
2903 |
* of "a[i++] = b;" due to performance issue. |
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
2904 |
*/ |
5995
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2905 |
a[less] = ak; |
8188
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
2906 |
++less; |
4356 | 2907 |
} else if (ak > pivot2) { // Move a[k] to right part |
4241 | 2908 |
while (a[great] > pivot2) { |
4356 | 2909 |
if (great-- == k) { |
4241 | 2910 |
break outer; |
2911 |
} |
|
4170
a94a6faf44e6
6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents:
diff
changeset
|
2912 |
} |
6896
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
2913 |
if (a[great] < pivot1) { // a[great] <= pivot2 |
4170
a94a6faf44e6
6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents:
diff
changeset
|
2914 |
a[k] = a[less]; |
5995
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2915 |
a[less] = a[great]; |
8188
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
2916 |
++less; |
4356 | 2917 |
} else { // pivot1 <= a[great] <= pivot2 |
2918 |
a[k] = a[great]; |
|
5995
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2919 |
} |
6896
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
2920 |
/* |
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
2921 |
* Here and below we use "a[i] = b; i--;" instead |
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
2922 |
* of "a[i--] = b;" due to performance issue. |
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
2923 |
*/ |
5995
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2924 |
a[great] = ak; |
8188
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
2925 |
--great; |
5995
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2926 |
} |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2927 |
} |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2928 |
|
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2929 |
// Swap pivots into their final positions |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2930 |
a[left] = a[less - 1]; a[less - 1] = pivot1; |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2931 |
a[right] = a[great + 1]; a[great + 1] = pivot2; |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2932 |
|
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2933 |
// Sort left and right parts recursively, excluding known pivots |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2934 |
sort(a, left, less - 2, leftmost); |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2935 |
sort(a, great + 2, right, false); |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2936 |
|
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2937 |
/* |
6896
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
2938 |
* If center part is too large (comprises > 4/7 of the array), |
5995
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2939 |
* swap internal pivot values to ends. |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2940 |
*/ |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2941 |
if (less < e1 && e5 < great) { |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2942 |
/* |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2943 |
* Skip elements, which are equal to pivot values. |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2944 |
*/ |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2945 |
while (a[less] == pivot1) { |
8188
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
2946 |
++less; |
5995
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2947 |
} |
8188
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
2948 |
|
5995
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2949 |
while (a[great] == pivot2) { |
8188
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
2950 |
--great; |
5995
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2951 |
} |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2952 |
|
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2953 |
/* |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2954 |
* Partitioning: |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2955 |
* |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2956 |
* left part center part right part |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2957 |
* +----------------------------------------------------------+ |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2958 |
* | == pivot1 | pivot1 < && < pivot2 | ? | == pivot2 | |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2959 |
* +----------------------------------------------------------+ |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2960 |
* ^ ^ ^ |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2961 |
* | | | |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2962 |
* less k great |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2963 |
* |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2964 |
* Invariants: |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2965 |
* |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2966 |
* all in (*, less) == pivot1 |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2967 |
* pivot1 < all in [less, k) < pivot2 |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2968 |
* all in (great, *) == pivot2 |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2969 |
* |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2970 |
* Pointer k is the first index of ?-part. |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2971 |
*/ |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2972 |
outer: |
6896
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
2973 |
for (int k = less - 1; ++k <= great; ) { |
5995
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2974 |
double ak = a[k]; |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2975 |
if (ak == pivot1) { // Move a[k] to left part |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2976 |
a[k] = a[less]; |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2977 |
a[less] = ak; |
8188
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
2978 |
++less; |
5995
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2979 |
} else if (ak == pivot2) { // Move a[k] to right part |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2980 |
while (a[great] == pivot2) { |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2981 |
if (great-- == k) { |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2982 |
break outer; |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2983 |
} |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2984 |
} |
6896
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
2985 |
if (a[great] == pivot1) { // a[great] < pivot2 |
5995
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2986 |
a[k] = a[less]; |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2987 |
/* |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2988 |
* Even though a[great] equals to pivot1, the |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2989 |
* assignment a[less] = pivot1 may be incorrect, |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2990 |
* if a[great] and pivot1 are floating-point zeros |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2991 |
* of different signs. Therefore in float and |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2992 |
* double sorting methods we have to use more |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2993 |
* accurate assignment a[less] = a[great]. |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2994 |
*/ |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2995 |
a[less] = a[great]; |
8188
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
2996 |
++less; |
5995
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2997 |
} else { // pivot1 < a[great] < pivot2 |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2998 |
a[k] = a[great]; |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
2999 |
} |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
3000 |
a[great] = ak; |
8188
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
3001 |
--great; |
4170
a94a6faf44e6
6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents:
diff
changeset
|
3002 |
} |
a94a6faf44e6
6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents:
diff
changeset
|
3003 |
} |
a94a6faf44e6
6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents:
diff
changeset
|
3004 |
} |
5995
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
3005 |
|
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
3006 |
// Sort center part recursively |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
3007 |
sort(a, less, great, false); |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
3008 |
|
8188
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
3009 |
} else { // Partitioning with one pivot |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
3010 |
/* |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
3011 |
* Use the third of the five sorted elements as pivot. |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
3012 |
* This value is inexpensive approximation of the median. |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
3013 |
*/ |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
3014 |
double pivot = a[e3]; |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
3015 |
|
4170
a94a6faf44e6
6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents:
diff
changeset
|
3016 |
/* |
6896
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
3017 |
* Partitioning degenerates to the traditional 3-way |
5995
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
3018 |
* (or "Dutch National Flag") schema: |
4170
a94a6faf44e6
6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents:
diff
changeset
|
3019 |
* |
5995
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
3020 |
* left part center part right part |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
3021 |
* +-------------------------------------------------+ |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
3022 |
* | < pivot | == pivot | ? | > pivot | |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
3023 |
* +-------------------------------------------------+ |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
3024 |
* ^ ^ ^ |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
3025 |
* | | | |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
3026 |
* less k great |
4170
a94a6faf44e6
6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents:
diff
changeset
|
3027 |
* |
a94a6faf44e6
6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents:
diff
changeset
|
3028 |
* Invariants: |
a94a6faf44e6
6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents:
diff
changeset
|
3029 |
* |
a94a6faf44e6
6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents:
diff
changeset
|
3030 |
* all in (left, less) < pivot |
a94a6faf44e6
6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents:
diff
changeset
|
3031 |
* all in [less, k) == pivot |
a94a6faf44e6
6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents:
diff
changeset
|
3032 |
* all in (great, right) > pivot |
a94a6faf44e6
6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents:
diff
changeset
|
3033 |
* |
5995
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
3034 |
* Pointer k is the first index of ?-part. |
4170
a94a6faf44e6
6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents:
diff
changeset
|
3035 |
*/ |
6896
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
3036 |
for (int k = less; k <= great; ++k) { |
8188
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
3037 |
if (a[k] == pivot) { |
4241 | 3038 |
continue; |
4170
a94a6faf44e6
6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents:
diff
changeset
|
3039 |
} |
5995
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
3040 |
double ak = a[k]; |
8188
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
3041 |
if (ak < pivot) { // Move a[k] to left part |
5995
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
3042 |
a[k] = a[less]; |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
3043 |
a[less] = ak; |
8188
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
3044 |
++less; |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
3045 |
} else { // a[k] > pivot - Move a[k] to right part |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
3046 |
while (a[great] > pivot) { |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
3047 |
--great; |
4170
a94a6faf44e6
6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents:
diff
changeset
|
3048 |
} |
8188
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
3049 |
if (a[great] < pivot) { // a[great] <= pivot |
4170
a94a6faf44e6
6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents:
diff
changeset
|
3050 |
a[k] = a[less]; |
5995
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
3051 |
a[less] = a[great]; |
8188
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
3052 |
++less; |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
3053 |
} else { // a[great] == pivot |
5995
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
3054 |
/* |
8188
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
3055 |
* Even though a[great] equals to pivot, the |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
3056 |
* assignment a[k] = pivot may be incorrect, |
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
3057 |
* if a[great] and pivot are floating-point |
5995
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
3058 |
* zeros of different signs. Therefore in float |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
3059 |
* and double sorting methods we have to use |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
3060 |
* more accurate assignment a[k] = a[great]. |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
3061 |
*/ |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
3062 |
a[k] = a[great]; |
4170
a94a6faf44e6
6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents:
diff
changeset
|
3063 |
} |
5995
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
3064 |
a[great] = ak; |
8188
b90884cf34f5
7013585: Dual-pivot quicksort improvements for highly structured (nearly sorted) and data with small periods
alanb
parents:
6896
diff
changeset
|
3065 |
--great; |
4170
a94a6faf44e6
6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents:
diff
changeset
|
3066 |
} |
a94a6faf44e6
6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents:
diff
changeset
|
3067 |
} |
4356 | 3068 |
|
6896
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
3069 |
/* |
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
3070 |
* Sort left and right parts recursively. |
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
3071 |
* All elements from center part are equal |
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
3072 |
* and, therefore, already sorted. |
d229d56fd918
6976036: Dual-pivot quicksort update (10/2010 tune-up)
alanb
parents:
5995
diff
changeset
|
3073 |
*/ |
5995
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
3074 |
sort(a, left, less - 1, leftmost); |
0b76e67c2054
6947216: Even more Dual-pivot quicksort improvements
alanb
parents:
5506
diff
changeset
|
3075 |
sort(a, great + 1, right, false); |
4170
a94a6faf44e6
6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents:
diff
changeset
|
3076 |
} |
4233 | 3077 |
} |
4170
a94a6faf44e6
6880672: Replace quicksort in java.util.Arrays with dual-pivot implementation
alanb
parents:
diff
changeset
|
3078 |
} |