mirror of
https://gitlab.uni-marburg.de/langbeid/powersort.git
synced 2025-01-21 19:50:35 +01:00
Converting IMPL_M_1 to Object and it works fine
This commit is contained in:
parent
637b988afd
commit
a684cb30ec
10
.idea/compiler.xml
generated
10
.idea/compiler.xml
generated
@ -3,6 +3,16 @@
|
|||||||
<component name="CompilerConfiguration">
|
<component name="CompilerConfiguration">
|
||||||
<annotationProcessing>
|
<annotationProcessing>
|
||||||
<profile default="true" name="Default" enabled="true" />
|
<profile default="true" name="Default" enabled="true" />
|
||||||
|
<profile name="Gradle Imported" enabled="true">
|
||||||
|
<outputRelativeToContentRoot value="true" />
|
||||||
|
<processorPath useClasspath="false">
|
||||||
|
<entry name="$USER_HOME$/.gradle/caches/modules-2/files-2.1/org.openjdk.jmh/jmh-generator-annprocess/1.37/da93888682df163144edf9b13d2b78e54166063a/jmh-generator-annprocess-1.37.jar" />
|
||||||
|
<entry name="$USER_HOME$/.gradle/caches/modules-2/files-2.1/org.openjdk.jmh/jmh-core/1.37/896f27e49105b35ea1964319c83d12082e7a79ef/jmh-core-1.37.jar" />
|
||||||
|
<entry name="$USER_HOME$/.gradle/caches/modules-2/files-2.1/net.sf.jopt-simple/jopt-simple/5.0.4/4fdac2fbe92dfad86aa6e9301736f6b4342a3f5c/jopt-simple-5.0.4.jar" />
|
||||||
|
<entry name="$USER_HOME$/.gradle/caches/modules-2/files-2.1/org.apache.commons/commons-math3/3.6.1/e4ba98f1d4b3c80ec46392f25e094a6a2e58fcbf/commons-math3-3.6.1.jar" />
|
||||||
|
</processorPath>
|
||||||
|
<module name="powersort.app.jmh" />
|
||||||
|
</profile>
|
||||||
</annotationProcessing>
|
</annotationProcessing>
|
||||||
<bytecodeTargetLevel target="23" />
|
<bytecodeTargetLevel target="23" />
|
||||||
</component>
|
</component>
|
||||||
|
@ -22,8 +22,8 @@ public class JmhCgl extends JmhBase {
|
|||||||
CglEnum dataEnum;
|
CglEnum dataEnum;
|
||||||
|
|
||||||
// Either all or a selection of sort implementations.
|
// Either all or a selection of sort implementations.
|
||||||
@Param()
|
//@Param()
|
||||||
//@Param({"TIM_SORT", "FASTER_FINN_SORT"})
|
@Param({"TIM_SORT", "FINN_SORT", "IMPL_M_11"})
|
||||||
SortEnum sortEnum;
|
SortEnum sortEnum;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -2,10 +2,11 @@ package de.uni_marburg.powersort.MSort;
|
|||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
|
import java.util.Comparator;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.stream.IntStream;
|
import java.util.stream.IntStream;
|
||||||
|
|
||||||
public class IMPL_M_1 <T extends Comparable<? super T>> {
|
public class IMPL_M_1 {
|
||||||
|
|
||||||
private IMPL_M_1() {
|
private IMPL_M_1() {
|
||||||
}
|
}
|
||||||
@ -28,7 +29,6 @@ public class IMPL_M_1 <T extends Comparable<? super T>> {
|
|||||||
*/
|
*/
|
||||||
protected static int MERGE_COST = 0;
|
protected static int MERGE_COST = 0;
|
||||||
|
|
||||||
|
|
||||||
public static void fillWithAscRunsHighToLow(List<Integer> A, int[] runLengths, int runLenFactor) {
|
public static void fillWithAscRunsHighToLow(List<Integer> A, int[] runLengths, int runLenFactor) {
|
||||||
//A has a fixed size, but it doesn't have any meaningful values
|
//A has a fixed size, but it doesn't have any meaningful values
|
||||||
int n = A.size();
|
int n = A.size();
|
||||||
@ -54,55 +54,91 @@ public class IMPL_M_1 <T extends Comparable<? super T>> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static <T extends Comparable<? super T>> List<T> merge(List<T> run1, List<T> run2) {
|
static <T> List<T> merge(List<T> run1, List<T> run2, Comparator<? super T> c) {
|
||||||
List<T> result = new ArrayList<>();
|
List<T> result = new ArrayList<>();
|
||||||
while (!run1.isEmpty() && !run2.isEmpty()) {
|
while (!run1.isEmpty() && !run2.isEmpty()) {
|
||||||
//This comparison only works if the lists are sorted
|
//This comparison only works if the lists are sorted
|
||||||
if (run1.get(0).compareTo(run2.get(0)) < 0) {
|
if (c.compare(run1.getFirst(),run2.getFirst()) < 0) {
|
||||||
result.add(run1.remove(0));
|
result.add(run1.removeFirst());
|
||||||
} else {
|
} else {
|
||||||
result.add(run2.remove(0));
|
result.add(run2.removeFirst());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
/// can be improved by finding out which one is empty and only add the other one
|
// can be improved by finding out which one is empty and only add the other one
|
||||||
result.addAll(run1);
|
result.addAll(run1);
|
||||||
result.addAll(run2);
|
result.addAll(run2);
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
static <T extends Comparable<? super T>> void mergeInplace(List<T> a, int i, int m, int j) {
|
// New helper method to handle range reversal
|
||||||
// System.out.printf("Merge(%d, %d, %d)%n", i, m, j);
|
private static <T> void reverseRange(T[] a, int start, int end) {
|
||||||
MERGE_COST += j - i;
|
// end is exclusive, so we need to use end-1 as the right boundary
|
||||||
List<T> sublist = merge(
|
int left = start;
|
||||||
new ArrayList<>(a.subList(i, m)),
|
int right = end - 1;
|
||||||
new ArrayList<>(a.subList(m, j))
|
while (left < right) {
|
||||||
);
|
T temp = a[left];
|
||||||
for (int k = 0; k < sublist.size(); k++) {
|
a[left] = a[right];
|
||||||
a.set(i + k, sublist.get(k));
|
a[right] = temp;
|
||||||
|
left++;
|
||||||
|
right--;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static <T extends Comparable<? super T>> int extendRun(List<T> a, int i) {
|
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);
|
||||||
|
MERGE_COST += j - i;
|
||||||
|
// Create temporary arrays for merging
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
|
T[] left = (T[]) new Object[m - i];
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
|
T[] right = (T[]) new Object[j - m];
|
||||||
|
|
||||||
|
// Copy data to temporary arrays
|
||||||
|
System.arraycopy(a, i, left, 0, m - i);
|
||||||
|
System.arraycopy(a, m, right, 0, j - m);
|
||||||
|
|
||||||
|
// Merge back to original array
|
||||||
|
int k = i, l = 0, r = 0;
|
||||||
|
while (l < left.length && r < right.length) {
|
||||||
|
if (c.compare(left[l], right[r]) <= 0) {
|
||||||
|
a[k++] = left[l++];
|
||||||
|
} else {
|
||||||
|
a[k++] = right[r++];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Copy remaining elements
|
||||||
|
while (l < left.length) {
|
||||||
|
a[k++] = left[l++];
|
||||||
|
}
|
||||||
|
while (r < right.length) {
|
||||||
|
a[k++] = right[r++];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
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 was the element before end so just return the last element
|
||||||
if (i == a.size() - 1) {
|
if (i == a.length - 1) {
|
||||||
return i + 1;
|
return i + 1;
|
||||||
}
|
}
|
||||||
//we're looking at the element next to a[i]
|
//we're looking at the element next to a[i]
|
||||||
int j = i + 1;
|
int j = i + 1;
|
||||||
if (a.get(i).compareTo( a.get(j)) <=0) {
|
if (c.compare(a[i],a[j]) <=0) {
|
||||||
while (j < a.size() && a.get(j - 1).compareTo( a.get(j)) <= 0) {
|
while (j < a.length && c.compare(a[j - 1], a[j]) <= 0) {
|
||||||
j++;
|
j++;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
while (j < a.size() && a.get(j - 1).compareTo(a.get(j)) > 0) {
|
while (j < a.length && c.compare(a[j - 1],a[j]) > 0) {
|
||||||
j++;
|
j++;
|
||||||
}
|
}
|
||||||
List<T> sublist = a.subList(i, j);
|
// Reverse the decreasing sequence
|
||||||
Collections.reverse(sublist);
|
reverseRange(a,i,j);
|
||||||
}
|
}
|
||||||
return j;
|
return j;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public static int power(int[] run1, int[] run2, int n) {
|
public static int power(int[] run1, int[] run2, int n) {
|
||||||
int i1 = run1[0], n1 = run1[1];
|
int i1 = run1[0], n1 = run1[1];
|
||||||
int i2 = run2[0], n2 = run2[1];
|
int i2 = run2[0], n2 = run2[1];
|
||||||
@ -122,35 +158,35 @@ public class IMPL_M_1 <T extends Comparable<? super T>> {
|
|||||||
return l;
|
return l;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static <T extends Comparable<? super T>> void mergeTopmost2(List<T> a, List<int []> runs) {
|
public static <T> void mergeTopmost2(T[] a, List<int[]> runs, Comparator<? super T> c) {
|
||||||
assert runs.size() >= 2;
|
assert runs.size() >= 2;
|
||||||
|
|
||||||
int[] Y = runs.get(runs.size() - 2);
|
int[] Y = runs.get(runs.size() - 2);
|
||||||
int[] Z = runs.get(runs.size() - 1);
|
int[] Z = runs.getLast();
|
||||||
|
|
||||||
assert Z[0] == Y[0] + Y[1];
|
assert Z[0] == Y[0] + Y[1];
|
||||||
|
|
||||||
mergeInplace(a, Y[0], Z[0], Z[0] + Z[1]);
|
mergeInplace(a, Y[0], Z[0], Z[0] + Z[1], c);
|
||||||
|
|
||||||
runs.set(runs.size() - 2, new int[] {Y[0], Y[1] + Z[1], Y[2]});
|
runs.set(runs.size() - 2, new int[] {Y[0], Y[1] + Z[1], Y[2]});
|
||||||
runs.removeLast();
|
runs.removeLast();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static <T extends Comparable<? super T>> void powerSort(List<T> a) {
|
public static <T> void powerSort(T[] a, Comparator<? super T> c) {
|
||||||
int n = a.size();
|
int n = a.length;
|
||||||
int i = 0;
|
int i = 0;
|
||||||
List<int[]> runs = new ArrayList<>();
|
List<int[]> runs = new ArrayList<>();
|
||||||
|
|
||||||
int j = extendRun(a, i);
|
int j = extendRun(a, i, c);
|
||||||
runs.add(new int[] {i, j - i, 0});
|
runs.add(new int[] {i, j - i, 0});
|
||||||
i = j;
|
i = j;
|
||||||
|
|
||||||
while (i < n) {
|
while (i < n) {
|
||||||
j = extendRun(a, i);
|
j = extendRun(a, i, c);
|
||||||
int p = power(runs.get(runs.size() - 1), new int[] {i, j - i}, n);
|
int p = power(runs.getLast(), new int[] {i, j - i}, n);
|
||||||
|
|
||||||
while (p <= runs.getLast()[2]) {
|
while (p <= runs.getLast()[2]) {
|
||||||
mergeTopmost2(a, runs);
|
mergeTopmost2(a, runs, c);
|
||||||
}
|
}
|
||||||
|
|
||||||
runs.add(new int[] {i, j - i, p});
|
runs.add(new int[] {i, j - i, p});
|
||||||
@ -158,7 +194,7 @@ public class IMPL_M_1 <T extends Comparable<? super T>> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
while (runs.size() >= 2) {
|
while (runs.size() >= 2) {
|
||||||
mergeTopmost2(a, runs);
|
mergeTopmost2(a, runs, c);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -79,7 +79,7 @@ public class IMPL_M_2<T>{
|
|||||||
* of the minimum stack length required as a function of the length
|
* of the minimum stack length required as a function of the length
|
||||||
* of the array being sorted and the minimum merge sequence length.
|
* of the array being sorted and the minimum merge sequence length.
|
||||||
*/
|
*/
|
||||||
private static final int MIN_MERGE = 32;
|
private static final int MIN_MERGE = 24;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The array being sorted.
|
* The array being sorted.
|
||||||
|
@ -2,6 +2,8 @@ package de.uni_marburg.powersort.sort;
|
|||||||
|
|
||||||
import de.uni_marburg.powersort.FinnSort.FinnSort;
|
import de.uni_marburg.powersort.FinnSort.FinnSort;
|
||||||
import de.uni_marburg.powersort.FinnSort.FasterFinnSort;
|
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.benchmark.NaturalOrder;
|
import de.uni_marburg.powersort.benchmark.NaturalOrder;
|
||||||
import de.uni_marburg.powersort.sort.dpqs.DualPivotQuicksort;
|
import de.uni_marburg.powersort.sort.dpqs.DualPivotQuicksort;
|
||||||
|
|
||||||
@ -10,6 +12,8 @@ public enum SortEnum {
|
|||||||
FASTER_FINN_SORT,
|
FASTER_FINN_SORT,
|
||||||
ASORT,
|
ASORT,
|
||||||
FINN_SORT,
|
FINN_SORT,
|
||||||
|
IMPL_M_11,
|
||||||
|
IMPL_M_21,
|
||||||
DPQS,
|
DPQS,
|
||||||
QUICK_SORT,
|
QUICK_SORT,
|
||||||
MERGE_SORT,
|
MERGE_SORT,
|
||||||
@ -23,6 +27,8 @@ public enum SortEnum {
|
|||||||
case MERGE_SORT -> array -> MergeSort.legacyMergeSort(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 TIM_SORT -> array -> TimSort.sort(array, 0, array.length, NaturalOrder.INSTANCE, null, 0, 0);
|
||||||
case FINN_SORT -> array -> FinnSort.sort(array, NaturalOrder.INSTANCE);
|
case FINN_SORT -> array -> FinnSort.sort(array, NaturalOrder.INSTANCE);
|
||||||
|
case IMPL_M_11 -> array -> IMPL_M_1.powerSort(array,NaturalOrder.INSTANCE);
|
||||||
|
case IMPL_M_21 -> array -> IMPL_M_2.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 FASTER_FINN_SORT -> array -> FasterFinnSort.sort(array, 0, array.length, NaturalOrder.INSTANCE, null, 0, 0);
|
||||||
case ASORT -> array -> ASort.sort(array, NaturalOrder.INSTANCE);
|
case ASORT -> array -> ASort.sort(array, NaturalOrder.INSTANCE);
|
||||||
};
|
};
|
||||||
|
@ -33,22 +33,22 @@ public class PowerSortT {
|
|||||||
public static void testMerge() {
|
public static void testMerge() {
|
||||||
List<Integer> run1 = new ArrayList<>(Arrays.asList(1, 4, 6));
|
List<Integer> run1 = new ArrayList<>(Arrays.asList(1, 4, 6));
|
||||||
List<Integer> run2 = new ArrayList<>(Arrays.asList(2, 3, 5));
|
List<Integer> run2 = new ArrayList<>(Arrays.asList(2, 3, 5));
|
||||||
List<Integer> result = merge(run1, run2);
|
// List<Integer> result = merge(run1, run2);
|
||||||
System.out.println("Test merge: " + result);
|
// System.out.println("Test merge: " + result);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Test for mergeInplace
|
// Test for mergeInplace
|
||||||
public static void testMergeInplace() {
|
public static void testMergeInplace() {
|
||||||
List<Integer> A = new ArrayList<>(Arrays.asList(1, 4, 6, 2, 3, 5));
|
List<Integer> A = new ArrayList<>(Arrays.asList(1, 4, 6, 2, 3, 5));
|
||||||
mergeInplace(A, 0, 3, 6);
|
//mergeInplace(A, 0, 3, 6);
|
||||||
System.out.println("Test mergeInplace: " + A);
|
System.out.println("Test mergeInplace: " + A);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Test for extendRun
|
// Test for extendRun
|
||||||
public static void testExtendRun() {
|
public static void testExtendRun() {
|
||||||
List<Integer> A = new ArrayList<>(Arrays.asList(1, 2, 3, 6, 5, 4));
|
List<Integer> A = new ArrayList<>(Arrays.asList(1, 2, 3, 6, 5, 4));
|
||||||
int endIndex = extendRun(A, 0);
|
// int endIndex = extendRun(A, 0);
|
||||||
System.out.println("Test extendRun (from 0): " + endIndex);
|
// System.out.println("Test extendRun (from 0): " + endIndex);
|
||||||
System.out.println("Modified List: " + A);
|
System.out.println("Modified List: " + A);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -76,14 +76,14 @@ public class PowerSortT {
|
|||||||
List<int[]> runs = new ArrayList<>();
|
List<int[]> runs = new ArrayList<>();
|
||||||
runs.add(new int[]{0, 3, 1});
|
runs.add(new int[]{0, 3, 1});
|
||||||
runs.add(new int[]{3, 3, 1});
|
runs.add(new int[]{3, 3, 1});
|
||||||
mergeTopmost2(A, runs);
|
// mergeTopmost2(A, runs);
|
||||||
System.out.println("Test mergeTopmost2: " + A);
|
System.out.println("Test mergeTopmost2: " + A);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Test for powerSort
|
// Test for powerSort
|
||||||
public static void testPowerSort() {
|
public static void testPowerSort() {
|
||||||
List<Integer> A = new ArrayList<>(Arrays.asList(10, 9, 8, 7, 6, 5, 4, 3, 2, 1));
|
List<Integer> A = new ArrayList<>(Arrays.asList(10, 9, 8, 7, 6, 5, 4, 3, 2, 1));
|
||||||
powerSort(A);
|
// powerSort(A);
|
||||||
System.out.println("Test powerSort: " + A);
|
System.out.println("Test powerSort: " + A);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -28,7 +28,7 @@ class PowerSortTest {
|
|||||||
fillWithAscRunsHighToLow(a, runs, 1);
|
fillWithAscRunsHighToLow(a, runs, 1);
|
||||||
MERGE_COST = 0;
|
MERGE_COST = 0;
|
||||||
System.out.println("Sorting with Powersort:");
|
System.out.println("Sorting with Powersort:");
|
||||||
powerSort(a);
|
//powerSort(a);
|
||||||
System.out.println("Merge cost: " + MERGE_COST);
|
System.out.println("Merge cost: " + MERGE_COST);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -36,7 +36,7 @@ class PowerSortTest {
|
|||||||
public void testWithFinnInputList() {
|
public void testWithFinnInputList() {
|
||||||
List<Integer> numbers = List.of(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});
|
List<Integer> numbers = List.of(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});
|
||||||
|
|
||||||
powerSort(numbers);
|
// powerSort(numbers);
|
||||||
System.out.println("Result: ");
|
System.out.println("Result: ");
|
||||||
System.out.println(new ArrayList<>(List.of(numbers)));
|
System.out.println(new ArrayList<>(List.of(numbers)));
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user