author | pli |
Tue, 16 Jul 2019 00:57:00 +0000 | |
changeset 55689 | 8c5c9d86e1d6 |
parent 47216 | 71c04702a3d5 |
permissions | -rw-r--r-- |
31079
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
1 |
/* |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
2 |
* Copyright 2015 Goldman Sachs. |
37912
80b046f15b53
8154049: DualPivot sorting calculates incorrect runs for nearly sorted arrays
psandoz
parents:
31079
diff
changeset
|
3 |
* Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved. |
31079
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
4 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
5 |
* |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
6 |
* This code is free software; you can redistribute it and/or modify it |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
7 |
* under the terms of the GNU General Public License version 2 only, as |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
8 |
* published by the Free Software Foundation. |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
9 |
* |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
10 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
11 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
12 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
13 |
* version 2 for more details (a copy is included in the LICENSE file that |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
14 |
* accompanied this code). |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
15 |
* |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
16 |
* You should have received a copy of the GNU General Public License version |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
17 |
* 2 along with this work; if not, write to the Free Software Foundation, |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
18 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
19 |
* |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
20 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
21 |
* or visit www.oracle.com if you need additional information or have any |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
22 |
* questions. |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
23 |
*/ |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
24 |
/* |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
25 |
* @test |
37912
80b046f15b53
8154049: DualPivot sorting calculates incorrect runs for nearly sorted arrays
psandoz
parents:
31079
diff
changeset
|
26 |
* @bug 8154049 |
31079
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
27 |
* @summary Tests the sorting of a large array of sorted primitive values, |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
28 |
* predominently for cases where the array is nearly sorted. This tests |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
29 |
* code that detects patterns in the array to determine if it is nearly |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
30 |
* sorted and if so employs and optimizes merge sort rather than a |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
31 |
* Dual-Pivot QuickSort. |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
32 |
* |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
33 |
* @run testng SortingNearlySortedPrimitive |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
34 |
*/ |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
35 |
|
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
36 |
import org.testng.annotations.DataProvider; |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
37 |
import org.testng.annotations.Test; |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
38 |
|
37912
80b046f15b53
8154049: DualPivot sorting calculates incorrect runs for nearly sorted arrays
psandoz
parents:
31079
diff
changeset
|
39 |
import java.util.ArrayList; |
31079
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
40 |
import java.util.Arrays; |
37912
80b046f15b53
8154049: DualPivot sorting calculates incorrect runs for nearly sorted arrays
psandoz
parents:
31079
diff
changeset
|
41 |
import java.util.List; |
80b046f15b53
8154049: DualPivot sorting calculates incorrect runs for nearly sorted arrays
psandoz
parents:
31079
diff
changeset
|
42 |
import java.util.StringJoiner; |
80b046f15b53
8154049: DualPivot sorting calculates incorrect runs for nearly sorted arrays
psandoz
parents:
31079
diff
changeset
|
43 |
import java.util.function.IntFunction; |
80b046f15b53
8154049: DualPivot sorting calculates incorrect runs for nearly sorted arrays
psandoz
parents:
31079
diff
changeset
|
44 |
import java.util.stream.IntStream; |
80b046f15b53
8154049: DualPivot sorting calculates incorrect runs for nearly sorted arrays
psandoz
parents:
31079
diff
changeset
|
45 |
import java.util.stream.Stream; |
31079
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
46 |
|
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
47 |
public class SortingNearlySortedPrimitive { |
37912
80b046f15b53
8154049: DualPivot sorting calculates incorrect runs for nearly sorted arrays
psandoz
parents:
31079
diff
changeset
|
48 |
|
80b046f15b53
8154049: DualPivot sorting calculates incorrect runs for nearly sorted arrays
psandoz
parents:
31079
diff
changeset
|
49 |
static final int BASE = 3; |
80b046f15b53
8154049: DualPivot sorting calculates incorrect runs for nearly sorted arrays
psandoz
parents:
31079
diff
changeset
|
50 |
static final int WIDTH = 4; |
80b046f15b53
8154049: DualPivot sorting calculates incorrect runs for nearly sorted arrays
psandoz
parents:
31079
diff
changeset
|
51 |
// Should be > DualPivotQuicksort.QUICKSORT_THRESHOLD |
80b046f15b53
8154049: DualPivot sorting calculates incorrect runs for nearly sorted arrays
psandoz
parents:
31079
diff
changeset
|
52 |
static final int PAD = 300; |
80b046f15b53
8154049: DualPivot sorting calculates incorrect runs for nearly sorted arrays
psandoz
parents:
31079
diff
changeset
|
53 |
|
80b046f15b53
8154049: DualPivot sorting calculates incorrect runs for nearly sorted arrays
psandoz
parents:
31079
diff
changeset
|
54 |
Stream<int[]> createCombinations() { |
80b046f15b53
8154049: DualPivot sorting calculates incorrect runs for nearly sorted arrays
psandoz
parents:
31079
diff
changeset
|
55 |
// Create all combinations for the BASE value and double the WIDTH |
80b046f15b53
8154049: DualPivot sorting calculates incorrect runs for nearly sorted arrays
psandoz
parents:
31079
diff
changeset
|
56 |
// elements |
80b046f15b53
8154049: DualPivot sorting calculates incorrect runs for nearly sorted arrays
psandoz
parents:
31079
diff
changeset
|
57 |
// This is create various combinations of ascending, descending and |
80b046f15b53
8154049: DualPivot sorting calculates incorrect runs for nearly sorted arrays
psandoz
parents:
31079
diff
changeset
|
58 |
// equal runs to exercise the nearly sorted code paths |
80b046f15b53
8154049: DualPivot sorting calculates incorrect runs for nearly sorted arrays
psandoz
parents:
31079
diff
changeset
|
59 |
return IntStream.range(0, (int) Math.pow(BASE, 2 * WIDTH)). |
80b046f15b53
8154049: DualPivot sorting calculates incorrect runs for nearly sorted arrays
psandoz
parents:
31079
diff
changeset
|
60 |
mapToObj(this::createArray); |
80b046f15b53
8154049: DualPivot sorting calculates incorrect runs for nearly sorted arrays
psandoz
parents:
31079
diff
changeset
|
61 |
} |
31079
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
62 |
|
37912
80b046f15b53
8154049: DualPivot sorting calculates incorrect runs for nearly sorted arrays
psandoz
parents:
31079
diff
changeset
|
63 |
// Create an array which at either end is filled with -ve and +ve elements |
80b046f15b53
8154049: DualPivot sorting calculates incorrect runs for nearly sorted arrays
psandoz
parents:
31079
diff
changeset
|
64 |
// according to the base value and padded with zeros in between |
80b046f15b53
8154049: DualPivot sorting calculates incorrect runs for nearly sorted arrays
psandoz
parents:
31079
diff
changeset
|
65 |
int[] createArray(int v) { |
80b046f15b53
8154049: DualPivot sorting calculates incorrect runs for nearly sorted arrays
psandoz
parents:
31079
diff
changeset
|
66 |
int[] a = new int[WIDTH + PAD + WIDTH]; |
80b046f15b53
8154049: DualPivot sorting calculates incorrect runs for nearly sorted arrays
psandoz
parents:
31079
diff
changeset
|
67 |
|
80b046f15b53
8154049: DualPivot sorting calculates incorrect runs for nearly sorted arrays
psandoz
parents:
31079
diff
changeset
|
68 |
// Fill head of array |
80b046f15b53
8154049: DualPivot sorting calculates incorrect runs for nearly sorted arrays
psandoz
parents:
31079
diff
changeset
|
69 |
for (int j = 0; j < WIDTH; j++) { |
80b046f15b53
8154049: DualPivot sorting calculates incorrect runs for nearly sorted arrays
psandoz
parents:
31079
diff
changeset
|
70 |
a[j] = (v % BASE) - (BASE / 2); |
80b046f15b53
8154049: DualPivot sorting calculates incorrect runs for nearly sorted arrays
psandoz
parents:
31079
diff
changeset
|
71 |
v /= BASE; |
80b046f15b53
8154049: DualPivot sorting calculates incorrect runs for nearly sorted arrays
psandoz
parents:
31079
diff
changeset
|
72 |
} |
80b046f15b53
8154049: DualPivot sorting calculates incorrect runs for nearly sorted arrays
psandoz
parents:
31079
diff
changeset
|
73 |
// Fill tail of array |
80b046f15b53
8154049: DualPivot sorting calculates incorrect runs for nearly sorted arrays
psandoz
parents:
31079
diff
changeset
|
74 |
for (int j = 0; j < WIDTH; j++) { |
80b046f15b53
8154049: DualPivot sorting calculates incorrect runs for nearly sorted arrays
psandoz
parents:
31079
diff
changeset
|
75 |
a[WIDTH + PAD + j] = (v % BASE) - (BASE / 2); |
80b046f15b53
8154049: DualPivot sorting calculates incorrect runs for nearly sorted arrays
psandoz
parents:
31079
diff
changeset
|
76 |
v /= BASE; |
80b046f15b53
8154049: DualPivot sorting calculates incorrect runs for nearly sorted arrays
psandoz
parents:
31079
diff
changeset
|
77 |
} |
80b046f15b53
8154049: DualPivot sorting calculates incorrect runs for nearly sorted arrays
psandoz
parents:
31079
diff
changeset
|
78 |
return a; |
31079
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
79 |
} |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
80 |
|
37912
80b046f15b53
8154049: DualPivot sorting calculates incorrect runs for nearly sorted arrays
psandoz
parents:
31079
diff
changeset
|
81 |
@Test |
80b046f15b53
8154049: DualPivot sorting calculates incorrect runs for nearly sorted arrays
psandoz
parents:
31079
diff
changeset
|
82 |
public void testCombination() { |
80b046f15b53
8154049: DualPivot sorting calculates incorrect runs for nearly sorted arrays
psandoz
parents:
31079
diff
changeset
|
83 |
createCombinations().forEach(a -> { |
80b046f15b53
8154049: DualPivot sorting calculates incorrect runs for nearly sorted arrays
psandoz
parents:
31079
diff
changeset
|
84 |
try { |
80b046f15b53
8154049: DualPivot sorting calculates incorrect runs for nearly sorted arrays
psandoz
parents:
31079
diff
changeset
|
85 |
// Clone source array to ensure it is not modified |
80b046f15b53
8154049: DualPivot sorting calculates incorrect runs for nearly sorted arrays
psandoz
parents:
31079
diff
changeset
|
86 |
this.sortAndAssert(a.clone()); |
80b046f15b53
8154049: DualPivot sorting calculates incorrect runs for nearly sorted arrays
psandoz
parents:
31079
diff
changeset
|
87 |
this.sortAndAssert(floatCopyFromInt(a)); |
80b046f15b53
8154049: DualPivot sorting calculates incorrect runs for nearly sorted arrays
psandoz
parents:
31079
diff
changeset
|
88 |
this.sortAndAssert(doubleCopyFromInt(a)); |
80b046f15b53
8154049: DualPivot sorting calculates incorrect runs for nearly sorted arrays
psandoz
parents:
31079
diff
changeset
|
89 |
this.sortAndAssert(longCopyFromInt(a)); |
80b046f15b53
8154049: DualPivot sorting calculates incorrect runs for nearly sorted arrays
psandoz
parents:
31079
diff
changeset
|
90 |
this.sortAndAssert(shortCopyFromInt(a)); |
80b046f15b53
8154049: DualPivot sorting calculates incorrect runs for nearly sorted arrays
psandoz
parents:
31079
diff
changeset
|
91 |
this.sortAndAssert(charCopyFromInt(a)); |
80b046f15b53
8154049: DualPivot sorting calculates incorrect runs for nearly sorted arrays
psandoz
parents:
31079
diff
changeset
|
92 |
} catch (AssertionError sae) { |
80b046f15b53
8154049: DualPivot sorting calculates incorrect runs for nearly sorted arrays
psandoz
parents:
31079
diff
changeset
|
93 |
AssertionError ae = new AssertionError("Sort failed for " + arrayToString(a)); |
80b046f15b53
8154049: DualPivot sorting calculates incorrect runs for nearly sorted arrays
psandoz
parents:
31079
diff
changeset
|
94 |
ae.addSuppressed(sae); |
80b046f15b53
8154049: DualPivot sorting calculates incorrect runs for nearly sorted arrays
psandoz
parents:
31079
diff
changeset
|
95 |
throw ae; |
80b046f15b53
8154049: DualPivot sorting calculates incorrect runs for nearly sorted arrays
psandoz
parents:
31079
diff
changeset
|
96 |
} |
80b046f15b53
8154049: DualPivot sorting calculates incorrect runs for nearly sorted arrays
psandoz
parents:
31079
diff
changeset
|
97 |
}); |
80b046f15b53
8154049: DualPivot sorting calculates incorrect runs for nearly sorted arrays
psandoz
parents:
31079
diff
changeset
|
98 |
} |
80b046f15b53
8154049: DualPivot sorting calculates incorrect runs for nearly sorted arrays
psandoz
parents:
31079
diff
changeset
|
99 |
|
80b046f15b53
8154049: DualPivot sorting calculates incorrect runs for nearly sorted arrays
psandoz
parents:
31079
diff
changeset
|
100 |
String arrayToString(int[] a) { |
80b046f15b53
8154049: DualPivot sorting calculates incorrect runs for nearly sorted arrays
psandoz
parents:
31079
diff
changeset
|
101 |
int[] l = Arrays.copyOfRange(a, 0, WIDTH + 2); |
80b046f15b53
8154049: DualPivot sorting calculates incorrect runs for nearly sorted arrays
psandoz
parents:
31079
diff
changeset
|
102 |
int[] r = Arrays.copyOfRange(a, a.length - (WIDTH + 2), a.length); |
80b046f15b53
8154049: DualPivot sorting calculates incorrect runs for nearly sorted arrays
psandoz
parents:
31079
diff
changeset
|
103 |
StringJoiner sj = new StringJoiner(",", "[", "]"); |
80b046f15b53
8154049: DualPivot sorting calculates incorrect runs for nearly sorted arrays
psandoz
parents:
31079
diff
changeset
|
104 |
for (int i : l) { |
80b046f15b53
8154049: DualPivot sorting calculates incorrect runs for nearly sorted arrays
psandoz
parents:
31079
diff
changeset
|
105 |
sj.add(Integer.toString(i)); |
80b046f15b53
8154049: DualPivot sorting calculates incorrect runs for nearly sorted arrays
psandoz
parents:
31079
diff
changeset
|
106 |
} |
80b046f15b53
8154049: DualPivot sorting calculates incorrect runs for nearly sorted arrays
psandoz
parents:
31079
diff
changeset
|
107 |
sj.add("..."); |
80b046f15b53
8154049: DualPivot sorting calculates incorrect runs for nearly sorted arrays
psandoz
parents:
31079
diff
changeset
|
108 |
for (int i : r) { |
80b046f15b53
8154049: DualPivot sorting calculates incorrect runs for nearly sorted arrays
psandoz
parents:
31079
diff
changeset
|
109 |
sj.add(Integer.toString(i)); |
80b046f15b53
8154049: DualPivot sorting calculates incorrect runs for nearly sorted arrays
psandoz
parents:
31079
diff
changeset
|
110 |
} |
80b046f15b53
8154049: DualPivot sorting calculates incorrect runs for nearly sorted arrays
psandoz
parents:
31079
diff
changeset
|
111 |
return sj.toString(); |
80b046f15b53
8154049: DualPivot sorting calculates incorrect runs for nearly sorted arrays
psandoz
parents:
31079
diff
changeset
|
112 |
} |
80b046f15b53
8154049: DualPivot sorting calculates incorrect runs for nearly sorted arrays
psandoz
parents:
31079
diff
changeset
|
113 |
|
80b046f15b53
8154049: DualPivot sorting calculates incorrect runs for nearly sorted arrays
psandoz
parents:
31079
diff
changeset
|
114 |
|
80b046f15b53
8154049: DualPivot sorting calculates incorrect runs for nearly sorted arrays
psandoz
parents:
31079
diff
changeset
|
115 |
@DataProvider(name = "shapes") |
80b046f15b53
8154049: DualPivot sorting calculates incorrect runs for nearly sorted arrays
psandoz
parents:
31079
diff
changeset
|
116 |
public Object[][] createShapes() { |
80b046f15b53
8154049: DualPivot sorting calculates incorrect runs for nearly sorted arrays
psandoz
parents:
31079
diff
changeset
|
117 |
Stream<List<Object>> baseCases = Stream.of( |
80b046f15b53
8154049: DualPivot sorting calculates incorrect runs for nearly sorted arrays
psandoz
parents:
31079
diff
changeset
|
118 |
List.of("hiZeroLowTest", (IntFunction<int[]>) this::hiZeroLowData), |
80b046f15b53
8154049: DualPivot sorting calculates incorrect runs for nearly sorted arrays
psandoz
parents:
31079
diff
changeset
|
119 |
List.of("endLessThanTest", (IntFunction<int[]>) this::endLessThanData), |
80b046f15b53
8154049: DualPivot sorting calculates incorrect runs for nearly sorted arrays
psandoz
parents:
31079
diff
changeset
|
120 |
List.of("highFlatLowTest", (IntFunction<int[]>) this::highFlatLowData), |
80b046f15b53
8154049: DualPivot sorting calculates incorrect runs for nearly sorted arrays
psandoz
parents:
31079
diff
changeset
|
121 |
List.of("identicalTest", (IntFunction<int[]>) this::identicalData), |
80b046f15b53
8154049: DualPivot sorting calculates incorrect runs for nearly sorted arrays
psandoz
parents:
31079
diff
changeset
|
122 |
List.of("sortedReversedSortedTest", (IntFunction<int[]>) this::sortedReversedSortedData), |
80b046f15b53
8154049: DualPivot sorting calculates incorrect runs for nearly sorted arrays
psandoz
parents:
31079
diff
changeset
|
123 |
List.of("pairFlipTest", (IntFunction<int[]>) this::pairFlipData), |
80b046f15b53
8154049: DualPivot sorting calculates incorrect runs for nearly sorted arrays
psandoz
parents:
31079
diff
changeset
|
124 |
List.of("zeroHiTest", (IntFunction<int[]>) this::zeroHiData) |
80b046f15b53
8154049: DualPivot sorting calculates incorrect runs for nearly sorted arrays
psandoz
parents:
31079
diff
changeset
|
125 |
); |
80b046f15b53
8154049: DualPivot sorting calculates incorrect runs for nearly sorted arrays
psandoz
parents:
31079
diff
changeset
|
126 |
|
80b046f15b53
8154049: DualPivot sorting calculates incorrect runs for nearly sorted arrays
psandoz
parents:
31079
diff
changeset
|
127 |
// Ensure the following inequality holds for certain sizes |
80b046f15b53
8154049: DualPivot sorting calculates incorrect runs for nearly sorted arrays
psandoz
parents:
31079
diff
changeset
|
128 |
// DualPivotQuicksort.QUICKSORT_THRESHOLD <= size - 1 |
80b046f15b53
8154049: DualPivot sorting calculates incorrect runs for nearly sorted arrays
psandoz
parents:
31079
diff
changeset
|
129 |
// < DualPivotQuicksort.COUNTING_SORT_THRESHOLD_FOR_SHORT_OR_CHAR |
80b046f15b53
8154049: DualPivot sorting calculates incorrect runs for nearly sorted arrays
psandoz
parents:
31079
diff
changeset
|
130 |
// This guarantees that code paths are taken for checking nearly sorted |
80b046f15b53
8154049: DualPivot sorting calculates incorrect runs for nearly sorted arrays
psandoz
parents:
31079
diff
changeset
|
131 |
// arrays for all primitive types |
80b046f15b53
8154049: DualPivot sorting calculates incorrect runs for nearly sorted arrays
psandoz
parents:
31079
diff
changeset
|
132 |
List<Integer> sizes = List.of(100, 1_000, 10_000, 1_000_000); |
80b046f15b53
8154049: DualPivot sorting calculates incorrect runs for nearly sorted arrays
psandoz
parents:
31079
diff
changeset
|
133 |
return baseCases. |
80b046f15b53
8154049: DualPivot sorting calculates incorrect runs for nearly sorted arrays
psandoz
parents:
31079
diff
changeset
|
134 |
flatMap(l -> sizes.stream().map(s -> append(l, s))). |
80b046f15b53
8154049: DualPivot sorting calculates incorrect runs for nearly sorted arrays
psandoz
parents:
31079
diff
changeset
|
135 |
toArray(Object[][]::new); |
80b046f15b53
8154049: DualPivot sorting calculates incorrect runs for nearly sorted arrays
psandoz
parents:
31079
diff
changeset
|
136 |
} |
80b046f15b53
8154049: DualPivot sorting calculates incorrect runs for nearly sorted arrays
psandoz
parents:
31079
diff
changeset
|
137 |
|
80b046f15b53
8154049: DualPivot sorting calculates incorrect runs for nearly sorted arrays
psandoz
parents:
31079
diff
changeset
|
138 |
Object[] append(List<Object> l, Object value) { |
80b046f15b53
8154049: DualPivot sorting calculates incorrect runs for nearly sorted arrays
psandoz
parents:
31079
diff
changeset
|
139 |
List<Object> nl = new ArrayList<>(l); |
80b046f15b53
8154049: DualPivot sorting calculates incorrect runs for nearly sorted arrays
psandoz
parents:
31079
diff
changeset
|
140 |
nl.add(value); |
80b046f15b53
8154049: DualPivot sorting calculates incorrect runs for nearly sorted arrays
psandoz
parents:
31079
diff
changeset
|
141 |
return nl.toArray(); |
80b046f15b53
8154049: DualPivot sorting calculates incorrect runs for nearly sorted arrays
psandoz
parents:
31079
diff
changeset
|
142 |
} |
80b046f15b53
8154049: DualPivot sorting calculates incorrect runs for nearly sorted arrays
psandoz
parents:
31079
diff
changeset
|
143 |
|
80b046f15b53
8154049: DualPivot sorting calculates incorrect runs for nearly sorted arrays
psandoz
parents:
31079
diff
changeset
|
144 |
@Test(dataProvider = "shapes") |
80b046f15b53
8154049: DualPivot sorting calculates incorrect runs for nearly sorted arrays
psandoz
parents:
31079
diff
changeset
|
145 |
public void testShapes(String testName, IntFunction<int[]> dataMethod, int size) { |
80b046f15b53
8154049: DualPivot sorting calculates incorrect runs for nearly sorted arrays
psandoz
parents:
31079
diff
changeset
|
146 |
int[] intSourceArray = dataMethod.apply(size); |
31079
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
147 |
|
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
148 |
// Clone source array to ensure it is not modified |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
149 |
this.sortAndAssert(intSourceArray.clone()); |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
150 |
this.sortAndAssert(floatCopyFromInt(intSourceArray)); |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
151 |
this.sortAndAssert(doubleCopyFromInt(intSourceArray)); |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
152 |
this.sortAndAssert(longCopyFromInt(intSourceArray)); |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
153 |
this.sortAndAssert(shortCopyFromInt(intSourceArray)); |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
154 |
this.sortAndAssert(charCopyFromInt(intSourceArray)); |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
155 |
} |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
156 |
|
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
157 |
private float[] floatCopyFromInt(int[] src) { |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
158 |
float[] result = new float[src.length]; |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
159 |
for (int i = 0; i < result.length; i++) { |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
160 |
result[i] = src[i]; |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
161 |
} |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
162 |
return result; |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
163 |
} |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
164 |
|
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
165 |
private double[] doubleCopyFromInt(int[] src) { |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
166 |
double[] result = new double[src.length]; |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
167 |
for (int i = 0; i < result.length; i++) { |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
168 |
result[i] = src[i]; |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
169 |
} |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
170 |
return result; |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
171 |
} |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
172 |
|
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
173 |
private long[] longCopyFromInt(int[] src) { |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
174 |
long[] result = new long[src.length]; |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
175 |
for (int i = 0; i < result.length; i++) { |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
176 |
result[i] = src[i]; |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
177 |
} |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
178 |
return result; |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
179 |
} |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
180 |
|
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
181 |
private short[] shortCopyFromInt(int[] src) { |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
182 |
short[] result = new short[src.length]; |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
183 |
for (int i = 0; i < result.length; i++) { |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
184 |
result[i] = (short) src[i]; |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
185 |
} |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
186 |
return result; |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
187 |
} |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
188 |
|
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
189 |
private char[] charCopyFromInt(int[] src) { |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
190 |
char[] result = new char[src.length]; |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
191 |
for (int i = 0; i < result.length; i++) { |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
192 |
result[i] = (char) src[i]; |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
193 |
} |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
194 |
return result; |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
195 |
} |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
196 |
|
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
197 |
private void sortAndAssert(int[] array) { |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
198 |
Arrays.sort(array); |
37912
80b046f15b53
8154049: DualPivot sorting calculates incorrect runs for nearly sorted arrays
psandoz
parents:
31079
diff
changeset
|
199 |
for (int i = 1; i < array.length; i++) { |
31079
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
200 |
if (array[i] < array[i - 1]) { |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
201 |
throw new AssertionError("not sorted"); |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
202 |
} |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
203 |
} |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
204 |
} |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
205 |
|
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
206 |
private void sortAndAssert(char[] array) { |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
207 |
Arrays.sort(array); |
37912
80b046f15b53
8154049: DualPivot sorting calculates incorrect runs for nearly sorted arrays
psandoz
parents:
31079
diff
changeset
|
208 |
for (int i = 1; i < array.length; i++) { |
31079
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
209 |
if (array[i] < array[i - 1]) { |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
210 |
throw new AssertionError("not sorted"); |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
211 |
} |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
212 |
} |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
213 |
} |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
214 |
|
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
215 |
private void sortAndAssert(short[] array) { |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
216 |
Arrays.sort(array); |
37912
80b046f15b53
8154049: DualPivot sorting calculates incorrect runs for nearly sorted arrays
psandoz
parents:
31079
diff
changeset
|
217 |
for (int i = 1; i < array.length; i++) { |
31079
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
218 |
if (array[i] < array[i - 1]) { |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
219 |
throw new AssertionError("not sorted"); |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
220 |
} |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
221 |
} |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
222 |
} |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
223 |
|
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
224 |
private void sortAndAssert(double[] array) { |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
225 |
Arrays.sort(array); |
37912
80b046f15b53
8154049: DualPivot sorting calculates incorrect runs for nearly sorted arrays
psandoz
parents:
31079
diff
changeset
|
226 |
for (int i = 1; i < array.length; i++) { |
31079
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
227 |
if (array[i] < array[i - 1]) { |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
228 |
throw new AssertionError("not sorted"); |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
229 |
} |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
230 |
} |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
231 |
} |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
232 |
|
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
233 |
private void sortAndAssert(float[] array) { |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
234 |
Arrays.sort(array); |
37912
80b046f15b53
8154049: DualPivot sorting calculates incorrect runs for nearly sorted arrays
psandoz
parents:
31079
diff
changeset
|
235 |
for (int i = 1; i < array.length; i++) { |
31079
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
236 |
if (array[i] < array[i - 1]) { |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
237 |
throw new AssertionError("not sorted"); |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
238 |
} |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
239 |
} |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
240 |
} |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
241 |
|
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
242 |
private void sortAndAssert(long[] array) { |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
243 |
Arrays.sort(array); |
37912
80b046f15b53
8154049: DualPivot sorting calculates incorrect runs for nearly sorted arrays
psandoz
parents:
31079
diff
changeset
|
244 |
for (int i = 1; i < array.length; i++) { |
31079
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
245 |
if (array[i] < array[i - 1]) { |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
246 |
throw new AssertionError("not sorted"); |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
247 |
} |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
248 |
} |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
249 |
} |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
250 |
|
37912
80b046f15b53
8154049: DualPivot sorting calculates incorrect runs for nearly sorted arrays
psandoz
parents:
31079
diff
changeset
|
251 |
private int[] zeroHiData(int size) { |
80b046f15b53
8154049: DualPivot sorting calculates incorrect runs for nearly sorted arrays
psandoz
parents:
31079
diff
changeset
|
252 |
int[] array = new int[size]; |
31079
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
253 |
|
37912
80b046f15b53
8154049: DualPivot sorting calculates incorrect runs for nearly sorted arrays
psandoz
parents:
31079
diff
changeset
|
254 |
int threeQuarters = (int) (size * 0.75); |
31079
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
255 |
for (int i = 0; i < threeQuarters; i++) { |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
256 |
array[i] = 0; |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
257 |
} |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
258 |
int k = 1; |
37912
80b046f15b53
8154049: DualPivot sorting calculates incorrect runs for nearly sorted arrays
psandoz
parents:
31079
diff
changeset
|
259 |
for (int i = threeQuarters; i < size; i++) { |
31079
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
260 |
array[i] = k; |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
261 |
k++; |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
262 |
} |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
263 |
|
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
264 |
return array; |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
265 |
} |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
266 |
|
37912
80b046f15b53
8154049: DualPivot sorting calculates incorrect runs for nearly sorted arrays
psandoz
parents:
31079
diff
changeset
|
267 |
private int[] hiZeroLowData(int size) { |
80b046f15b53
8154049: DualPivot sorting calculates incorrect runs for nearly sorted arrays
psandoz
parents:
31079
diff
changeset
|
268 |
int[] array = new int[size]; |
31079
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
269 |
|
37912
80b046f15b53
8154049: DualPivot sorting calculates incorrect runs for nearly sorted arrays
psandoz
parents:
31079
diff
changeset
|
270 |
int oneThird = size / 3; |
31079
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
271 |
for (int i = 0; i < oneThird; i++) { |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
272 |
array[i] = i; |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
273 |
} |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
274 |
int twoThirds = oneThird * 2; |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
275 |
for (int i = oneThird; i < twoThirds; i++) { |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
276 |
array[i] = 0; |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
277 |
} |
37912
80b046f15b53
8154049: DualPivot sorting calculates incorrect runs for nearly sorted arrays
psandoz
parents:
31079
diff
changeset
|
278 |
for (int i = twoThirds; i < size; i++) { |
31079
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
279 |
array[i] = oneThird - i + twoThirds; |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
280 |
} |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
281 |
return array; |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
282 |
} |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
283 |
|
37912
80b046f15b53
8154049: DualPivot sorting calculates incorrect runs for nearly sorted arrays
psandoz
parents:
31079
diff
changeset
|
284 |
private int[] highFlatLowData(int size) { |
80b046f15b53
8154049: DualPivot sorting calculates incorrect runs for nearly sorted arrays
psandoz
parents:
31079
diff
changeset
|
285 |
int[] array = new int[size]; |
31079
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
286 |
|
37912
80b046f15b53
8154049: DualPivot sorting calculates incorrect runs for nearly sorted arrays
psandoz
parents:
31079
diff
changeset
|
287 |
int oneThird = size / 3; |
31079
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
288 |
for (int i = 0; i < oneThird; i++) { |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
289 |
array[i] = i; |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
290 |
} |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
291 |
int twoThirds = oneThird * 2; |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
292 |
int constant = oneThird - 1; |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
293 |
for (int i = oneThird; i < twoThirds; i++) { |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
294 |
array[i] = constant; |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
295 |
} |
37912
80b046f15b53
8154049: DualPivot sorting calculates incorrect runs for nearly sorted arrays
psandoz
parents:
31079
diff
changeset
|
296 |
for (int i = twoThirds; i < size; i++) { |
31079
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
297 |
array[i] = constant - i + twoThirds; |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
298 |
} |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
299 |
|
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
300 |
return array; |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
301 |
} |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
302 |
|
37912
80b046f15b53
8154049: DualPivot sorting calculates incorrect runs for nearly sorted arrays
psandoz
parents:
31079
diff
changeset
|
303 |
private int[] identicalData(int size) { |
80b046f15b53
8154049: DualPivot sorting calculates incorrect runs for nearly sorted arrays
psandoz
parents:
31079
diff
changeset
|
304 |
int[] array = new int[size]; |
31079
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
305 |
int listNumber = 24; |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
306 |
|
37912
80b046f15b53
8154049: DualPivot sorting calculates incorrect runs for nearly sorted arrays
psandoz
parents:
31079
diff
changeset
|
307 |
for (int i = 0; i < size; i++) { |
31079
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
308 |
array[i] = listNumber; |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
309 |
} |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
310 |
|
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
311 |
return array; |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
312 |
} |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
313 |
|
37912
80b046f15b53
8154049: DualPivot sorting calculates incorrect runs for nearly sorted arrays
psandoz
parents:
31079
diff
changeset
|
314 |
private int[] endLessThanData(int size) { |
80b046f15b53
8154049: DualPivot sorting calculates incorrect runs for nearly sorted arrays
psandoz
parents:
31079
diff
changeset
|
315 |
int[] array = new int[size]; |
31079
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
316 |
|
37912
80b046f15b53
8154049: DualPivot sorting calculates incorrect runs for nearly sorted arrays
psandoz
parents:
31079
diff
changeset
|
317 |
for (int i = 0; i < size - 1; i++) { |
31079
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
318 |
array[i] = 3; |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
319 |
} |
37912
80b046f15b53
8154049: DualPivot sorting calculates incorrect runs for nearly sorted arrays
psandoz
parents:
31079
diff
changeset
|
320 |
array[size - 1] = 1; |
31079
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
321 |
|
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
322 |
return array; |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
323 |
} |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
324 |
|
37912
80b046f15b53
8154049: DualPivot sorting calculates incorrect runs for nearly sorted arrays
psandoz
parents:
31079
diff
changeset
|
325 |
private int[] sortedReversedSortedData(int size) { |
80b046f15b53
8154049: DualPivot sorting calculates incorrect runs for nearly sorted arrays
psandoz
parents:
31079
diff
changeset
|
326 |
int[] array = new int[size]; |
31079
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
327 |
|
37912
80b046f15b53
8154049: DualPivot sorting calculates incorrect runs for nearly sorted arrays
psandoz
parents:
31079
diff
changeset
|
328 |
for (int i = 0; i < size / 2; i++) { |
31079
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
329 |
array[i] = i; |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
330 |
} |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
331 |
int num = 0; |
37912
80b046f15b53
8154049: DualPivot sorting calculates incorrect runs for nearly sorted arrays
psandoz
parents:
31079
diff
changeset
|
332 |
for (int i = size / 2; i < size; i++) { |
80b046f15b53
8154049: DualPivot sorting calculates incorrect runs for nearly sorted arrays
psandoz
parents:
31079
diff
changeset
|
333 |
array[i] = size - num; |
31079
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
334 |
num++; |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
335 |
} |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
336 |
|
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
337 |
return array; |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
338 |
} |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
339 |
|
37912
80b046f15b53
8154049: DualPivot sorting calculates incorrect runs for nearly sorted arrays
psandoz
parents:
31079
diff
changeset
|
340 |
private int[] pairFlipData(int size) { |
80b046f15b53
8154049: DualPivot sorting calculates incorrect runs for nearly sorted arrays
psandoz
parents:
31079
diff
changeset
|
341 |
int[] array = new int[size]; |
31079
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
342 |
|
37912
80b046f15b53
8154049: DualPivot sorting calculates incorrect runs for nearly sorted arrays
psandoz
parents:
31079
diff
changeset
|
343 |
for (int i = 0; i < size; i++) { |
31079
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
344 |
array[i] = i; |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
345 |
} |
37912
80b046f15b53
8154049: DualPivot sorting calculates incorrect runs for nearly sorted arrays
psandoz
parents:
31079
diff
changeset
|
346 |
for (int i = 0; i < size; i += 2) { |
31079
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
347 |
int temp = array[i]; |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
348 |
array[i] = array[i + 1]; |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
349 |
array[i + 1] = temp; |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
350 |
} |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
351 |
|
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
352 |
return array; |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
353 |
} |
bf5fabb914b6
8080945: Improve the performance of primitive Arrays.sort for certain patterns of array elements
psandoz
parents:
diff
changeset
|
354 |
} |