mirror of
https://gitlab.uni-marburg.de/langbeid/powersort.git
synced 2025-01-21 19:50:35 +01:00
Merge remote-tracking branch 'origin/main'
# Conflicts: # .idea/misc.xml
This commit is contained in:
commit
691d7eb0ac
1
.gitignore
vendored
1
.gitignore
vendored
@ -1,4 +1,5 @@
|
||||
/.idea
|
||||
/app/bin/
|
||||
|
||||
# Ignore Gradle project-specific cache directory
|
||||
/.gradle
|
||||
|
2
.idea/misc.xml
generated
2
.idea/misc.xml
generated
@ -3,5 +3,5 @@
|
||||
<component name="FrameworkDetectionExcludesConfiguration">
|
||||
<file type="web" url="file://$PROJECT_DIR$" />
|
||||
</component>
|
||||
<component name="ProjectRootManager" version="2" languageLevel="JDK_23" project-jdk-name="23" project-jdk-type="JavaSDK" />
|
||||
<component name="ProjectRootManager" version="2" languageLevel="JDK_21" project-jdk-name="23" project-jdk-type="JavaSDK" />
|
||||
</project>
|
@ -56,9 +56,3 @@ Run the task "test":
|
||||
```shell
|
||||
./gradlew test
|
||||
```
|
||||
|
||||
## TimSort
|
||||
|
||||
Imported from
|
||||
- src/java.base/share/classes/java/util/TimSort.java
|
||||
- src/java.base/share/classes/java/util/ComparableTimSort.java
|
||||
|
@ -1,49 +0,0 @@
|
||||
package de.uni_marburg.powersort.benchmark;
|
||||
|
||||
import de.uni_marburg.powersort.sort.DummySort;
|
||||
import de.uni_marburg.powersort.sort.MergeSort;
|
||||
import de.uni_marburg.powersort.sort.TimSort;
|
||||
import de.uni_marburg.powersort.data.RandomIntegers;
|
||||
import org.openjdk.jmh.annotations.*;
|
||||
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
// TODO: The parameters are way too low. Use for debugging only!
|
||||
@Fork(1)
|
||||
@Warmup(iterations = 3)
|
||||
@Measurement(iterations = 6)
|
||||
public class BenchmarkJmh {
|
||||
@State(Scope.Benchmark)
|
||||
public static class State1 {
|
||||
RandomIntegers d = new RandomIntegers();
|
||||
Integer[] a;
|
||||
|
||||
// TODO: This is inaccurate.
|
||||
// How to create and use separate arrays for each warmup x iteration x sortAlgorithm ?
|
||||
@Setup(Level.Invocation)
|
||||
public void setup() {
|
||||
a = d.get();
|
||||
}
|
||||
}
|
||||
|
||||
@BenchmarkMode(Mode.AverageTime)
|
||||
@OutputTimeUnit(TimeUnit.MILLISECONDS)
|
||||
@Benchmark
|
||||
public void rand1DummySort(State1 s) {
|
||||
DummySort.sort(s.a, 0, s.a.length, NaturalOrder.INSTANCE, null, 0, 0);
|
||||
}
|
||||
|
||||
@BenchmarkMode(Mode.AverageTime)
|
||||
@OutputTimeUnit(TimeUnit.MILLISECONDS)
|
||||
@Benchmark
|
||||
public void rand1TimSort(State1 s) {
|
||||
TimSort.sort(s.a, 0, s.a.length, NaturalOrder.INSTANCE, null, 0, 0);
|
||||
}
|
||||
|
||||
@BenchmarkMode(Mode.AverageTime)
|
||||
@OutputTimeUnit(TimeUnit.MILLISECONDS)
|
||||
@Benchmark
|
||||
public void rand1MergeSort(State1 s) {
|
||||
MergeSort.legacyMergeSort(s.a, NaturalOrder.INSTANCE);
|
||||
}
|
||||
}
|
@ -0,0 +1,65 @@
|
||||
package de.uni_marburg.powersort.benchmark;
|
||||
|
||||
import de.uni_marburg.powersort.data.DataEnum;
|
||||
import de.uni_marburg.powersort.data.ObjectSupplier;
|
||||
import de.uni_marburg.powersort.sort.SortEnum;
|
||||
import org.openjdk.jmh.annotations.Benchmark;
|
||||
import org.openjdk.jmh.annotations.BenchmarkMode;
|
||||
import org.openjdk.jmh.annotations.Fork;
|
||||
import org.openjdk.jmh.annotations.Level;
|
||||
import org.openjdk.jmh.annotations.Measurement;
|
||||
import org.openjdk.jmh.annotations.Mode;
|
||||
import org.openjdk.jmh.annotations.OutputTimeUnit;
|
||||
import org.openjdk.jmh.annotations.Param;
|
||||
import org.openjdk.jmh.annotations.Scope;
|
||||
import org.openjdk.jmh.annotations.Setup;
|
||||
import org.openjdk.jmh.annotations.State;
|
||||
import org.openjdk.jmh.annotations.Warmup;
|
||||
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
// TODO: The parameters are way too low. Use for debugging only!
|
||||
/*
|
||||
* Benchmark parameters
|
||||
*/
|
||||
@Fork(0)
|
||||
@Warmup(iterations = 0)
|
||||
@Measurement(iterations = 1)
|
||||
@BenchmarkMode(Mode.AverageTime)
|
||||
@OutputTimeUnit(TimeUnit.MILLISECONDS)
|
||||
/*
|
||||
* Benchmark state parameters
|
||||
*
|
||||
* Quote from JMH:
|
||||
* State objects naturally encapsulate the state on which benchmark is working on.
|
||||
*/
|
||||
@State(Scope.Benchmark)
|
||||
public class MainJmh {
|
||||
@Param()
|
||||
private DataEnum dataEnum;
|
||||
@Param()
|
||||
private SortEnum sortEnum;
|
||||
|
||||
private ObjectSupplier data;
|
||||
/* package-protected */ Object[] workingCopy;
|
||||
|
||||
// TODO: This is inaccurate. How to create and use separate arrays for each warmup x iteration x sortAlgorithm ?
|
||||
@Setup(Level.Invocation)
|
||||
public void setup() {
|
||||
// A new MainJmh object is created for each @Param variation.
|
||||
// Then, `data` is `null` again.
|
||||
if (data == null) {
|
||||
data = dataEnum.getObjectSupplier();
|
||||
}
|
||||
// For all warmup and measurement iterations of one @Param variation, the MainJmh object is reused.
|
||||
// Thus, we can't just sort `data` directly.
|
||||
// Instead, we have to create a copy of it on which the sort algorithm can work.
|
||||
// This way, all iterations sort the same input.
|
||||
workingCopy = data.getCopy();
|
||||
}
|
||||
|
||||
@Benchmark
|
||||
public void benchmark() {
|
||||
sortEnum.getSortImpl().sort(workingCopy);
|
||||
}
|
||||
}
|
@ -1,4 +1,7 @@
|
||||
package de.uni_marburg.powersort.benchmark;
|
||||
package de.uni_marburg.powersort;
|
||||
|
||||
import de.uni_marburg.powersort.benchmark.DummyComparable1;
|
||||
import de.uni_marburg.powersort.benchmark.NaturalOrder;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
@ -0,0 +1,960 @@
|
||||
package de.uni_marburg.powersort.FinnSort;
|
||||
/*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
|
||||
import java.util.Comparator;
|
||||
|
||||
/**
|
||||
* 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
|
||||
*/
|
||||
class FasterFinnSort<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 = 32;
|
||||
|
||||
/**
|
||||
* 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 = 256;
|
||||
|
||||
/**
|
||||
* 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;
|
||||
|
||||
/**
|
||||
* 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
|
||||
*/
|
||||
private FasterFinnSort(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)
|
||||
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
|
||||
*/
|
||||
int stackLen = (len < 120 ? 5 :
|
||||
len < 1542 ? 10 :
|
||||
len < 119151 ? 24 : 49);
|
||||
runBase = new int[stackLen];
|
||||
runLen = new int[stackLen];
|
||||
}
|
||||
|
||||
/*
|
||||
* 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
|
||||
*/
|
||||
static <T> void sort(T[] a, int lo, int hi, Comparator<? super T> c,
|
||||
T[] work, int workBase, int workLen) {
|
||||
assert c != null && a != null && lo >= 0 && lo <= hi && hi <= a.length;
|
||||
|
||||
int nRemaining = hi - lo;
|
||||
if (nRemaining < 2)
|
||||
return; // Arrays of size 0 and 1 are always sorted
|
||||
|
||||
// If array is small, do a "mini-TimSort" with no merges
|
||||
if (nRemaining < MIN_MERGE) {
|
||||
int initRunLen = countRunAndMakeAscending(a, lo, hi, c);
|
||||
binarySort(a, lo, hi, lo + initRunLen, c);
|
||||
return;
|
||||
}
|
||||
|
||||
/**
|
||||
* March over the array once, left to right, finding natural runs,
|
||||
* extending short natural runs to minRun elements, and merging runs
|
||||
* to maintain stack invariant.
|
||||
*/
|
||||
FasterFinnSort<T> ts = new FasterFinnSort<>(a, c, work, workBase, workLen);
|
||||
int minRun = minRunLength(nRemaining);
|
||||
do {
|
||||
// Identify next run
|
||||
int runLen = countRunAndMakeAscending(a, lo, hi, c);
|
||||
|
||||
// If run is short, extend to min(minRun, nRemaining)
|
||||
if (runLen < minRun) {
|
||||
int force = nRemaining <= minRun ? nRemaining : minRun;
|
||||
binarySort(a, lo, lo + force, lo + runLen, c);
|
||||
runLen = force;
|
||||
}
|
||||
|
||||
// Push run onto pending-run stack, and maybe merge
|
||||
ts.pushRun(lo, runLen);
|
||||
ts.mergeCollapse();
|
||||
|
||||
// Advance to find next run
|
||||
lo += runLen;
|
||||
nRemaining -= runLen;
|
||||
} while (nRemaining != 0);
|
||||
|
||||
// Merge all remaining runs to complete sort
|
||||
assert lo == hi;
|
||||
ts.mergeForceCollapse();
|
||||
assert ts.stackSize == 1;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 static <T> int countRunAndMakeAscending(T[] a, int lo, int hi,
|
||||
Comparator<? super T> c) {
|
||||
assert lo < hi;
|
||||
int runHi = lo + 1;
|
||||
if (runHi == hi)
|
||||
return 1;
|
||||
|
||||
// Find end of run, and reverse range if descending
|
||||
if (c.compare(a[runHi++], a[lo]) < 0) { // Descending
|
||||
while (runHi < hi && c.compare(a[runHi], a[runHi - 1]) < 0)
|
||||
runHi++;
|
||||
reverseRange(a, lo, runHi);
|
||||
} else { // Ascending
|
||||
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 static void reverseRange(Object[] a, int lo, int hi) {
|
||||
hi--;
|
||||
while (lo < hi) {
|
||||
Object 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 static int minRunLength(int n) {
|
||||
assert n >= 0;
|
||||
int r = 0; // Becomes 1 if any 1 bits are shifted off
|
||||
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) {
|
||||
this.runBase[stackSize] = runBase;
|
||||
this.runLen[stackSize] = runLen;
|
||||
stackSize++;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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] ||
|
||||
n > 1 && runLen[n-2] <= runLen[n] + runLen[n-1]) {
|
||||
if (runLen[n - 1] < runLen[n + 1])
|
||||
n--;
|
||||
} else if (n < 0 || runLen[n] > runLen[n + 1]) {
|
||||
break; // Invariant is established
|
||||
}
|
||||
mergeAt(n);
|
||||
}
|
||||
}
|
||||
/*
|
||||
Backup mergeCollapse() von TimSort:
|
||||
|
||||
private void mergeCollapse() {
|
||||
while (stackSize > 1) {
|
||||
int n = stackSize - 2;
|
||||
if (n > 0 && runLen[n-1] <= runLen[n] + runLen[n+1] ||
|
||||
n > 1 && runLen[n-2] <= runLen[n] + runLen[n-1]) {
|
||||
if (runLen[n - 1] < runLen[n + 1])
|
||||
n--;
|
||||
} else if (n < 0 || runLen[n] > runLen[n + 1]) {
|
||||
break; // Invariant is established
|
||||
}
|
||||
mergeAt(n);
|
||||
}
|
||||
}
|
||||
|
||||
*/
|
||||
|
||||
/**
|
||||
* 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;
|
||||
|
||||
/*
|
||||
* 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;
|
||||
if (i == stackSize - 3) {
|
||||
runBase[i + 1] = runBase[i + 2];
|
||||
runLen[i + 1] = runLen[i + 2];
|
||||
}
|
||||
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) {
|
||||
assert len > 0 && hint >= 0 && hint < len;
|
||||
int lastOfs = 0;
|
||||
int ofs = 1;
|
||||
if (c.compare(key, a[base + hint]) > 0) {
|
||||
// Gallop right until a[base+hint+lastOfs] < key <= a[base+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 base
|
||||
lastOfs += hint;
|
||||
ofs += hint;
|
||||
} else { // key <= a[base + hint]
|
||||
// Gallop left until a[base+hint-ofs] < key <= a[base+hint-lastOfs]
|
||||
final 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 base
|
||||
int tmp = lastOfs;
|
||||
lastOfs = hint - ofs;
|
||||
ofs = hint - tmp;
|
||||
}
|
||||
assert -1 <= lastOfs && lastOfs < ofs && ofs <= len;
|
||||
|
||||
/*
|
||||
* Now a[base+lastOfs] < key <= a[base+ofs], so key belongs somewhere
|
||||
* to the right of lastOfs but no farther right than ofs. Do a binary
|
||||
* search, with invariant a[base + lastOfs - 1] < key <= a[base + ofs].
|
||||
*/
|
||||
lastOfs++;
|
||||
while (lastOfs < ofs) {
|
||||
int m = lastOfs + ((ofs - lastOfs) >>> 1);
|
||||
|
||||
if (c.compare(key, a[base + m]) > 0)
|
||||
lastOfs = m + 1; // a[base + m] < key
|
||||
else
|
||||
ofs = m; // key <= a[base + m]
|
||||
}
|
||||
assert lastOfs == ofs; // so a[base + ofs - 1] < key <= a[base + ofs]
|
||||
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;
|
||||
}
|
||||
}
|
@ -5,60 +5,70 @@ import static java.lang.Math.*;
|
||||
|
||||
public class FinnSort {
|
||||
|
||||
private static final ArrayList<Run> runs = new ArrayList<>();
|
||||
private static ArrayList<Run> runs;
|
||||
|
||||
static void sort(Integer[] a) {
|
||||
public static <T> void sort(T[] a, Comparator<? super T> c) {
|
||||
|
||||
runs = new ArrayList<>();
|
||||
|
||||
int n = a.length;
|
||||
int i = 0;
|
||||
int j = extendRunRight(a, i);
|
||||
int j = extendRunRight(a, i, c);
|
||||
|
||||
printList(a);
|
||||
printList(a, c);
|
||||
|
||||
runs.add(new Run(i, j, 0));
|
||||
|
||||
i = j;
|
||||
while (i < n) {
|
||||
j = extendRunRight(a, i);
|
||||
j = extendRunRight(a, i, c);
|
||||
|
||||
//printRuns();
|
||||
|
||||
int p = power(runs.getLast(), new Run(i, j, 0), n);
|
||||
|
||||
while (runs.size() >= 2 && p < power(runs.getLast(), runs.get(runs.size() - 2), n)) {
|
||||
basicMerge(a, runs.removeFirst(), runs.removeFirst());
|
||||
basicMerge(a, runs.removeLast(), runs.removeLast(), c);
|
||||
}
|
||||
|
||||
runs.add(new Run(i, j, p));
|
||||
i = j;
|
||||
}
|
||||
while (runs.size() >= 2) {
|
||||
basicMerge(a, runs.removeLast(), runs.removeLast());
|
||||
basicMerge(a, runs.removeLast(), runs.removeLast(), c);
|
||||
}
|
||||
}
|
||||
|
||||
private static void basicMerge(Integer[] a, Run r1, Run r2) {
|
||||
ArrayList<Integer> run1 = new ArrayList<>(Arrays.asList(a).subList(r1.start, r1.end));
|
||||
ArrayList<Integer> run2 = new ArrayList<>(Arrays.asList(a).subList(r2.start, r2.end));
|
||||
ArrayList<Integer> merge = new ArrayList<>();
|
||||
private static <T> void basicMerge(T[] a, Run r1, Run r2, Comparator<? super T> c) {
|
||||
ArrayList<T> run1 = new ArrayList<>(Arrays.asList(a).subList(r1.start, r1.end));
|
||||
ArrayList<T> run2 = new ArrayList<>(Arrays.asList(a).subList(r2.start, r2.end));
|
||||
ArrayList<T> merge = new ArrayList<>();
|
||||
|
||||
while (!run1.isEmpty() || !run2.isEmpty()) {
|
||||
if (run2.isEmpty() || !run1.isEmpty() && run1.getFirst() < run2.getFirst()) {
|
||||
while (!run1.isEmpty() && !run2.isEmpty()) {
|
||||
if (c.compare(run1.getFirst(), run2.getFirst()) < 0) {
|
||||
merge.add(run1.removeFirst());
|
||||
} else {
|
||||
merge.add(run2.removeFirst());
|
||||
}
|
||||
}
|
||||
|
||||
while (!run1.isEmpty()) {
|
||||
merge.add(run1.removeFirst());
|
||||
}
|
||||
|
||||
while (!run2.isEmpty()) {
|
||||
merge.add(run2.removeFirst());
|
||||
}
|
||||
|
||||
System.arraycopy(merge.toArray(), 0, a, min(r1.start, r2.start), merge.size());
|
||||
Run r = new Run(min(r1.start, r2.start), max(r1.end, r2.end), min(r1.power, r2.power));
|
||||
runs.add(r);
|
||||
printList(a);
|
||||
printList(a, c);
|
||||
}
|
||||
|
||||
private static int extendRunRight(Integer[] a, int i) {
|
||||
private static <T> int extendRunRight(T[] a, int i, Comparator<? super T> c) {
|
||||
int j = i + 1;
|
||||
while (j < a.length && a[j-1] <= a[j]) {
|
||||
while (j < a.length && c.compare(a[j-1], a[j]) <= 0) {
|
||||
j++;
|
||||
}
|
||||
return j;
|
||||
@ -87,12 +97,12 @@ public class FinnSort {
|
||||
}
|
||||
System.out.println(s);
|
||||
}
|
||||
public static void printList(Integer[] arr) {
|
||||
public static <T> void printList(T[] arr, Comparator<? super T> c) {
|
||||
String s = "";
|
||||
int i = 0;
|
||||
while (i < arr.length) {
|
||||
String run = "[";
|
||||
int j = extendRunRight(arr, i);
|
||||
int j = extendRunRight(arr, i, c);
|
||||
for (int t = i; t < j; t++) {
|
||||
run += arr[t] + ", ";
|
||||
}
|
||||
@ -101,13 +111,4 @@ public class FinnSort {
|
||||
}
|
||||
System.out.println(s);
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
Integer[] numbers = new Integer[]{24, 25, 26, 27, 28, 21, 22, 23, 18, 19, 20, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 3, 1, 2};
|
||||
|
||||
|
||||
sort(numbers);
|
||||
System.out.println("Result: ");
|
||||
System.out.println(new ArrayList<>(List.of(numbers)));
|
||||
}
|
||||
}
|
@ -3,7 +3,7 @@ package de.uni_marburg.powersort.benchmark;
|
||||
/**
|
||||
* A class for tiny, comparable objects.
|
||||
*/
|
||||
record DummyComparable1(int id) implements Comparable<DummyComparable1> {
|
||||
public record DummyComparable1(int id) implements Comparable<DummyComparable1> {
|
||||
@Override
|
||||
public int compareTo(DummyComparable1 other) {
|
||||
return id - other.id;
|
||||
|
@ -4,32 +4,44 @@ public class IntegerArray {
|
||||
private IntegerArray() {
|
||||
}
|
||||
|
||||
public static Integer[] random(final int length) {
|
||||
return random(length, Integer.MIN_VALUE, Integer.MAX_VALUE);
|
||||
}
|
||||
|
||||
public static Integer[] random(final int length, final int minInt, final int maxInt) {
|
||||
public static Integer[] random(final int length, final long seed) {
|
||||
final Integer[] list = new Integer[length];
|
||||
for (int i = 0; i < length; i++) {
|
||||
list[i] = RandomInt.integer(minInt, maxInt);
|
||||
list[i] = RandomInt.integer(seed);
|
||||
}
|
||||
return list;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return [start, start-1, ..., end+1, end]
|
||||
* @return [high, high-1, ..., low+1, low]
|
||||
*/
|
||||
public static Integer[] descending(final int start, final int end) {
|
||||
assert start > end;
|
||||
public static Integer[] descending(final int high, final int low) {
|
||||
assert high >= low;
|
||||
|
||||
Integer[] list = new Integer[start - end + 1];
|
||||
Integer[] list = new Integer[high - low + 1];
|
||||
for (int i = 0; i < list.length; i++) {
|
||||
int value = start - i;
|
||||
int value = high - i;
|
||||
list[i] = value;
|
||||
}
|
||||
|
||||
assert list[0] == start;
|
||||
assert list[list.length - 1] == end;
|
||||
assert list[0] == high;
|
||||
assert list[list.length - 1] == low;
|
||||
return list;
|
||||
}
|
||||
/**
|
||||
* @return [low, low-1, ..., high+1, high]
|
||||
*/
|
||||
public static Integer[] ascending(final int low, final int high) {
|
||||
assert low <= high;
|
||||
|
||||
Integer[] list = new Integer[high - low + 1];
|
||||
for (int i = 0; i < list.length; i++) {
|
||||
int value = low + i;
|
||||
list[i] = value;
|
||||
}
|
||||
|
||||
assert list[0] == low;
|
||||
assert list[list.length - 1] == high;
|
||||
return list;
|
||||
}
|
||||
}
|
||||
|
@ -1,80 +1,50 @@
|
||||
package de.uni_marburg.powersort.benchmark;
|
||||
|
||||
import de.uni_marburg.powersort.sort.DummySort;
|
||||
import de.uni_marburg.powersort.sort.MergeSort;
|
||||
import de.uni_marburg.powersort.sort.TimSort;
|
||||
import de.uni_marburg.powersort.data.DescendingIntegers;
|
||||
import de.uni_marburg.powersort.data.RandomIntegers;
|
||||
import de.uni_marburg.powersort.data.DataEnum;
|
||||
import de.uni_marburg.powersort.sort.SortEnum;
|
||||
import de.uni_marburg.powersort.data.ObjectSupplier;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.EnumSet;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
/**
|
||||
* Custom benchmark.
|
||||
*/
|
||||
public class Main {
|
||||
public static void main(final String[] args) {
|
||||
final SortImpl[] sortImplementations = getSortImplementations();
|
||||
final List<Supplier<ObjectSupplier>> sortInputSuppliers = getSortInputSuppliers();
|
||||
final EnumSet<SortEnum> sortImplementations = getSortImplementations();
|
||||
final EnumSet<DataEnum> dataEnums = getSortInputSuppliers();
|
||||
|
||||
for (Supplier<ObjectSupplier> sortInputSupplier : sortInputSuppliers) {
|
||||
ObjectSupplier objectSupplier = sortInputSupplier.get();
|
||||
System.out.println("\n" + objectSupplier.title());
|
||||
for (DataEnum dataEnum : dataEnums) {
|
||||
ObjectSupplier objectSupplier = dataEnum.getObjectSupplier();
|
||||
System.out.println(dataEnum);
|
||||
|
||||
for (SortImpl sortImplementation : sortImplementations) {
|
||||
Object[] sortInput = objectSupplier.get();
|
||||
for (SortEnum sortImplementation : sortImplementations) {
|
||||
Object[] sortInput = objectSupplier.getCopy();
|
||||
|
||||
// TODO: JVM warmup!
|
||||
final long startNanos = System.nanoTime();
|
||||
sortImplementation.sort(sortInput);
|
||||
sortImplementation.getSortImpl().sort(sortInput);
|
||||
final long stopNanos = System.nanoTime();
|
||||
|
||||
final long durNanos = stopNanos - startNanos;
|
||||
final long durMillis = TimeUnit.NANOSECONDS.toMillis(durNanos);
|
||||
final String durFormatted = LongFormatter.formatUnderscore(durMillis);
|
||||
System.out.println(durFormatted + "," + sortImplementation.title);
|
||||
System.out.println(durFormatted + "," + sortImplementation);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static SortImpl[] getSortImplementations() {
|
||||
return new SortImpl[]{
|
||||
new SortImpl("DummySort") {
|
||||
@Override
|
||||
public void sort(Object[] a) {
|
||||
DummySort.sort(a, 0, a.length, NaturalOrder.INSTANCE, null, 0, 0);
|
||||
}
|
||||
},
|
||||
new SortImpl("TimSort") {
|
||||
@Override
|
||||
public void sort(Object[] a) {
|
||||
TimSort.sort(a, 0, a.length, NaturalOrder.INSTANCE, null, 0, 0);
|
||||
}
|
||||
},
|
||||
new SortImpl("MergeSort") {
|
||||
@Override
|
||||
public void sort(Object[] a) {
|
||||
MergeSort.legacyMergeSort(a, NaturalOrder.INSTANCE);
|
||||
}
|
||||
}
|
||||
};
|
||||
static EnumSet<SortEnum> getSortImplementations() {
|
||||
return EnumSet.allOf(SortEnum.class);
|
||||
}
|
||||
|
||||
/**
|
||||
* The returned ObjectSupplier objects are wrapped by Supplier objects.
|
||||
* This way they are lazily created on their first access.
|
||||
* The returned ObjectSupplier objects are wrapped by DataEnum objects.
|
||||
* This way they are lazily created on their first access with DataEnum.get().
|
||||
* This saves memory if we work with large lists.
|
||||
*/
|
||||
static List<Supplier<ObjectSupplier>> getSortInputSuppliers() {
|
||||
return Arrays.asList(
|
||||
// Three different random lists.
|
||||
RandomIntegers::new,
|
||||
RandomIntegers::new,
|
||||
RandomIntegers::new,
|
||||
// One descending list.
|
||||
DescendingIntegers::new
|
||||
);
|
||||
static EnumSet<DataEnum> getSortInputSuppliers() {
|
||||
return EnumSet.allOf(DataEnum.class);
|
||||
}
|
||||
}
|
||||
|
@ -5,11 +5,11 @@ import java.util.Comparator;
|
||||
/**
|
||||
* Copied from JDK23 Arrays.java
|
||||
*/
|
||||
final class NaturalOrder implements Comparator<Object> {
|
||||
public final class NaturalOrder implements Comparator<Object> {
|
||||
@SuppressWarnings("unchecked")
|
||||
public int compare(Object first, Object second) {
|
||||
return ((Comparable<Object>) first).compareTo(second);
|
||||
}
|
||||
|
||||
static final NaturalOrder INSTANCE = new NaturalOrder();
|
||||
public static final NaturalOrder INSTANCE = new NaturalOrder();
|
||||
}
|
||||
|
@ -1,5 +1,7 @@
|
||||
package de.uni_marburg.powersort.benchmark;
|
||||
|
||||
import java.util.Random;
|
||||
|
||||
/**
|
||||
* Provides utility methods related to random integers.
|
||||
*/
|
||||
@ -8,15 +10,33 @@ public final class RandomInt {
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a random integer.
|
||||
*
|
||||
* @return A random integer.
|
||||
*/
|
||||
public static int integer() {
|
||||
return integer(Integer.MIN_VALUE, Integer.MAX_VALUE);
|
||||
}
|
||||
public static int integer(final int minInt, final int maxInt) {
|
||||
final double random = Math.random() * (maxInt - minInt) - minInt;
|
||||
return (int) Math.round(random);
|
||||
final double rand01 = Math.random();
|
||||
return helper(minInt, maxInt, rand01);
|
||||
}
|
||||
|
||||
/**
|
||||
* The returned random integer to return is determined by the given seed.
|
||||
*
|
||||
* @return A random integer.
|
||||
*/
|
||||
public static int integer(final long seed) {
|
||||
Random random = new Random(seed);
|
||||
final double rand01 = random.nextDouble();
|
||||
return helper(Integer.MIN_VALUE, Integer.MAX_VALUE, rand01);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @param rand01 Random value in range [0,1)
|
||||
*/
|
||||
private static int helper(final int minInt, final int maxInt, final double rand01) {
|
||||
final double randMinMax = rand01 * (maxInt - minInt) - minInt;
|
||||
return (int) Math.round(randMinMax);
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,9 @@
|
||||
package de.uni_marburg.powersort.data;
|
||||
|
||||
import de.uni_marburg.powersort.benchmark.IntegerArray;
|
||||
|
||||
public class AscendingIntegers extends IntegerSupplier {
|
||||
public AscendingIntegers(int size) {
|
||||
super(IntegerArray.ascending( 1, size));
|
||||
}
|
||||
}
|
@ -0,0 +1,35 @@
|
||||
package de.uni_marburg.powersort.data;
|
||||
|
||||
import de.uni_marburg.powersort.benchmark.IntegerArray;
|
||||
|
||||
public class AscendingRuns extends IntegerSupplier {
|
||||
/**
|
||||
* Can be used e.g. to construct this array:
|
||||
* [0, 1, 2, 3, -2, -1, 0, 1, -4, -3, -2, -1]
|
||||
*
|
||||
* @param runLength >= 1
|
||||
*/
|
||||
public static AscendingRuns newAscendingRuns(
|
||||
int numOfRuns,
|
||||
int runLength,
|
||||
int decreaseBetweenRuns
|
||||
) {
|
||||
if(numOfRuns < 0 || runLength <= 0){
|
||||
throw new IllegalArgumentException();
|
||||
}
|
||||
|
||||
Integer[] data = new Integer[numOfRuns * runLength];
|
||||
for (int i = 0; i < numOfRuns; i++) {
|
||||
int low = decreaseBetweenRuns * i;
|
||||
int high = low + runLength - 1;
|
||||
|
||||
Integer[] run = IntegerArray.ascending(low, high);
|
||||
System.arraycopy(run, 0, data, i * runLength, run.length);
|
||||
}
|
||||
return new AscendingRuns(data);
|
||||
}
|
||||
|
||||
private AscendingRuns(Integer[] readonly) {
|
||||
super(readonly);
|
||||
}
|
||||
}
|
@ -0,0 +1,26 @@
|
||||
package de.uni_marburg.powersort.data;
|
||||
|
||||
public enum DataEnum {
|
||||
RANDOM_INTEGERS,
|
||||
ASCENDING_INTEGERS,
|
||||
DESCENDING_INTEGERS,
|
||||
ASCENDING_RUNS,
|
||||
ASCENDING_RUNS_WITH_OVERLAP;
|
||||
|
||||
public ObjectSupplier getObjectSupplier() {
|
||||
// We use a seed to get the same random list every time -> Repeatable benchmarks on same input data!
|
||||
// final long seed = 3651660232967549736L; // System.nanoTime() ++ Math.random()
|
||||
final long seed = 140506881906827520L; // (long) 'P' * (long) 'O' *(long) 'W' * (long) 'E' * (long) 'R' * (long) 'S' * (long) 'O' * (long) 'R' * (long) 'T';
|
||||
|
||||
int longListSize = 50_000_000;
|
||||
|
||||
return switch (this) {
|
||||
case RANDOM_INTEGERS -> new RandomIntegers(longListSize, seed);
|
||||
case ASCENDING_INTEGERS -> new AscendingIntegers(longListSize);
|
||||
case DESCENDING_INTEGERS -> new DescendingIntegers(longListSize);
|
||||
|
||||
case ASCENDING_RUNS -> AscendingRuns.newAscendingRuns(10_000, 10_000, -10_000);
|
||||
case ASCENDING_RUNS_WITH_OVERLAP -> AscendingRuns.newAscendingRuns(10_000, 10_000, -5_000);
|
||||
};
|
||||
}
|
||||
}
|
@ -1,17 +1,9 @@
|
||||
package de.uni_marburg.powersort.data;
|
||||
|
||||
import de.uni_marburg.powersort.benchmark.IntegerArray;
|
||||
import de.uni_marburg.powersort.benchmark.LongFormatter;
|
||||
|
||||
public class DescendingIntegers extends IntegerSupplier {
|
||||
private static final int SIZE = 50_000_000;
|
||||
|
||||
public DescendingIntegers() {
|
||||
super(IntegerArray.descending(SIZE, 0));
|
||||
}
|
||||
|
||||
@Override
|
||||
public String title() {
|
||||
return "Array of " + LongFormatter.formatUnderscore(SIZE) + " descending Integer objects.";
|
||||
public DescendingIntegers(int size) {
|
||||
super(IntegerArray.descending(size, 1));
|
||||
}
|
||||
}
|
||||
|
@ -6,7 +6,7 @@ public abstract class IntegerSupplier extends ObjectSupplier {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Integer[] get() {
|
||||
return (Integer[]) super.get();
|
||||
public Integer[] getCopy() {
|
||||
return (Integer[]) super.getCopy();
|
||||
}
|
||||
}
|
||||
|
@ -3,21 +3,16 @@ package de.uni_marburg.powersort.data;
|
||||
import java.util.Arrays;
|
||||
|
||||
public abstract class ObjectSupplier {
|
||||
final Object[] readOnly;
|
||||
/* package-protected */ final Object[] readOnly;
|
||||
|
||||
ObjectSupplier(Object[] readOnly) {
|
||||
this.readOnly = readOnly;
|
||||
}
|
||||
|
||||
/**
|
||||
* Descriptive title for the array of objects represented by this object.
|
||||
*/
|
||||
public abstract String title();
|
||||
|
||||
/**
|
||||
* @return A fresh copy of the array of objects represented by this object.
|
||||
*/
|
||||
public Object[] get(){
|
||||
public Object[] getCopy(){
|
||||
return Arrays.copyOf(readOnly, readOnly.length);
|
||||
}
|
||||
}
|
||||
|
@ -1,17 +1,9 @@
|
||||
package de.uni_marburg.powersort.data;
|
||||
|
||||
import de.uni_marburg.powersort.benchmark.IntegerArray;
|
||||
import de.uni_marburg.powersort.benchmark.LongFormatter;
|
||||
|
||||
public class RandomIntegers extends IntegerSupplier {
|
||||
private static final int SIZE = 50_000_000;
|
||||
|
||||
public RandomIntegers() {
|
||||
super(IntegerArray.random(SIZE));
|
||||
}
|
||||
|
||||
@Override
|
||||
public String title() {
|
||||
return "Array of " + LongFormatter.formatUnderscore(SIZE) + " random Integer objects.";
|
||||
public RandomIntegers(final int size, final long seed) {
|
||||
super(IntegerArray.random(size, seed));
|
||||
}
|
||||
}
|
||||
|
298
app/src/main/java/de/uni_marburg/powersort/msort/IMPL_M_1.java
Normal file
298
app/src/main/java/de/uni_marburg/powersort/msort/IMPL_M_1.java
Normal file
@ -0,0 +1,298 @@
|
||||
package de.uni_marburg.powersort.msort;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.stream.IntStream;
|
||||
|
||||
public class IMPL_M_1 {
|
||||
|
||||
|
||||
|
||||
private IMPL_M_1() {}
|
||||
|
||||
/**
|
||||
* 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
|
||||
*/
|
||||
protected static int MERGE_COST = 0;
|
||||
|
||||
// Example usage
|
||||
// int[] runs = new int[] {5, 3, 3, 14, 1, 2}; // example from slides
|
||||
// //runs = new int[]{9, 16, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7};
|
||||
//
|
||||
// ArrayList<Integer> a = new ArrayList<>(IntStream.range(0, Arrays.stream(runs).sum()).boxed().collect(Collectors.toList()));
|
||||
//
|
||||
// System.out.println();
|
||||
// fillWithAscRunsHighToLow(a, runs);
|
||||
// MERGE_COST = 0;
|
||||
// System.out.println("Sorting with Powersort:");
|
||||
// powersort(a, this::extendRunIncreasingOnly);
|
||||
// System.out.println("Merge cost: " + MERGE_COST);
|
||||
|
||||
// runs = [5,3,3,14,1,2];
|
||||
// runs = [9,16,7,7,7,7,7,7,7,7,7,7];
|
||||
//
|
||||
// a = list(range(sum(runs)));
|
||||
// fill_with_asc_runs_high_to_low(a, runs);
|
||||
// MERGE_COST = 0;
|
||||
// System.out.println("Sorting with Powersort:");
|
||||
// powersort(a, extendRunIncreasingOnly);
|
||||
// System.out.println("Merge cost: " + MERGE_COST);
|
||||
|
||||
|
||||
public static void fillWithAscRunsHighToLow(List<Integer> A, int[] runLengths, int runLenFactor) {
|
||||
int n = A.size();
|
||||
assert IntStream.of(runLengths).sum() * runLenFactor == n;
|
||||
|
||||
for (int i = 0; i < n; i++) {
|
||||
A.set(i, n - i);
|
||||
}
|
||||
|
||||
int i = 0;
|
||||
for (int l : runLengths) {
|
||||
int L = l * runLenFactor;
|
||||
List<Integer> sublist = A.subList(i, i + L);
|
||||
Collections.sort(sublist);
|
||||
i += L;
|
||||
}
|
||||
}
|
||||
|
||||
private static List<Integer> merge(List<Integer> run1, List<Integer> run2) {
|
||||
List<Integer> result = new ArrayList<>();
|
||||
while (!run1.isEmpty() && !run2.isEmpty()) {
|
||||
if (run1.get(0) < run2.get(0)) {
|
||||
result.add(run1.remove(0));
|
||||
} else {
|
||||
result.add(run2.remove(0));
|
||||
}
|
||||
}
|
||||
result.addAll(run1);
|
||||
result.addAll(run2);
|
||||
return result;
|
||||
}
|
||||
|
||||
private static void mergeInplace(List<Integer> a, int i, int m, int j) {
|
||||
System.out.printf("Merge(%d, %d, %d)%n", i, m, j);
|
||||
MERGE_COST += j - i;
|
||||
List<Integer> sublist = merge(
|
||||
new ArrayList<>(a.subList(i, m)),
|
||||
new ArrayList<>(a.subList(m, j))
|
||||
);
|
||||
for (int k = 0; k < sublist.size(); k++) {
|
||||
a.set(i + k, sublist.get(k));
|
||||
}
|
||||
}
|
||||
|
||||
static int extendRun(List<Integer> a, int i) {
|
||||
if (i == a.size() - 1) {
|
||||
return i + 1;
|
||||
}
|
||||
int j = i + 1;
|
||||
if (a.get(i) <= a.get(j)) {
|
||||
while (j < a.size() && a.get(j - 1) <= a.get(j)) {
|
||||
j++;
|
||||
}
|
||||
} else {
|
||||
while (j < a.size() && a.get(j - 1) > a.get(j)) {
|
||||
j++;
|
||||
}
|
||||
List<Integer> sublist = a.subList(i, j);
|
||||
Collections.reverse(sublist);
|
||||
}
|
||||
return j;
|
||||
}
|
||||
|
||||
private static int extendRunIncreasingOnly(List<Integer> a, int i) {
|
||||
if (i == a.size() - 1) {
|
||||
return i + 1;
|
||||
}
|
||||
int j = i + 1;
|
||||
while (j < a.size() && a.get(j - 1) <= a.get(j)) {
|
||||
j++;
|
||||
}
|
||||
return j;
|
||||
}
|
||||
|
||||
public static int power(int[] run1, int[] run2, int n) {
|
||||
int i1 = run1[0], n1 = run1[1];
|
||||
int i2 = run2[0], n2 = run2[1];
|
||||
|
||||
assert i1 >= 0;
|
||||
assert i2 == i1 + n1;
|
||||
assert n1 >= 1 && n2 >= 1;
|
||||
assert i2 + n2 <= n;
|
||||
|
||||
double a = ((i1 + n1 / 2.0d) / n);
|
||||
double b = ((i2 + n2 / 2.0d) / n);
|
||||
|
||||
int l = 0;
|
||||
while (Math.floor(a * Math.pow(2, l)) == Math.floor(b * Math.pow(2, l))) {
|
||||
l++;
|
||||
}
|
||||
return l;
|
||||
}
|
||||
|
||||
public static int powerFast(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) {
|
||||
assert b >= a;
|
||||
a -= n;
|
||||
b -= n;
|
||||
} else if (b >= n) {
|
||||
break;
|
||||
}
|
||||
assert a < b && b < n;
|
||||
a <<= 1;
|
||||
b <<= 1;
|
||||
}
|
||||
return l;
|
||||
}
|
||||
|
||||
public static void mergeTopmost2(List<Integer> a, List<int[]> runs) {
|
||||
assert runs.size() >= 2;
|
||||
|
||||
int[] Y = runs.get(runs.size() - 2);
|
||||
int[] Z = runs.get(runs.size() - 1);
|
||||
|
||||
assert Z[0] == Y[0] + Y[1];
|
||||
|
||||
mergeInplace(a, Y[0], Z[0], Z[0] + Z[1]);
|
||||
|
||||
runs.set(runs.size() - 2, new int[] {Y[0], Y[1] + Z[1], Y[2]});
|
||||
runs.remove(runs.size() - 1);
|
||||
}
|
||||
|
||||
public static void powerSort(List<Integer> a) {
|
||||
int n = a.size();
|
||||
int i = 0;
|
||||
List<int[]> runs = new ArrayList<>();
|
||||
|
||||
int j = extendRun(a, i);
|
||||
runs.add(new int[] {i, j - i, 0});
|
||||
i = j;
|
||||
|
||||
while (i < n) {
|
||||
j = extendRun(a, i);
|
||||
int p = power(runs.get(runs.size() - 1), new int[] {i, j - i}, n);
|
||||
|
||||
while (p <= runs.get(runs.size() - 1)[2]) {
|
||||
mergeTopmost2(a, runs);
|
||||
}
|
||||
|
||||
runs.add(new int[] {i, j - i, p});
|
||||
i = j;
|
||||
}
|
||||
|
||||
while (runs.size() >= 2) {
|
||||
mergeTopmost2(a, runs);
|
||||
}
|
||||
}
|
||||
|
||||
/* """Fills the given array A with ascending runs of the given list of run
|
||||
lengths.
|
||||
More precisely, the array is first filled n, n-1, ..., 1
|
||||
and then for i=0..l-1 segments of runLengths.get(i) * runLenFactor
|
||||
are sorted ascending.
|
||||
The sum of all lengths in runLengths times runLenFactor should be equal to the
|
||||
length of A.
|
||||
"""*/
|
||||
|
||||
|
||||
/* static <T> void sort(T[] a, int lo, int hi, Comparator<? super T> c,
|
||||
T[] work, int workBase, int workLen) {
|
||||
assert c != null && a != null && lo >= 0 && lo <= hi && hi <= a.length;
|
||||
}*/
|
||||
/*
|
||||
public static final int MIN_MERGE=24;
|
||||
public int mergeCost=0;
|
||||
private final T []sortedArray;
|
||||
|
||||
public PowerSort(T[] sortedArray) {
|
||||
super();
|
||||
this.sortedArray = sortedArray;
|
||||
}
|
||||
|
||||
ArrayList<Integer> run1 = new ArrayList<>();
|
||||
ArrayList<Integer> run2 = new ArrayList<>();
|
||||
|
||||
private AbstractList<Integer> merge(ArrayList <Integer> run1, ArrayList<Integer> run2) {
|
||||
ArrayList<Integer> result = new ArrayList<>();
|
||||
|
||||
while(run1.size() > 0 && run2.size() >0) {
|
||||
if (run1.getFirst()<run2.getFirst()){
|
||||
result.add(run1.getFirst());
|
||||
run1.removeFirst();
|
||||
}else {
|
||||
result.add(run2.getFirst());
|
||||
run2.removeFirst();
|
||||
}
|
||||
result.addAll(run1);
|
||||
result.addAll(run2);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
public void mergeInplace(int[] a, int[] i, int[] m, int[] j) {
|
||||
//System.out.println("merge(" + i + "," + m + "," + j + ")");
|
||||
|
||||
System.out.printf("Merge(%d, %d, %d)%n", i, m, j);
|
||||
// this.mergeCost += j - i;
|
||||
|
||||
|
||||
for(int s =0; s < i.length && s< j.length ; s++) {
|
||||
|
||||
|
||||
// int[] leftSubarray = copyOfRange(a, i, m);
|
||||
// int[] rightSubarray = copyOfRange(a, m, j);
|
||||
// int[] mergedSubarray = merge(leftSubarray, rightSubarray);
|
||||
// System.arraycopy(mergedSubarray, 0, a, i, mergedSubarray.length);
|
||||
//// mergeCost += j[s] - i[s];
|
||||
// System.arraycopy(merge(Arrays.copyOfRange(a, i, m), Arrays.copyOfRange(a, m, j)), 0, a, i, j - i);
|
||||
// a[i:j]=merge(a[i:m],a[m:j]);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
public void power(int run1,int run2, int n) {
|
||||
int i = run1;
|
||||
int n1 = run1;
|
||||
int j = run2;
|
||||
int n2 = run2;
|
||||
int a=(i + n1/2) / n;
|
||||
int b=(j + n2/2) / n;
|
||||
int l =0;
|
||||
//while( Math.floor(a * 2**1)){
|
||||
// Math.floor(b * );
|
||||
// }
|
||||
|
||||
}
|
||||
|
||||
public void sorting(final int[] Array, final int left, final int right) {
|
||||
|
||||
|
||||
}*/
|
||||
|
||||
}
|
@ -24,6 +24,10 @@
|
||||
* questions.
|
||||
*/
|
||||
|
||||
/*
|
||||
* Imported from OpenJDK git repo ComparableTimSort.java
|
||||
*/
|
||||
|
||||
package de.uni_marburg.powersort.sort;
|
||||
|
||||
/**
|
||||
|
@ -0,0 +1,22 @@
|
||||
package de.uni_marburg.powersort.sort;
|
||||
|
||||
import de.uni_marburg.powersort.FinnSort.FinnSort;
|
||||
import de.uni_marburg.powersort.benchmark.NaturalOrder;
|
||||
|
||||
public enum SortEnum {
|
||||
// BUBBLE_SORT,
|
||||
MERGE_SORT,
|
||||
TIM_SORT,
|
||||
FINN_SORT,
|
||||
ASORT;
|
||||
|
||||
public SortImpl getSortImpl() {
|
||||
return switch (this) {
|
||||
// case BUBBLE_SORT -> array -> BubbleSort.sort(array, NaturalOrder.INSTANCE);
|
||||
case MERGE_SORT -> array -> MergeSort.legacyMergeSort(array, NaturalOrder.INSTANCE);
|
||||
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 ASORT -> array -> ASort.sort(array, NaturalOrder.INSTANCE);
|
||||
};
|
||||
}
|
||||
}
|
@ -0,0 +1,5 @@
|
||||
package de.uni_marburg.powersort.sort;
|
||||
|
||||
public interface SortImpl {
|
||||
void sort(Object[] list);
|
||||
}
|
@ -24,6 +24,10 @@
|
||||
* questions.
|
||||
*/
|
||||
|
||||
/*
|
||||
* Imported from OpenJDK git repo TimSort.java
|
||||
*/
|
||||
|
||||
package de.uni_marburg.powersort.sort;
|
||||
|
||||
import java.util.Comparator;
|
||||
|
41
app/src/test/java/de/uni_marburg/powersort/JUnitUtil.java
Normal file
41
app/src/test/java/de/uni_marburg/powersort/JUnitUtil.java
Normal file
@ -0,0 +1,41 @@
|
||||
package de.uni_marburg.powersort;
|
||||
|
||||
import org.junit.jupiter.params.converter.SimpleArgumentConverter;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
public class JUnitUtil {
|
||||
/**
|
||||
* https://stackoverflow.com/a/46850299/6334421
|
||||
*/
|
||||
public static class IntArrayConverter extends SimpleArgumentConverter {
|
||||
@Override
|
||||
protected int[] convert(Object source, Class<?> targetType) {
|
||||
if (source instanceof String s && int[].class.isAssignableFrom(targetType)) {
|
||||
if (s.isEmpty()) {
|
||||
return new int[0];
|
||||
}
|
||||
String[] strings = s.split("\\s*\\|\\s*");
|
||||
return Arrays.stream(strings).mapToInt(Integer::valueOf).toArray();
|
||||
} else {
|
||||
throw new IllegalArgumentException("Conversion from " + source.getClass()
|
||||
+ " to " + targetType + " not supported.");
|
||||
}
|
||||
}
|
||||
}
|
||||
public static class IntegerArrayConverter extends SimpleArgumentConverter {
|
||||
@Override
|
||||
protected Integer[] convert(Object source, Class<?> targetType) {
|
||||
if (source instanceof String s && Integer[].class.isAssignableFrom(targetType)) {
|
||||
if (s.isEmpty()) {
|
||||
return new Integer[0];
|
||||
}
|
||||
String[] strings = s.split("\\s*\\|\\s*");
|
||||
return Arrays.stream(strings).map(Integer::valueOf).toArray(Integer[]::new);
|
||||
} else {
|
||||
throw new IllegalArgumentException("Conversion from " + source.getClass()
|
||||
+ " to " + targetType + " not supported.");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -3,8 +3,10 @@ package de.uni_marburg.powersort.benchmark;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
class RandomIntTest {
|
||||
protected RandomIntTest(){
|
||||
protected RandomIntTest() {
|
||||
// This constructor is intentionally empty. Nothing special is needed here.
|
||||
}
|
||||
|
||||
@ -33,4 +35,14 @@ class RandomIntTest {
|
||||
|
||||
Assertions.fail("min or max not reached - not a random int generator");
|
||||
}
|
||||
|
||||
@Test
|
||||
void testReproducibility() {
|
||||
long seed = 1337;
|
||||
|
||||
int expected = 2147483647;
|
||||
long actual = RandomInt.integer(seed);
|
||||
|
||||
assertEquals(expected, actual);
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,35 @@
|
||||
package de.uni_marburg.powersort.data;
|
||||
|
||||
import de.uni_marburg.powersort.JUnitUtil;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.converter.ConvertWith;
|
||||
import org.junit.jupiter.params.provider.CsvSource;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
class AscendingRunsTest {
|
||||
@ParameterizedTest
|
||||
@CsvSource({
|
||||
"0,1,-1,''",
|
||||
"1,1,-1,0",
|
||||
"1,2,-2,0|1",
|
||||
"2,2,-2,0|1|-2|-1",
|
||||
"2,4,-2,0|1|2|3|-2|-1|0|1",
|
||||
})
|
||||
void testAscendingRuns1(int numOfRuns, int runLength, int decreaseBetweenRuns, @ConvertWith(JUnitUtil.IntArrayConverter.class) int[] expected) {
|
||||
Integer[] actualIntegers = AscendingRuns.newAscendingRuns(numOfRuns, runLength, decreaseBetweenRuns).getCopy();
|
||||
int[] actual = Arrays.stream(actualIntegers).mapToInt(Integer::valueOf).toArray();
|
||||
assertArrayEquals(expected, actual);
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@CsvSource({
|
||||
"1,0,-1",
|
||||
"-1,1,-1",
|
||||
})
|
||||
void testAscendingRuns2(int numOfRuns, int runLength, int decreaseBetweenRuns) {
|
||||
assertThrows(IllegalArgumentException.class, () -> AscendingRuns.newAscendingRuns(numOfRuns, runLength, decreaseBetweenRuns));
|
||||
}
|
||||
}
|
@ -0,0 +1,37 @@
|
||||
package de.uni_marburg.powersort.msort;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.IntStream;
|
||||
|
||||
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.extendRun;
|
||||
import static de.uni_marburg.powersort.msort.IMPL_M_1.fillWithAscRunsHighToLow;
|
||||
import static de.uni_marburg.powersort.msort.IMPL_M_1.powerSort;
|
||||
|
||||
class PowerSortTest {
|
||||
@Test
|
||||
|
||||
public void testWithOneInputList() {
|
||||
List<Integer> list = new ArrayList<>(List.of(5, 2, 8, 12, 1, 6));
|
||||
extendRun(list, 0);
|
||||
System.out.println(list);
|
||||
// example from slides he wrote this
|
||||
int[] runs = {5, 3, 3, 14, 1, 2};
|
||||
// runs = new int[]{9, 16, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7};
|
||||
|
||||
List<Integer> a = new ArrayList<>(IntStream.range(0, Arrays.stream(runs).sum()).boxed().collect(Collectors.toList()));
|
||||
|
||||
System.out.println();
|
||||
fillWithAscRunsHighToLow(a, runs, 1);
|
||||
MERGE_COST = 0;
|
||||
System.out.println("Sorting with Powersort:");
|
||||
powerSort(a);
|
||||
System.out.println("Merge cost: " + MERGE_COST);
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,7 @@
|
||||
package de.uni_marburg.powersort.sort;
|
||||
|
||||
public class ASortTest extends AbstractSortTest {
|
||||
ASortTest() {
|
||||
sortAlg = SortEnum.ASORT;
|
||||
}
|
||||
}
|
@ -0,0 +1,57 @@
|
||||
package de.uni_marburg.powersort.sort;
|
||||
|
||||
import de.uni_marburg.powersort.JUnitUtil;
|
||||
import de.uni_marburg.powersort.data.AscendingRuns;
|
||||
import de.uni_marburg.powersort.data.DescendingIntegers;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.converter.ConvertWith;
|
||||
import org.junit.jupiter.params.provider.CsvSource;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
|
||||
|
||||
public abstract class AbstractSortTest {
|
||||
SortEnum sortAlg;
|
||||
|
||||
@ParameterizedTest
|
||||
@CsvSource({
|
||||
"''",
|
||||
"1337",
|
||||
"2|3|1",
|
||||
"1|1",
|
||||
"2|1|2",
|
||||
})
|
||||
void test1(@ConvertWith(JUnitUtil.IntegerArrayConverter.class) Integer[] array) {
|
||||
sortAndCheckResult(array);
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@CsvSource({
|
||||
"3,7,-13",
|
||||
"3,7,-3",
|
||||
})
|
||||
void test2(int numOfRuns, int runLength, int decreaseBetweenRuns) {
|
||||
Integer[] array = AscendingRuns.newAscendingRuns(numOfRuns, runLength, decreaseBetweenRuns).getCopy();
|
||||
sortAndCheckResult(array);
|
||||
}
|
||||
@ParameterizedTest
|
||||
@CsvSource({
|
||||
"2",
|
||||
"3",
|
||||
"13",
|
||||
"1337",
|
||||
})
|
||||
void test2(int size) {
|
||||
Integer[] array = new DescendingIntegers(size).getCopy();
|
||||
sortAndCheckResult(array);
|
||||
}
|
||||
|
||||
void sortAndCheckResult(Integer[] array){
|
||||
Integer[] expected = Arrays.copyOf(array, array.length);
|
||||
Arrays.sort(expected);
|
||||
|
||||
sortAlg.getSortImpl().sort(array);
|
||||
assertArrayEquals(expected, array);
|
||||
}
|
||||
}
|
@ -0,0 +1,7 @@
|
||||
package de.uni_marburg.powersort.sort;
|
||||
|
||||
public class FinnSortTest extends AbstractSortTest {
|
||||
FinnSortTest() {
|
||||
sortAlg = SortEnum.FINN_SORT;
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user