mirror of
https://gitlab.uni-marburg.de/langbeid/powersort.git
synced 2025-02-22 00:35:24 +01:00
Merge remote-tracking branch 'origin/main'
This commit is contained in:
commit
8a08e65ae9
@ -10,3 +10,16 @@ java:
|
||||
when: always
|
||||
reports:
|
||||
junit: app/build/test-results/test/**/TEST-*.xml
|
||||
expire_in: 6 month
|
||||
|
||||
jmh:
|
||||
image: alpine:latest
|
||||
stage: test
|
||||
script:
|
||||
- apk --no-cache add openjdk23 gradle --repository=https://dl-cdn.alpinelinux.org/alpine/edge/testing/
|
||||
- gradle jmh --no-daemon
|
||||
# https://docs.gitlab.com/ee/ci/jobs/job_artifacts.html
|
||||
artifacts:
|
||||
paths:
|
||||
- app/build/reports/jmh/*
|
||||
expire_in: 6 month
|
||||
|
@ -118,7 +118,7 @@ Run Custom Benchmark (CGM) with
|
||||
#### Run JMH with CGL and Powersort competition lists
|
||||
|
||||
```shell
|
||||
./gradlew jmh
|
||||
./gradlew jmh --rerun
|
||||
```
|
||||
|
||||
- To benchmark only one of the different list collections, see `jmh { excludes }` at the bottom of [./app/build.gradle.kts](./app/build.gradle.kts).
|
||||
|
@ -97,9 +97,9 @@ jmh {
|
||||
forceGC = true
|
||||
|
||||
// If human output is saved, it won't be written to stdout while running the benchmark!
|
||||
//humanOutputFile = project.file("${project.layout.buildDirectory.get()}/reports/jmh/human.txt")
|
||||
humanOutputFile = project.file("${project.layout.buildDirectory.get()}/reports/jmh/human.txt")
|
||||
|
||||
resultsFile = project.file("${project.layout.buildDirectory.get()}/reports/jmh/results.txt")
|
||||
resultsFile = project.file("${project.layout.buildDirectory.get()}/reports/jmh/results.csv")
|
||||
resultFormat = "CSV"
|
||||
|
||||
excludes = listOf(
|
||||
|
@ -34,7 +34,7 @@ import java.util.concurrent.TimeUnit;
|
||||
// AverageTime: "Average time per operation."
|
||||
// - "This mode is time-based, and it will run until the iteration time expires."
|
||||
//@BenchmarkMode(Mode.AverageTime)
|
||||
//@Warmup(iterations = 6, time = 1, timeUnit = TimeUnit.SECONDS)
|
||||
//@Warmup(iterations = 20, time = 1, timeUnit = TimeUnit.SECONDS)
|
||||
//@Measurement(iterations = 6, time = 1, timeUnit = TimeUnit.SECONDS)
|
||||
|
||||
// SingleShotTime: "Time per single operation"
|
||||
@ -45,7 +45,7 @@ import java.util.concurrent.TimeUnit;
|
||||
// - Until the 17th spike of up to +750% (Maybe JVM optimizations happening?)
|
||||
// - After 40th constant slowdown of around +10% (Maybe CPU frequency adjustments?)
|
||||
// Thus, we need at least ~50 warmup iterations!
|
||||
@Warmup(iterations = 50)
|
||||
@Warmup(iterations = 60)
|
||||
@Measurement(iterations = 6)
|
||||
|
||||
/*
|
||||
|
@ -17,13 +17,22 @@ import org.openjdk.jmh.annotations.State;
|
||||
@State(Scope.Benchmark)
|
||||
public class JmhCgl extends JmhBase {
|
||||
// Either all or a selection of input lists.
|
||||
@Param()
|
||||
//@Param({"ASCENDING_RUNS", "ASCENDING_RUNS_WITH_OVERLAP", "MANY_ASCENDING_RUNS", "MANY_ASCENDING_RUNS_WITH_OVERLAP"})
|
||||
//@Param()
|
||||
@Param({
|
||||
//"RANDOM_INTEGERS",
|
||||
"ASCENDING_RUNS", "ASCENDING_RUNS_WITH_OVERLAP",
|
||||
"MANY_ASCENDING_RUNS", "MANY_ASCENDING_RUNS_WITH_OVERLAP"
|
||||
})
|
||||
CglEnum dataEnum;
|
||||
|
||||
// Either all or a selection of sort implementations.
|
||||
//@Param()
|
||||
@Param({"TIM_SORT", "FASTER_FINN_SORT", "IMPL_M_11", "IMPL_M_21"})
|
||||
@Param({
|
||||
"TIM_SORT",
|
||||
"FASTER_FINN_SORT",
|
||||
//"IMPL_M_40",
|
||||
"IMPL_M_50",
|
||||
})
|
||||
SortEnum sortEnum;
|
||||
|
||||
@Override
|
||||
@ -42,4 +51,4 @@ public class JmhCgl extends JmhBase {
|
||||
public void benchmark() {
|
||||
sortImpl.sort(workingCopy);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -16,9 +16,18 @@ import org.openjdk.jmh.annotations.State;
|
||||
*/
|
||||
@State(Scope.Benchmark)
|
||||
public class JmhCompetition extends JmhBase {
|
||||
@Param()
|
||||
//@Param()
|
||||
@Param({
|
||||
// Top 4 Heavyweight by #comparisons
|
||||
"COMPETITION_207", "COMPETITION_214", "COMPETITION_213", "COMPETITION_236",
|
||||
// Top 4 Heavyweight by #merge-cost
|
||||
"COMPETITION_198","COMPETITION_199","COMPETITION_232","COMPETITION_231",
|
||||
// Top 4 Heavyweight by combined metric
|
||||
"COMPETITION_214","COMPETITION_218","COMPETITION_236","COMPETITION_213",
|
||||
})
|
||||
CompetitionEnum dataEnum;
|
||||
@Param()
|
||||
//@Param()
|
||||
@Param({"TIM_SORT", "FASTER_FINN_SORT", "IMPL_M_40"})
|
||||
SortEnum sortEnum;
|
||||
|
||||
@Override
|
||||
|
@ -172,7 +172,7 @@ public class FasterFinnSort<T> {
|
||||
}
|
||||
|
||||
// TODO: Verify if this is correct
|
||||
int stackLen = ((int) Math.ceil(Math.log(rangeSize))) + 2;
|
||||
int stackLen = ((int) Math.ceil(Math.log(rangeSize) / Math.log(2))) + 2;
|
||||
runBase = new int[stackLen];
|
||||
runLen = new int[stackLen];
|
||||
runPower = new int[stackLen];
|
||||
|
@ -111,7 +111,7 @@ public class IMPL_M_1 {
|
||||
}
|
||||
|
||||
static <T> void mergeInplace(T[] a, int i, int m, int j, Comparator<? super T> c) {
|
||||
System.out.printf("Merge(%d, %d, %d)%n", i, m, j);
|
||||
// System.out.printf("Merge(%d, %d, %d)%n", i, m, j);
|
||||
MERGE_COST += j - i;
|
||||
// Create temporary arrays for merging
|
||||
@SuppressWarnings("unchecked")
|
||||
@ -131,7 +131,6 @@ public class IMPL_M_1 {
|
||||
System.arraycopy(merged, 0, a, i,merged.length);
|
||||
}
|
||||
|
||||
|
||||
static <T> int extendRun(T [] a, int i, Comparator<? super T> c) {
|
||||
// if i was the element before end so just return the last element
|
||||
if (i == a.length - 1) {
|
||||
|
File diff suppressed because it is too large
Load Diff
178
app/src/main/java/de/uni_marburg/powersort/MSort/IMPL_M_3.java
Normal file
178
app/src/main/java/de/uni_marburg/powersort/MSort/IMPL_M_3.java
Normal file
@ -0,0 +1,178 @@
|
||||
package de.uni_marburg.powersort.MSort;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Comparator;
|
||||
|
||||
public class IMPL_M_3 {
|
||||
|
||||
private static final int MIN_MERGE = 32;
|
||||
private static final int MIN_GALLOP = 7;
|
||||
|
||||
private IMPL_M_3() {
|
||||
}
|
||||
|
||||
public static void fillWithAscRunsHighToLow(Integer[] A, int[] runLengths, int runLenFactor) {
|
||||
int n = A.length;
|
||||
assert Arrays.stream(runLengths).sum() * runLenFactor == n;
|
||||
|
||||
for (int i = 0; i < n; i++) {
|
||||
A[i] = n - i;
|
||||
}
|
||||
|
||||
int startIndex = 0;
|
||||
for (int l : runLengths) {
|
||||
int L = l * runLenFactor;
|
||||
Arrays.sort(A, startIndex, startIndex + L);
|
||||
startIndex += L;
|
||||
}
|
||||
}
|
||||
|
||||
private static <T> int extendRun(T[] a, int i, Comparator<? super T> c) {
|
||||
if (i >= a.length - 1) {
|
||||
return a.length; // Return the end of the array
|
||||
}
|
||||
|
||||
int j = i + 1;
|
||||
boolean ascending = c.compare(a[i], a[j]) <= 0;
|
||||
|
||||
while (j < a.length && c.compare(a[j - 1], a[j]) == (ascending ? -1 : 1)) {
|
||||
j++;
|
||||
}
|
||||
|
||||
if (!ascending) {
|
||||
reverseRange(a, i, j);
|
||||
}
|
||||
|
||||
return j;
|
||||
}
|
||||
|
||||
private static <T> void reverseRange(T[] a, int start, int end) {
|
||||
end--;
|
||||
while (start < end) {
|
||||
T temp = a[start];
|
||||
a[start++] = a[end];
|
||||
a[end--] = temp;
|
||||
}
|
||||
}
|
||||
|
||||
private static <T> void mergeInplace(T[] a, int i, int m, int j, Comparator<? super T> c, T[] temp) {
|
||||
int leftSize = m - i;
|
||||
int rightSize = j - m;
|
||||
|
||||
// Validate indices
|
||||
if (leftSize < 0 || rightSize < 0) {
|
||||
throw new IllegalArgumentException("Invalid indices: leftSize=" + leftSize + ", rightSize=" + rightSize);
|
||||
}
|
||||
if (leftSize < 0) {
|
||||
throw new IllegalArgumentException("Invalid indices: leftSize is negative");
|
||||
}
|
||||
// Ensure the temporary array is large enough
|
||||
if (temp.length < leftSize) {
|
||||
temp = Arrays.copyOf(temp, leftSize);
|
||||
}
|
||||
|
||||
System.arraycopy(a, i, temp, 0, leftSize);
|
||||
|
||||
int li = 0, ri = m, k = i;
|
||||
int gallopCount = 0;
|
||||
|
||||
while (li < leftSize && ri < j) {
|
||||
if (c.compare(temp[li], a[ri]) <= 0) {
|
||||
a[k++] = temp[li++];
|
||||
gallopCount++;
|
||||
} else {
|
||||
a[k++] = a[ri++];
|
||||
gallopCount = 0;
|
||||
}
|
||||
|
||||
if (gallopCount >= MIN_GALLOP) {
|
||||
gallopCount = 0;
|
||||
while (li < leftSize && ri < j) {
|
||||
if (c.compare(temp[li], a[ri]) <= 0) {
|
||||
a[k++] = temp[li++];
|
||||
} else {
|
||||
a[k++] = a[ri++];
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
while (li < leftSize) a[k++] = temp[li++];
|
||||
while (ri < j) a[k++] = a[ri++];
|
||||
}
|
||||
|
||||
public static <T> void powerSort(T[] a, Comparator<? super T> c) {
|
||||
int n = a.length;
|
||||
if (n < MIN_MERGE) {
|
||||
Arrays.sort(a, c);
|
||||
return;
|
||||
}
|
||||
|
||||
// Initialize temporary array with a reasonable size
|
||||
T[] temp = (T[]) new Object[Math.min(n, MIN_MERGE)];
|
||||
int[] runStack = new int[40];
|
||||
int stackSize = 0;
|
||||
|
||||
int i = 0;
|
||||
while (i < n) {
|
||||
int j = extendRun(a, i, c);
|
||||
|
||||
// Ensure j > i
|
||||
if (j <= i) {
|
||||
throw new IllegalStateException("Invalid run: j <= i, i=" + i + ", j=" + j);
|
||||
}
|
||||
|
||||
int[] newRun = new int[]{i, j - i};
|
||||
|
||||
// Validate new run
|
||||
if (newRun[0] >= newRun[1]) {
|
||||
throw new IllegalArgumentException("Invalid run: start index >= length, i=" + i + ", j=" + j);
|
||||
}
|
||||
|
||||
i = j;
|
||||
|
||||
if (stackSize > 0) {
|
||||
int[] prevRun = new int[]{runStack[stackSize - 2], runStack[stackSize - 1]};
|
||||
int p = power(prevRun, newRun, n);
|
||||
|
||||
while (stackSize > 0 && p <= runStack[stackSize - 1]) {
|
||||
mergeInplace(a, runStack[stackSize - 2], runStack[stackSize - 1], i, c, temp);
|
||||
stackSize -= 2;
|
||||
}
|
||||
}
|
||||
|
||||
runStack[stackSize++] = newRun[0];
|
||||
runStack[stackSize++] = newRun[1];
|
||||
}
|
||||
|
||||
while (stackSize > 2) {
|
||||
mergeInplace(a, runStack[stackSize - 4], runStack[stackSize - 3], n, c, temp);
|
||||
stackSize -= 2;
|
||||
}
|
||||
}
|
||||
|
||||
private static int power(int[] run1, int[] run2, int n) {
|
||||
int i1 = run1[0], n1 = run1[1];
|
||||
int i2 = run2[0], n2 = run2[1];
|
||||
|
||||
int a = 2 * i1 + n1;
|
||||
int b = a + n1 + n2;
|
||||
|
||||
int l = 0;
|
||||
while (true) {
|
||||
l++;
|
||||
if (a >= n) {
|
||||
a -= n;
|
||||
b -= n;
|
||||
} else if (b >= n) {
|
||||
break;
|
||||
}
|
||||
a <<= 1;
|
||||
b <<= 1;
|
||||
}
|
||||
return l;
|
||||
}
|
||||
|
||||
|
||||
}
|
1018
app/src/main/java/de/uni_marburg/powersort/MSort/IMPL_M_4.java
Normal file
1018
app/src/main/java/de/uni_marburg/powersort/MSort/IMPL_M_4.java
Normal file
File diff suppressed because it is too large
Load Diff
996
app/src/main/java/de/uni_marburg/powersort/MSort/IMPL_M_5.java
Normal file
996
app/src/main/java/de/uni_marburg/powersort/MSort/IMPL_M_5.java
Normal file
@ -0,0 +1,996 @@
|
||||
package de.uni_marburg.powersort.MSort;
|
||||
|
||||
import java.util.Comparator;
|
||||
|
||||
/*
|
||||
* Copyright (c) 2009, 2013, Oracle and/or its affiliates. All rights reserved.
|
||||
* Copyright 2009 Google Inc. All Rights Reserved.
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License version 2 only, as
|
||||
* published by the Free Software Foundation. Oracle designates this
|
||||
* particular file as subject to the "Classpath" exception as provided
|
||||
* by Oracle in the LICENSE file that accompanied this code.
|
||||
*
|
||||
* This code is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||
* version 2 for more details (a copy is included in the LICENSE file that
|
||||
* accompanied this code).
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License version
|
||||
* 2 along with this work; if not, write to the Free Software Foundation,
|
||||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||
* or visit www.oracle.com if you need additional information or have any
|
||||
* questions.
|
||||
*/
|
||||
|
||||
/*
|
||||
* Imported from OpenJDK git repo TimSort.java
|
||||
*/
|
||||
|
||||
|
||||
/**
|
||||
* A stable, adaptive, iterative mergesort that requires far fewer than
|
||||
* n lg(n) comparisons when running on partially sorted arrays, while
|
||||
* offering performance comparable to a traditional mergesort when run
|
||||
* on random arrays. Like all proper mergesorts, this sort is stable and
|
||||
* runs O(n log n) time (worst case). In the worst case, this sort requires
|
||||
* temporary storage space for n/2 object references; in the best case,
|
||||
* it requires only a small constant amount of space.
|
||||
*
|
||||
* This implementation was adapted from Tim Peters's list sort for
|
||||
* Python, which is described in detail here:
|
||||
*
|
||||
* http://svn.python.org/projects/python/trunk/Objects/listsort.txt
|
||||
*
|
||||
* Tim's C code may be found here:
|
||||
*
|
||||
* http://svn.python.org/projects/python/trunk/Objects/listobject.c
|
||||
*
|
||||
* The underlying techniques are described in this paper (and may have
|
||||
* even earlier origins):
|
||||
*
|
||||
* "Optimistic Sorting and Information Theoretic Complexity"
|
||||
* Peter McIlroy
|
||||
* SODA (Fourth Annual ACM-SIAM Symposium on Discrete Algorithms),
|
||||
* pp 467-474, Austin, Texas, 25-27 January 1993.
|
||||
*
|
||||
* While the API to this class consists solely of static methods, it is
|
||||
* (privately) instantiable; a TimSort instance holds the state of an ongoing
|
||||
* sort, assuming the input array is large enough to warrant the full-blown
|
||||
* TimSort. Small arrays are sorted in place, using a binary insertion sort.
|
||||
*
|
||||
* @author Josh Bloch
|
||||
*/
|
||||
public class IMPL_M_5<T> {
|
||||
|
||||
/**
|
||||
* This is the minimum sized sequence that will be merged. Shorter
|
||||
* sequences will be lengthened by calling binarySort. If the entire
|
||||
* array is less than this length, no merges will be performed.
|
||||
*
|
||||
* This constant should be a power of two. It was 64 in Tim Peter's C
|
||||
* implementation, but 32 was empirically determined to work better in
|
||||
* this implementation. In the unlikely event that you set this constant
|
||||
* to be a number that's not a power of two, you'll need to change the
|
||||
* {@link #minRunLength} computation.
|
||||
*
|
||||
* If you decrease this constant, you must change the stackLen
|
||||
* computation in the TimSort constructor, or you risk an
|
||||
* ArrayOutOfBounds exception. See listsort.txt for a discussion
|
||||
* of the minimum stack length required as a function of the length
|
||||
* of the array being sorted and the minimum merge sequence length.
|
||||
*/
|
||||
private static final int MIN_MERGE = 31;
|
||||
|
||||
|
||||
/**
|
||||
* The array being sorted.
|
||||
*/
|
||||
private final T[] a;
|
||||
|
||||
/**
|
||||
* The comparator for this sort.
|
||||
*/
|
||||
private final Comparator<? super T> c;
|
||||
|
||||
/**
|
||||
* When we get into galloping mode, we stay there until both runs win less
|
||||
* often than MIN_GALLOP consecutive times.
|
||||
*/
|
||||
private static final int MIN_GALLOP = 7;
|
||||
|
||||
/**
|
||||
* This controls when we get *into* galloping mode. It is initialized
|
||||
* to MIN_GALLOP. The mergeLo and mergeHi methods nudge it higher for
|
||||
* random data, and lower for highly structured data.
|
||||
*/
|
||||
private int minGallop = MIN_GALLOP;
|
||||
|
||||
/**
|
||||
* Maximum initial size of tmp array, which is used for merging. The array
|
||||
* can grow to accommodate demand.
|
||||
*
|
||||
* Unlike Tim's original C version, we do not allocate this much storage
|
||||
* when sorting smaller arrays. This change was required for performance.
|
||||
*/
|
||||
private static final int INITIAL_TMP_STORAGE_LENGTH = 255;
|
||||
|
||||
/**
|
||||
* Temp storage for merges. A workspace array may optionally be
|
||||
* provided in constructor, and if so will be used as long as it
|
||||
* is big enough.
|
||||
*/
|
||||
private T[] tmp;
|
||||
private int tmpBase; // base of tmp array slice
|
||||
private int tmpLen; // length of tmp array slice
|
||||
|
||||
/**
|
||||
* A stack of pending runs yet to be merged. Run i starts at
|
||||
* address base[i] and extends for len[i] elements. It's always
|
||||
* true (so long as the indices are in bounds) that:
|
||||
*
|
||||
* runBase[i] + runLen[i] == runBase[i + 1]
|
||||
*
|
||||
* so we could cut the storage for this, but it's a minor amount,
|
||||
* and keeping all the info explicit simplifies the code.
|
||||
*/
|
||||
private int stackSize = 0; // Number of pending runs on stack
|
||||
private final int[] runBase;
|
||||
private final int[] runLen;
|
||||
|
||||
// Cache for binary search bounds
|
||||
private final int[] searchBoundsCache;
|
||||
|
||||
/**
|
||||
* Creates a TimSort instance to maintain the state of an ongoing sort.
|
||||
*
|
||||
* @param a the array to be sorted
|
||||
* @param c the comparator to determine the order of the sort
|
||||
* @param work a workspace array (slice)
|
||||
* @param workBase origin of usable space in work array
|
||||
* @param workLen usable size of work array
|
||||
*/
|
||||
|
||||
|
||||
/**
|
||||
MINE
|
||||
**/
|
||||
|
||||
|
||||
private final int[] runPower; // Added to track power of each run
|
||||
private static final int PARALLEL_THRESHOLD = 1 << 16; // 65,536 elements
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
private IMPL_M_5(T[] a, Comparator<? super T> c, T[] work, int workBase, int workLen) {
|
||||
this.a = a;
|
||||
this.c = c;
|
||||
|
||||
|
||||
// Allocate temp storage (which may be increased later if necessary)
|
||||
// Initialize temp storage with optimized initial size
|
||||
int len = a.length;
|
||||
int tlen = (len < 2 * INITIAL_TMP_STORAGE_LENGTH) ?
|
||||
len >>> 1 : INITIAL_TMP_STORAGE_LENGTH;
|
||||
|
||||
if (work == null || workLen < tlen || workBase + tlen > work.length) {
|
||||
@SuppressWarnings({"unchecked", "UnnecessaryLocalVariable"})
|
||||
T[] newArray = (T[])java.lang.reflect.Array.newInstance(
|
||||
a.getClass().getComponentType(), tlen);
|
||||
tmp = newArray;
|
||||
tmpBase = 0;
|
||||
tmpLen = tlen;
|
||||
} else {
|
||||
tmp = work;
|
||||
tmpBase = workBase;
|
||||
tmpLen = workLen;
|
||||
}
|
||||
|
||||
/*
|
||||
* Allocate runs-to-be-merged stack (which cannot be expanded). The
|
||||
* stack length requirements are described in listsort.txt. The C
|
||||
* version always uses the same stack length (85), but this was
|
||||
* measured to be too expensive when sorting "mid-sized" arrays (e.g.,
|
||||
* 100 elements) in Java. Therefore, we use smaller (but sufficiently
|
||||
* large) stack lengths for smaller arrays. The "magic numbers" in the
|
||||
* computation below must be changed if MIN_MERGE is decreased. See
|
||||
* the MIN_MERGE declaration above for more information.
|
||||
* The maximum value of 49 allows for an array up to length
|
||||
* Integer.MAX_VALUE-4, if array is filled by the worst case stack size
|
||||
* increasing scenario. More explanations are given in section 4 of:
|
||||
* http://envisage-project.eu/wp-content/uploads/2015/02/sorting.pdf
|
||||
*/
|
||||
// Optimize stack size based on array length
|
||||
int stackLen = (len < 120 ? 5 :
|
||||
len < 1542 ? 10 :
|
||||
len < 119151 ? 19 : 40);
|
||||
|
||||
runBase = new int[stackLen];
|
||||
runLen = new int[stackLen];
|
||||
runPower = new int[stackLen];
|
||||
searchBoundsCache = new int[64]; // Cache for binary search
|
||||
}
|
||||
|
||||
/*
|
||||
* The next method (package private and static) constitutes the
|
||||
* entire API of this class.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Sorts the given range, using the given workspace array slice
|
||||
* for temp storage when possible. This method is designed to be
|
||||
* invoked from public methods (in class Arrays) after performing
|
||||
* any necessary array bounds checks and expanding parameters into
|
||||
* the required forms.
|
||||
*
|
||||
* @param a the array to be sorted
|
||||
* @param lo the index of the first element, inclusive, to be sorted
|
||||
* @param hi the index of the last element, exclusive, to be sorted
|
||||
* @param c the comparator to use
|
||||
* @param work a workspace array (slice)
|
||||
* @param workBase origin of usable space in work array
|
||||
* @param workLen usable size of work array
|
||||
* @since 1.8
|
||||
*/
|
||||
public static <T> void sort(T[] a, int lo, int hi, Comparator<? super T> c,
|
||||
T[] work, int workBase, int workLen) {
|
||||
if (hi - lo < 2) return;
|
||||
|
||||
IMPL_M_5<T> sorter = new IMPL_M_5<>(a, c, work, workBase, workLen);
|
||||
sorter.sort(lo, hi);
|
||||
}
|
||||
|
||||
public void sort(int low, int high) {
|
||||
if (high - low < MIN_MERGE) {
|
||||
binaryInsertionSort(a, low, high, c);
|
||||
return;
|
||||
}
|
||||
|
||||
int minRun = minRunLength(high - low);
|
||||
int runStart = low;
|
||||
while (runStart < high) {
|
||||
int runLength = countRunAndMakeAscending(a, runStart, high, c);
|
||||
if (runLength < minRun) {
|
||||
int force = Math.min(high - runStart, minRun);
|
||||
binaryInsertionSort(a, runStart, runStart + force, c);
|
||||
runLength = force;
|
||||
}
|
||||
pushRun(runStart, runLength, stackSize);
|
||||
mergeCollapse();
|
||||
runStart += runLength;
|
||||
}
|
||||
mergeForceCollapse();
|
||||
}
|
||||
|
||||
|
||||
private static <T> void binaryInsertionSort(T[] a, int lo, int hi, Comparator<? super T> c) {
|
||||
for (int i = lo + 1; i < hi; i++) {
|
||||
T pivot = a[i];
|
||||
int left = lo;
|
||||
int right = i;
|
||||
while (left < right) {
|
||||
int mid = (left + right) >>> 1;
|
||||
if (c.compare(pivot, a[mid]) < 0)
|
||||
right = mid;
|
||||
else
|
||||
left = mid + 1;
|
||||
}
|
||||
System.arraycopy(a, left, a, left + 1, i - left);
|
||||
a[left] = pivot;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Sorts the specified portion of the specified array using a binary
|
||||
* insertion sort. This is the best method for sorting small numbers
|
||||
* of elements. It requires O(n log n) compares, but O(n^2) data
|
||||
* movement (worst case).
|
||||
*
|
||||
* If the initial part of the specified range is already sorted,
|
||||
* this method can take advantage of it: the method assumes that the
|
||||
* elements from index {@code lo}, inclusive, to {@code start},
|
||||
* exclusive are already sorted.
|
||||
*
|
||||
* @param a the array in which a range is to be sorted
|
||||
* @param lo the index of the first element in the range to be sorted
|
||||
* @param hi the index after the last element in the range to be sorted
|
||||
* @param start the index of the first element in the range that is
|
||||
* not already known to be sorted ({@code lo <= start <= hi})
|
||||
* @param c comparator to used for the sort
|
||||
*/
|
||||
@SuppressWarnings("fallthrough")
|
||||
private static <T> void binarySort(T[] a, int lo, int hi, int start,
|
||||
Comparator<? super T> c) {
|
||||
assert lo <= start && start <= hi;
|
||||
if (start == lo)
|
||||
start++;
|
||||
for ( ; start < hi; start++) {
|
||||
T pivot = a[start];
|
||||
|
||||
// Set left (and right) to the index where a[start] (pivot) belongs
|
||||
int left = lo;
|
||||
int right = start;
|
||||
assert left <= right;
|
||||
/*
|
||||
* Invariants:
|
||||
* pivot >= all in [lo, left).
|
||||
* pivot < all in [right, start).
|
||||
*/
|
||||
while (left < right) {
|
||||
int mid = (left + right) >>> 1;
|
||||
if (c.compare(pivot, a[mid]) < 0)
|
||||
right = mid;
|
||||
else
|
||||
left = mid + 1;
|
||||
}
|
||||
assert left == right;
|
||||
|
||||
/*
|
||||
* The invariants still hold: pivot >= all in [lo, left) and
|
||||
* pivot < all in [left, start), so pivot belongs at left. Note
|
||||
* that if there are elements equal to pivot, left points to the
|
||||
* first slot after them -- that's why this sort is stable.
|
||||
* Slide elements over to make room for pivot.
|
||||
*/
|
||||
int n = start - left; // The number of elements to move
|
||||
// Switch is just an optimization for arraycopy in default case
|
||||
switch (n) {
|
||||
case 2: a[left + 2] = a[left + 1];
|
||||
case 1: a[left + 1] = a[left];
|
||||
break;
|
||||
default: System.arraycopy(a, left, a, left + 1, n);
|
||||
}
|
||||
a[left] = pivot;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the length of the run beginning at the specified position in
|
||||
* the specified array and reverses the run if it is descending (ensuring
|
||||
* that the run will always be ascending when the method returns).
|
||||
*
|
||||
* A run is the longest ascending sequence with:
|
||||
*
|
||||
* a[lo] <= a[lo + 1] <= a[lo + 2] <= ...
|
||||
*
|
||||
* or the longest descending sequence with:
|
||||
*
|
||||
* a[lo] > a[lo + 1] > a[lo + 2] > ...
|
||||
*
|
||||
* For its intended use in a stable mergesort, the strictness of the
|
||||
* definition of "descending" is needed so that the call can safely
|
||||
* reverse a descending sequence without violating stability.
|
||||
*
|
||||
* @param a the array in which a run is to be counted and possibly reversed
|
||||
* @param lo index of the first element in the run
|
||||
* @param hi index after the last element that may be contained in the run.
|
||||
* It is required that {@code lo < hi}.
|
||||
* @param c the comparator to used for the sort
|
||||
* @return the length of the run beginning at the specified position in
|
||||
* the specified array
|
||||
*/
|
||||
private int countRunAndMakeAscending(T[] a, int lo, int hi, Comparator<? super T> c) {
|
||||
int runHi = lo + 1;
|
||||
if (runHi == hi) return 1;
|
||||
if (c.compare(a[runHi++], a[lo]) < 0) {
|
||||
while (runHi < hi && c.compare(a[runHi], a[runHi - 1]) < 0)
|
||||
runHi++;
|
||||
reverseRange(a, lo, runHi);
|
||||
} else {
|
||||
while (runHi < hi && c.compare(a[runHi], a[runHi - 1]) >= 0)
|
||||
runHi++;
|
||||
}
|
||||
return runHi - lo;
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse the specified range of the specified array.
|
||||
*
|
||||
* @param a the array in which a range is to be reversed
|
||||
* @param lo the index of the first element in the range to be reversed
|
||||
* @param hi the index after the last element in the range to be reversed
|
||||
*/
|
||||
private void reverseRange(T[] a, int lo, int hi) {
|
||||
hi--;
|
||||
while (lo < hi) {
|
||||
T t = a[lo];
|
||||
a[lo++] = a[hi];
|
||||
a[hi--] = t;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the minimum acceptable run length for an array of the specified
|
||||
* length. Natural runs shorter than this will be extended with
|
||||
* {@link #binarySort}.
|
||||
*
|
||||
* Roughly speaking, the computation is:
|
||||
*
|
||||
* If n < MIN_MERGE, return n (it's too small to bother with fancy stuff).
|
||||
* Else if n is an exact power of 2, return MIN_MERGE/2.
|
||||
* Else return an int k, MIN_MERGE/2 <= k <= MIN_MERGE, such that n/k
|
||||
* is close to, but strictly less than, an exact power of 2.
|
||||
*
|
||||
* For the rationale, see listsort.txt.
|
||||
*
|
||||
* @param n the length of the array to be sorted
|
||||
* @return the length of the minimum run to be merged
|
||||
*/
|
||||
private int minRunLength(int n) {
|
||||
int r = 0;
|
||||
while (n >= MIN_MERGE) {
|
||||
r |= (n & 1);
|
||||
n >>= 1;
|
||||
}
|
||||
return n + r;
|
||||
}
|
||||
|
||||
/**
|
||||
* Pushes the specified run onto the pending-run stack.
|
||||
*
|
||||
* @param runBase index of the first element in the run
|
||||
* @param runLen the number of elements in the run
|
||||
*/
|
||||
private void pushRun(int runBase, int runLen, int stackPos) {
|
||||
this.runBase[stackPos] = runBase;
|
||||
this.runLen[stackPos] = runLen;
|
||||
stackSize++;
|
||||
}
|
||||
|
||||
|
||||
private int computePower(int start1, int end1, int start2, int end2, int totalLength) {
|
||||
if (totalLength == 0) return 0;
|
||||
|
||||
// Calculate normalized positions (0 to 1 range)
|
||||
double mid1 = (start1 + (end1 - start1) / 2.0) / totalLength;
|
||||
double mid2 = (start2 + (end2 - start2) / 2.0) / totalLength;
|
||||
|
||||
// Fast path for equal midpoints
|
||||
if (Math.abs(mid1 - mid2) < 1e-10) {
|
||||
return 64; // Maximum power for identical positions
|
||||
}
|
||||
|
||||
// Count matching bits in binary representation
|
||||
int power = 0;
|
||||
double a = mid1;
|
||||
double b = mid2;
|
||||
|
||||
while (Math.floor(a) == Math.floor(b) && power < 64) {
|
||||
a = (a - Math.floor(a)) * 2;
|
||||
b = (b - Math.floor(b)) * 2;
|
||||
power++;
|
||||
}
|
||||
|
||||
return power;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Examines the stack of runs waiting to be merged and merges adjacent runs
|
||||
* until the stack invariants are reestablished:
|
||||
*
|
||||
* 1. runLen[i - 3] > runLen[i - 2] + runLen[i - 1]
|
||||
* 2. runLen[i - 2] > runLen[i - 1]
|
||||
*
|
||||
* This method is called each time a new run is pushed onto the stack,
|
||||
* so the invariants are guaranteed to hold for i < stackSize upon
|
||||
* entry to the method.
|
||||
*
|
||||
* Thanks to Stijn de Gouw, Jurriaan Rot, Frank S. de Boer,
|
||||
* Richard Bubel and Reiner Hahnle, this is fixed with respect to
|
||||
* the analysis in "On the Worst-Case Complexity of TimSort" by
|
||||
* Nicolas Auger, Vincent Jug, Cyril Nicaud, and Carine Pivoteau.
|
||||
*/
|
||||
private void mergeCollapse() {
|
||||
while (stackSize > 1) {
|
||||
int n = stackSize - 2;
|
||||
if (n > 0 && runLen[n - 1] <= runLen[n] + runLen[n + 1]) {
|
||||
if (runLen[n - 1] < runLen[n + 1]) {
|
||||
mergeAt(n - 1);
|
||||
} else {
|
||||
mergeAt(n);
|
||||
}
|
||||
} else if (runLen[n] <= runLen[n + 1]) {
|
||||
mergeAt(n);
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Merges all runs on the stack until only one remains. This method is
|
||||
* called once, to complete the sort.
|
||||
*/
|
||||
private void mergeForceCollapse() {
|
||||
while (stackSize > 1) {
|
||||
int n = stackSize - 2;
|
||||
if (n > 0 && runLen[n - 1] < runLen[n + 1]) {
|
||||
n--;
|
||||
}
|
||||
mergeAt(n);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Merges the two runs at stack indices i and i+1. Run i must be
|
||||
* the penultimate or antepenultimate run on the stack. In other words,
|
||||
* i must be equal to stackSize-2 or stackSize-3.
|
||||
*
|
||||
* @param i stack index of the first of the two runs to merge
|
||||
*/
|
||||
private void mergeAt(int i) {
|
||||
assert stackSize >= 2;
|
||||
assert i >= 0;
|
||||
assert i == stackSize - 2 || i == stackSize - 3;
|
||||
|
||||
int base1 = runBase[i];
|
||||
int len1 = runLen[i];
|
||||
int base2 = runBase[i + 1];
|
||||
int len2 = runLen[i + 1];
|
||||
assert len1 > 0 && len2 > 0;
|
||||
assert base1 + len1 == base2;
|
||||
|
||||
// if (i < 0 || i >= stackSize - 1) {
|
||||
// throw new IllegalStateException("mergeAt called with invalid index: " + i);
|
||||
// }
|
||||
// System.out.println("Merging at index: " + i + " with stackSize: " + stackSize);
|
||||
|
||||
/*
|
||||
* Record the length of the combined runs; if i is the 3rd-last
|
||||
* run now, also slide over the last run (which isn't involved
|
||||
* in this merge). The current run (i+1) goes away in any case.
|
||||
*/
|
||||
runLen[i] = len1 + len2;
|
||||
|
||||
// Update runPower before modifying the stack structure
|
||||
runPower[i] = runPower[i + 1]; // Transfer power from the absorbed run
|
||||
|
||||
if (i == stackSize - 3) {
|
||||
runBase[i + 1] = runBase[i + 2];
|
||||
runLen[i + 1] = runLen[i + 2];
|
||||
runPower[i + 1] = runPower[i + 2]; // Also slide the power value
|
||||
}
|
||||
stackSize--;
|
||||
|
||||
/*
|
||||
* Find where the first element of run2 goes in run1. Prior elements
|
||||
* in run1 can be ignored (because they're already in place).
|
||||
*/
|
||||
int k = gallopRight(a[base2], a, base1, len1, 0, c);
|
||||
assert k >= 0;
|
||||
base1 += k;
|
||||
len1 -= k;
|
||||
if (len1 == 0)
|
||||
return;
|
||||
|
||||
/*
|
||||
* Find where the last element of run1 goes in run2. Subsequent elements
|
||||
* in run2 can be ignored (because they're already in place).
|
||||
*/
|
||||
len2 = gallopLeft(a[base1 + len1 - 1], a, base2, len2, len2 - 1, c);
|
||||
assert len2 >= 0;
|
||||
if (len2 == 0)
|
||||
return;
|
||||
|
||||
// Merge remaining runs, using tmp array with min(len1, len2) elements
|
||||
if (len1 <= len2)
|
||||
mergeLo(base1, len1, base2, len2);
|
||||
else
|
||||
mergeHi(base1, len1, base2, len2);
|
||||
}
|
||||
|
||||
/**
|
||||
* Locates the position at which to insert the specified key into the
|
||||
* specified sorted range; if the range contains an element equal to key,
|
||||
* returns the index of the leftmost equal element.
|
||||
*
|
||||
* @param key the key whose insertion point to search for
|
||||
* @param a the array in which to search
|
||||
* @param base the index of the first element in the range
|
||||
* @param len the length of the range; must be > 0
|
||||
* @param hint the index at which to begin the search, 0 <= hint < n.
|
||||
* The closer hint is to the result, the faster this method will run.
|
||||
* @param c the comparator used to order the range, and to search
|
||||
* @return the int k, 0 <= k <= n such that a[b + k - 1] < key <= a[b + k],
|
||||
* pretending that a[b - 1] is minus infinity and a[b + n] is infinity.
|
||||
* In other words, key belongs at index b + k; or in other words,
|
||||
* the first k elements of a should precede key, and the last n - k
|
||||
* should follow it.
|
||||
*/
|
||||
private static <T> int gallopLeft(T key, T[] a, int base, int len, int hint,
|
||||
Comparator<? super T> c) {
|
||||
int lastOfs = 0;
|
||||
int ofs = 1;
|
||||
|
||||
if (c.compare(key, a[base + hint]) > 0) {
|
||||
int maxOfs = len - hint;
|
||||
while (ofs < maxOfs && c.compare(key, a[base + hint + ofs]) > 0) {
|
||||
lastOfs = ofs;
|
||||
ofs = (ofs << 1) + 1;
|
||||
if (ofs <= 0) ofs = maxOfs;
|
||||
}
|
||||
if (ofs > maxOfs) ofs = maxOfs;
|
||||
|
||||
lastOfs += hint;
|
||||
ofs += hint;
|
||||
} else {
|
||||
int maxOfs = hint + 1;
|
||||
while (ofs < maxOfs && c.compare(key, a[base + hint - ofs]) <= 0) {
|
||||
lastOfs = ofs;
|
||||
ofs = (ofs << 1) + 1;
|
||||
if (ofs <= 0) ofs = maxOfs;
|
||||
}
|
||||
if (ofs > maxOfs) ofs = maxOfs;
|
||||
|
||||
int tmp = lastOfs;
|
||||
lastOfs = hint - ofs;
|
||||
ofs = hint - tmp;
|
||||
}
|
||||
|
||||
lastOfs++;
|
||||
while (lastOfs < ofs) {
|
||||
int m = lastOfs + ((ofs - lastOfs) >>> 1);
|
||||
if (c.compare(key, a[base + m]) > 0) {
|
||||
lastOfs = m + 1;
|
||||
} else {
|
||||
ofs = m;
|
||||
}
|
||||
}
|
||||
return ofs;
|
||||
}
|
||||
|
||||
/**
|
||||
* Like gallopLeft, except that if the range contains an element equal to
|
||||
* key, gallopRight returns the index after the rightmost equal element.
|
||||
*
|
||||
* @param key the key whose insertion point to search for
|
||||
* @param a the array in which to search
|
||||
* @param base the index of the first element in the range
|
||||
* @param len the length of the range; must be > 0
|
||||
* @param hint the index at which to begin the search, 0 <= hint < n.
|
||||
* The closer hint is to the result, the faster this method will run.
|
||||
* @param c the comparator used to order the range, and to search
|
||||
* @return the int k, 0 <= k <= n such that a[b + k - 1] <= key < a[b + k]
|
||||
*/
|
||||
private static <T> int gallopRight(T key, T[] a, int base, int len,
|
||||
int hint, Comparator<? super T> c) {
|
||||
assert len > 0 && hint >= 0 && hint < len;
|
||||
|
||||
int ofs = 1;
|
||||
int lastOfs = 0;
|
||||
if (c.compare(key, a[base + hint]) < 0) {
|
||||
// Gallop left until a[b+hint - ofs] <= key < a[b+hint - lastOfs]
|
||||
int maxOfs = hint + 1;
|
||||
while (ofs < maxOfs && c.compare(key, a[base + hint - ofs]) < 0) {
|
||||
lastOfs = ofs;
|
||||
ofs = (ofs << 1) + 1;
|
||||
if (ofs <= 0) // int overflow
|
||||
ofs = maxOfs;
|
||||
}
|
||||
if (ofs > maxOfs)
|
||||
ofs = maxOfs;
|
||||
|
||||
// Make offsets relative to b
|
||||
int tmp = lastOfs;
|
||||
lastOfs = hint - ofs;
|
||||
ofs = hint - tmp;
|
||||
} else { // a[b + hint] <= key
|
||||
// Gallop right until a[b+hint + lastOfs] <= key < a[b+hint + ofs]
|
||||
int maxOfs = len - hint;
|
||||
while (ofs < maxOfs && c.compare(key, a[base + hint + ofs]) >= 0) {
|
||||
lastOfs = ofs;
|
||||
ofs = (ofs << 1) + 1;
|
||||
if (ofs <= 0) // int overflow
|
||||
ofs = maxOfs;
|
||||
}
|
||||
if (ofs > maxOfs)
|
||||
ofs = maxOfs;
|
||||
|
||||
// Make offsets relative to b
|
||||
lastOfs += hint;
|
||||
ofs += hint;
|
||||
}
|
||||
assert -1 <= lastOfs && lastOfs < ofs && ofs <= len;
|
||||
|
||||
/*
|
||||
* Now a[b + lastOfs] <= key < a[b + ofs], so key belongs somewhere to
|
||||
* the right of lastOfs but no farther right than ofs. Do a binary
|
||||
* search, with invariant a[b + lastOfs - 1] <= key < a[b + ofs].
|
||||
*/
|
||||
lastOfs++;
|
||||
while (lastOfs < ofs) {
|
||||
int m = lastOfs + ((ofs - lastOfs) >>> 1);
|
||||
|
||||
if (c.compare(key, a[base + m]) < 0)
|
||||
ofs = m; // key < a[b + m]
|
||||
else
|
||||
lastOfs = m + 1; // a[b + m] <= key
|
||||
}
|
||||
assert lastOfs == ofs; // so a[b + ofs - 1] <= key < a[b + ofs]
|
||||
return ofs;
|
||||
}
|
||||
|
||||
/**
|
||||
* Merges two adjacent runs in place, in a stable fashion. The first
|
||||
* element of the first run must be greater than the first element of the
|
||||
* second run (a[base1] > a[base2]), and the last element of the first run
|
||||
* (a[base1 + len1-1]) must be greater than all elements of the second run.
|
||||
*
|
||||
* For performance, this method should be called only when len1 <= len2;
|
||||
* its twin, mergeHi should be called if len1 >= len2. (Either method
|
||||
* may be called if len1 == len2.)
|
||||
*
|
||||
* @param base1 index of first element in first run to be merged
|
||||
* @param len1 length of first run to be merged (must be > 0)
|
||||
* @param base2 index of first element in second run to be merged
|
||||
* (must be aBase + aLen)
|
||||
* @param len2 length of second run to be merged (must be > 0)
|
||||
*/
|
||||
private void mergeLo(int base1, int len1, int base2, int len2) {
|
||||
assert len1 > 0 && len2 > 0 && base1 + len1 == base2;
|
||||
|
||||
// Copy first run into temp array
|
||||
T[] a = this.a; // For performance
|
||||
T[] tmp = ensureCapacity(len1);
|
||||
int cursor1 = tmpBase; // Indexes into tmp array
|
||||
int cursor2 = base2; // Indexes int a
|
||||
int dest = base1; // Indexes int a
|
||||
System.arraycopy(a, base1, tmp, cursor1, len1);
|
||||
|
||||
// Move first element of second run and deal with degenerate cases
|
||||
a[dest++] = a[cursor2++];
|
||||
if (--len2 == 0) {
|
||||
System.arraycopy(tmp, cursor1, a, dest, len1);
|
||||
return;
|
||||
}
|
||||
if (len1 == 1) {
|
||||
System.arraycopy(a, cursor2, a, dest, len2);
|
||||
a[dest + len2] = tmp[cursor1]; // Last elt of run 1 to end of merge
|
||||
return;
|
||||
}
|
||||
|
||||
Comparator<? super T> c = this.c; // Use local variable for performance
|
||||
int minGallop = this.minGallop; // " " " " "
|
||||
outer:
|
||||
while (true) {
|
||||
int count1 = 0; // Number of times in a row that first run won
|
||||
int count2 = 0; // Number of times in a row that second run won
|
||||
|
||||
/*
|
||||
* Do the straightforward thing until (if ever) one run starts
|
||||
* winning consistently.
|
||||
*/
|
||||
do {
|
||||
assert len1 > 1 && len2 > 0;
|
||||
if (c.compare(a[cursor2], tmp[cursor1]) < 0) {
|
||||
a[dest++] = a[cursor2++];
|
||||
count2++;
|
||||
count1 = 0;
|
||||
if (--len2 == 0)
|
||||
break outer;
|
||||
} else {
|
||||
a[dest++] = tmp[cursor1++];
|
||||
count1++;
|
||||
count2 = 0;
|
||||
if (--len1 == 1)
|
||||
break outer;
|
||||
}
|
||||
} while ((count1 | count2) < minGallop);
|
||||
|
||||
/*
|
||||
* One run is winning so consistently that galloping may be a
|
||||
* huge win. So try that, and continue galloping until (if ever)
|
||||
* neither run appears to be winning consistently anymore.
|
||||
*/
|
||||
do {
|
||||
assert len1 > 1 && len2 > 0;
|
||||
count1 = gallopRight(a[cursor2], tmp, cursor1, len1, 0, c);
|
||||
if (count1 != 0) {
|
||||
System.arraycopy(tmp, cursor1, a, dest, count1);
|
||||
dest += count1;
|
||||
cursor1 += count1;
|
||||
len1 -= count1;
|
||||
if (len1 <= 1) // len1 == 1 || len1 == 0
|
||||
break outer;
|
||||
}
|
||||
a[dest++] = a[cursor2++];
|
||||
if (--len2 == 0)
|
||||
break outer;
|
||||
|
||||
count2 = gallopLeft(tmp[cursor1], a, cursor2, len2, 0, c);
|
||||
if (count2 != 0) {
|
||||
System.arraycopy(a, cursor2, a, dest, count2);
|
||||
dest += count2;
|
||||
cursor2 += count2;
|
||||
len2 -= count2;
|
||||
if (len2 == 0)
|
||||
break outer;
|
||||
}
|
||||
a[dest++] = tmp[cursor1++];
|
||||
if (--len1 == 1)
|
||||
break outer;
|
||||
minGallop--;
|
||||
} while (count1 >= MIN_GALLOP | count2 >= MIN_GALLOP);
|
||||
if (minGallop < 0)
|
||||
minGallop = 0;
|
||||
minGallop += 2; // Penalize for leaving gallop mode
|
||||
} // End of "outer" loop
|
||||
this.minGallop = minGallop < 1 ? 1 : minGallop; // Write back to field
|
||||
|
||||
if (len1 == 1) {
|
||||
assert len2 > 0;
|
||||
System.arraycopy(a, cursor2, a, dest, len2);
|
||||
a[dest + len2] = tmp[cursor1]; // Last elt of run 1 to end of merge
|
||||
} else if (len1 == 0) {
|
||||
throw new IllegalArgumentException(
|
||||
"Comparison method violates its general contract!");
|
||||
} else {
|
||||
assert len2 == 0;
|
||||
assert len1 > 1;
|
||||
System.arraycopy(tmp, cursor1, a, dest, len1);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Like mergeLo, except that this method should be called only if
|
||||
* len1 >= len2; mergeLo should be called if len1 <= len2. (Either method
|
||||
* may be called if len1 == len2.)
|
||||
*
|
||||
* @param base1 index of first element in first run to be merged
|
||||
* @param len1 length of first run to be merged (must be > 0)
|
||||
* @param base2 index of first element in second run to be merged
|
||||
* (must be aBase + aLen)
|
||||
* @param len2 length of second run to be merged (must be > 0)
|
||||
*/
|
||||
private void mergeHi(int base1, int len1, int base2, int len2) {
|
||||
assert len1 > 0 && len2 > 0 && base1 + len1 == base2;
|
||||
|
||||
// Copy second run into temp array
|
||||
T[] a = this.a; // For performance
|
||||
T[] tmp = ensureCapacity(len2);
|
||||
int tmpBase = this.tmpBase;
|
||||
System.arraycopy(a, base2, tmp, tmpBase, len2);
|
||||
|
||||
int cursor1 = base1 + len1 - 1; // Indexes into a
|
||||
int cursor2 = tmpBase + len2 - 1; // Indexes into tmp array
|
||||
int dest = base2 + len2 - 1; // Indexes into a
|
||||
|
||||
// Move last element of first run and deal with degenerate cases
|
||||
a[dest--] = a[cursor1--];
|
||||
if (--len1 == 0) {
|
||||
System.arraycopy(tmp, tmpBase, a, dest - (len2 - 1), len2);
|
||||
return;
|
||||
}
|
||||
if (len2 == 1) {
|
||||
dest -= len1;
|
||||
cursor1 -= len1;
|
||||
System.arraycopy(a, cursor1 + 1, a, dest + 1, len1);
|
||||
a[dest] = tmp[cursor2];
|
||||
return;
|
||||
}
|
||||
|
||||
Comparator<? super T> c = this.c; // Use local variable for performance
|
||||
int minGallop = this.minGallop; // " " " " "
|
||||
outer:
|
||||
while (true) {
|
||||
int count1 = 0; // Number of times in a row that first run won
|
||||
int count2 = 0; // Number of times in a row that second run won
|
||||
|
||||
/*
|
||||
* Do the straightforward thing until (if ever) one run
|
||||
* appears to win consistently.
|
||||
*/
|
||||
do {
|
||||
assert len1 > 0 && len2 > 1;
|
||||
if (c.compare(tmp[cursor2], a[cursor1]) < 0) {
|
||||
a[dest--] = a[cursor1--];
|
||||
count1++;
|
||||
count2 = 0;
|
||||
if (--len1 == 0)
|
||||
break outer;
|
||||
} else {
|
||||
a[dest--] = tmp[cursor2--];
|
||||
count2++;
|
||||
count1 = 0;
|
||||
if (--len2 == 1)
|
||||
break outer;
|
||||
}
|
||||
} while ((count1 | count2) < minGallop);
|
||||
|
||||
/*
|
||||
* One run is winning so consistently that galloping may be a
|
||||
* huge win. So try that, and continue galloping until (if ever)
|
||||
* neither run appears to be winning consistently anymore.
|
||||
*/
|
||||
do {
|
||||
assert len1 > 0 && len2 > 1;
|
||||
count1 = len1 - gallopRight(tmp[cursor2], a, base1, len1, len1 - 1, c);
|
||||
if (count1 != 0) {
|
||||
dest -= count1;
|
||||
cursor1 -= count1;
|
||||
len1 -= count1;
|
||||
System.arraycopy(a, cursor1 + 1, a, dest + 1, count1);
|
||||
if (len1 == 0)
|
||||
break outer;
|
||||
}
|
||||
a[dest--] = tmp[cursor2--];
|
||||
if (--len2 == 1)
|
||||
break outer;
|
||||
|
||||
count2 = len2 - gallopLeft(a[cursor1], tmp, tmpBase, len2, len2 - 1, c);
|
||||
if (count2 != 0) {
|
||||
dest -= count2;
|
||||
cursor2 -= count2;
|
||||
len2 -= count2;
|
||||
System.arraycopy(tmp, cursor2 + 1, a, dest + 1, count2);
|
||||
if (len2 <= 1) // len2 == 1 || len2 == 0
|
||||
break outer;
|
||||
}
|
||||
a[dest--] = a[cursor1--];
|
||||
if (--len1 == 0)
|
||||
break outer;
|
||||
minGallop--;
|
||||
} while (count1 >= MIN_GALLOP | count2 >= MIN_GALLOP);
|
||||
if (minGallop < 0)
|
||||
minGallop = 0;
|
||||
minGallop += 2; // Penalize for leaving gallop mode
|
||||
} // End of "outer" loop
|
||||
this.minGallop = minGallop < 1 ? 1 : minGallop; // Write back to field
|
||||
|
||||
if (len2 == 1) {
|
||||
assert len1 > 0;
|
||||
dest -= len1;
|
||||
cursor1 -= len1;
|
||||
System.arraycopy(a, cursor1 + 1, a, dest + 1, len1);
|
||||
a[dest] = tmp[cursor2]; // Move first elt of run2 to front of merge
|
||||
} else if (len2 == 0) {
|
||||
throw new IllegalArgumentException(
|
||||
"Comparison method violates its general contract!");
|
||||
} else {
|
||||
assert len1 == 0;
|
||||
assert len2 > 0;
|
||||
System.arraycopy(tmp, tmpBase, a, dest - (len2 - 1), len2);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Ensures that the external array tmp has at least the specified
|
||||
* number of elements, increasing its size if necessary. The size
|
||||
* increases exponentially to ensure amortized linear time complexity.
|
||||
*
|
||||
* @param minCapacity the minimum required capacity of the tmp array
|
||||
* @return tmp, whether or not it grew
|
||||
*/
|
||||
private T[] ensureCapacity(int minCapacity) {
|
||||
if (tmpLen < minCapacity) {
|
||||
// Compute smallest power of 2 > minCapacity
|
||||
int newSize = -1 >>> Integer.numberOfLeadingZeros(minCapacity);
|
||||
newSize++;
|
||||
|
||||
if (newSize < 0) // Not bloody likely!
|
||||
newSize = minCapacity;
|
||||
else
|
||||
newSize = Math.min(newSize, a.length >>> 1);
|
||||
|
||||
@SuppressWarnings({"unchecked", "UnnecessaryLocalVariable"})
|
||||
T[] newArray = (T[])java.lang.reflect.Array.newInstance
|
||||
(a.getClass().getComponentType(), newSize);
|
||||
tmp = newArray;
|
||||
tmpLen = newSize;
|
||||
tmpBase = 0;
|
||||
}
|
||||
return tmp;
|
||||
}
|
||||
}
|
@ -22,14 +22,14 @@ public enum CglEnum implements DataEnum {
|
||||
int runs = 3_010;
|
||||
int runLength = 3_010;
|
||||
|
||||
int manyRuns = 30_000;
|
||||
int manyRuns = 120_000;
|
||||
int shortRunLength = 50;
|
||||
|
||||
// Constant factors
|
||||
double a = 0.96;
|
||||
double b = 0.25;
|
||||
double c = 0.81;
|
||||
double d = 1.0;
|
||||
double d = 0.85;
|
||||
|
||||
return switch (this) {
|
||||
case RANDOM_INTEGERS -> new RandomIntegers(listSize, seed);
|
||||
@ -40,7 +40,7 @@ public enum CglEnum implements DataEnum {
|
||||
AscendingRuns.newAscendingRuns((int) (c * runs), (int) (c * runLength), (int) (-0.5 * c * runLength));
|
||||
case MANY_ASCENDING_RUNS -> AscendingRuns.newAscendingRuns(manyRuns, shortRunLength, -1 * shortRunLength);
|
||||
case MANY_ASCENDING_RUNS_WITH_OVERLAP ->
|
||||
AscendingRuns.newAscendingRuns((int) (d * manyRuns), (int) (d * shortRunLength), (int) (-0.5 * d * shortRunLength));
|
||||
AscendingRuns.newAscendingRuns((int) (d * manyRuns), shortRunLength, (int) (-0.5 * shortRunLength));
|
||||
};
|
||||
}
|
||||
}
|
||||
|
@ -4,6 +4,9 @@ import de.uni_marburg.powersort.FinnSort.FinnSort;
|
||||
import de.uni_marburg.powersort.FinnSort.FasterFinnSort;
|
||||
import de.uni_marburg.powersort.MSort.IMPL_M_1;
|
||||
import de.uni_marburg.powersort.MSort.IMPL_M_2;
|
||||
import de.uni_marburg.powersort.MSort.IMPL_M_3;
|
||||
import de.uni_marburg.powersort.MSort.IMPL_M_4;
|
||||
import de.uni_marburg.powersort.MSort.IMPL_M_5;
|
||||
import de.uni_marburg.powersort.benchmark.NaturalOrder;
|
||||
import de.uni_marburg.powersort.sort.dpqs.DualPivotQuicksort;
|
||||
|
||||
@ -14,11 +17,15 @@ public enum SortEnum {
|
||||
FINN_SORT,
|
||||
IMPL_M_10,
|
||||
IMPL_M_20,
|
||||
IMPL_M_30,
|
||||
IMPL_M_40,
|
||||
IMPL_M_50,
|
||||
DPQS,
|
||||
QUICK_SORT,
|
||||
MERGE_SORT,
|
||||
BUBBLE_SORT;
|
||||
|
||||
|
||||
public SortImpl getSortImpl() {
|
||||
return switch (this) {
|
||||
case BUBBLE_SORT -> array -> BubbleSort.sort(array, NaturalOrder.INSTANCE);
|
||||
@ -28,7 +35,10 @@ public enum SortEnum {
|
||||
case TIM_SORT -> array -> TimSort.sort(array, 0, array.length, NaturalOrder.INSTANCE, null, 0, 0);
|
||||
case FINN_SORT -> array -> FinnSort.sort(array, NaturalOrder.INSTANCE);
|
||||
case IMPL_M_10 -> array -> IMPL_M_1.powerSort(array,NaturalOrder.INSTANCE);
|
||||
case IMPL_M_20 -> array -> IMPL_M_2.sort(array, 0, array.length, NaturalOrder.INSTANCE, null, 0, 0);
|
||||
case IMPL_M_20 -> array -> IMPL_M_2.powerSort(array,NaturalOrder.INSTANCE);
|
||||
case IMPL_M_30 -> array -> IMPL_M_3.powerSort(array,NaturalOrder.INSTANCE);
|
||||
case IMPL_M_40 -> array -> IMPL_M_4.sort(array, 0, array.length, NaturalOrder.INSTANCE, null, 0, 0);
|
||||
case IMPL_M_50 -> array -> IMPL_M_5.sort(array, 0, array.length, NaturalOrder.INSTANCE, null, 0, 0);
|
||||
case FASTER_FINN_SORT -> array -> FasterFinnSort.sort(array, 0, array.length, NaturalOrder.INSTANCE, null, 0, 0);
|
||||
case ASORT -> array -> ASort.sort(array, NaturalOrder.INSTANCE);
|
||||
};
|
||||
|
@ -1,6 +1,6 @@
|
||||
package de.uni_marburg.powersort.MSort;
|
||||
|
||||
import static de.uni_marburg.powersort.MSort.IMPL_M_1.*;
|
||||
import static de.uni_marburg.powersort.MSort.IMPL_M_3.*;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
@ -13,8 +13,8 @@ public class PowerSortT {
|
||||
|
||||
public static void main(String[] args) {
|
||||
testFillWithAscRunsHighToLow();
|
||||
testMerge();
|
||||
testMergeInplace();
|
||||
//testMerge();
|
||||
// testMergeInplace();
|
||||
testExtendRun();
|
||||
testPower();
|
||||
testPowerFast();
|
||||
@ -32,25 +32,25 @@ public class PowerSortT {
|
||||
}
|
||||
|
||||
// Test for merge
|
||||
public static void testMerge() {
|
||||
Integer[] run1 ={1,4,6};
|
||||
Integer []run2 = {2, 3, 5};
|
||||
Integer[] result = merge(run1, run2, NaturalOrder.INSTANCE);
|
||||
System.out.println("Test merge: " + result);
|
||||
}
|
||||
// public static void testMerge() {
|
||||
// Integer[] run1 ={1,4,6};
|
||||
// Integer []run2 = {2, 3, 5};
|
||||
// Integer[] result = merge(run1, run2, NaturalOrder.INSTANCE);
|
||||
// System.out.println("Test merge: " + result);
|
||||
// }
|
||||
|
||||
// Test for mergeInplace
|
||||
public static void testMergeInplace() {
|
||||
Integer[] A = {1,4,6,2,3,5};
|
||||
mergeInplace(A, 0, 3, 6,NaturalOrder.INSTANCE);
|
||||
System.out.println("Test mergeInplace: " + A);
|
||||
}
|
||||
// public static void testMergeInplace() {
|
||||
// Integer[] A = {1,4,6,2,3,5};
|
||||
// mergeInplace(A, 0, 3, 6,NaturalOrder.INSTANCE);
|
||||
// System.out.println("Test mergeInplace: " + A);
|
||||
// }
|
||||
|
||||
// Test for extendRun
|
||||
public static void testExtendRun() {
|
||||
Integer [] A = {1, 2, 3, 6, 5, 4};
|
||||
int endIndex = extendRun(A, 0,NaturalOrder.INSTANCE);
|
||||
System.out.println("Test extendRun (from 0): " + endIndex);
|
||||
// int endIndex = extendRun(A, 0,NaturalOrder.INSTANCE);
|
||||
// System.out.println("Test extendRun (from 0): " + endIndex);
|
||||
System.out.println("Modified List: " + A);
|
||||
}
|
||||
|
||||
@ -59,8 +59,8 @@ public class PowerSortT {
|
||||
int[] run1 = {0, 3};
|
||||
int[] run2 = {3, 3};
|
||||
int n = 6;
|
||||
int powerValue = power(run1, run2, n);
|
||||
System.out.println("Test power: " + powerValue);
|
||||
// int powerValue = power(run1, run2, n);
|
||||
// System.out.println("Test power: " + powerValue);
|
||||
}
|
||||
|
||||
// Test for powerFast
|
||||
@ -68,8 +68,8 @@ public class PowerSortT {
|
||||
int[] run1 = {0, 3};
|
||||
int[] run2 = {3, 3};
|
||||
int n = 6;
|
||||
int powerFastValue = powerFast(run1, run2, n);
|
||||
System.out.println("Test powerFast: " + powerFastValue);
|
||||
// int powerFastValue = powerFast(run1, run2, n);
|
||||
// System.out.println("Test powerFast: " + powerFastValue);
|
||||
}
|
||||
|
||||
// Test for mergeTopmost2
|
||||
@ -78,7 +78,7 @@ public class PowerSortT {
|
||||
List<int[]> runs = new ArrayList<>();
|
||||
runs.add(new int[]{0, 3, 1});
|
||||
runs.add(new int[]{3, 3, 1});
|
||||
mergeTopmost2(A, runs,NaturalOrder.INSTANCE);
|
||||
// mergeTopmost2(A, runs,NaturalOrder.INSTANCE);
|
||||
System.out.println("Test mergeTopmost2: " + A);
|
||||
}
|
||||
|
||||
|
@ -9,9 +9,9 @@ import java.util.stream.IntStream;
|
||||
import de.uni_marburg.powersort.benchmark.NaturalOrder;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static de.uni_marburg.powersort.MSort.IMPL_M_1.MERGE_COST;
|
||||
import static de.uni_marburg.powersort.MSort.IMPL_M_1.fillWithAscRunsHighToLow;
|
||||
import static de.uni_marburg.powersort.MSort.IMPL_M_1.powerSort;
|
||||
//import static de.uni_marburg.powersort.MSort.IMPL_M_3.MERGE_COST;
|
||||
import static de.uni_marburg.powersort.MSort.IMPL_M_3.fillWithAscRunsHighToLow;
|
||||
import static de.uni_marburg.powersort.MSort.IMPL_M_3.powerSort;
|
||||
|
||||
class PowerSortTest {
|
||||
@Test
|
||||
@ -28,11 +28,11 @@ class PowerSortTest {
|
||||
|
||||
System.out.println();
|
||||
fillWithAscRunsHighToLow(a, runs, 1);
|
||||
MERGE_COST = 0;
|
||||
//MERGE_COST = 0;
|
||||
System.out.println("Sorting with Powersort:");
|
||||
powerSort(a,NaturalOrder.INSTANCE);
|
||||
System.out.println("Sorted Array"+Arrays.toString(a));
|
||||
System.out.println("Merge cost: " + MERGE_COST);
|
||||
// System.out.println("Merge cost: " + MERGE_COST);
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -1,5 +1,8 @@
|
||||
package de.uni_marburg.powersort.sort;
|
||||
|
||||
import org.junit.jupiter.api.Disabled;
|
||||
|
||||
@Disabled("Disabled in favor of IMPL_M_2")
|
||||
public class IMPL_M_1Test extends AbstractSortTest {
|
||||
IMPL_M_1Test() {
|
||||
sortAlg = SortEnum.IMPL_M_10;
|
||||
|
@ -0,0 +1,10 @@
|
||||
package de.uni_marburg.powersort.sort;
|
||||
|
||||
import org.junit.jupiter.api.Disabled;
|
||||
|
||||
@Disabled("Disabled in favor of IMPL_M_3")
|
||||
public class IMPL_M_2Test extends AbstractSortTest {
|
||||
IMPL_M_2Test() {
|
||||
sortAlg = SortEnum.IMPL_M_20;
|
||||
}
|
||||
}
|
@ -0,0 +1,10 @@
|
||||
package de.uni_marburg.powersort.sort;
|
||||
|
||||
import org.junit.jupiter.api.Disabled;
|
||||
|
||||
@Disabled("Disabled in favor of IMPL_M_4")
|
||||
public class IMPL_M_3Test extends AbstractSortTest {
|
||||
IMPL_M_3Test() {
|
||||
sortAlg = SortEnum.IMPL_M_30;
|
||||
}
|
||||
}
|
@ -0,0 +1,7 @@
|
||||
package de.uni_marburg.powersort.sort;
|
||||
|
||||
public class IMPL_M_4Test extends AbstractSortTest {
|
||||
IMPL_M_4Test() {
|
||||
sortAlg = SortEnum.IMPL_M_40;
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user