mirror of
https://gitlab.uni-marburg.de/langbeid/powersort.git
synced 2025-01-22 19:55:44 +01:00
Merge branch 'implementation-by-finn'
# Conflicts: # app/src/main/java/de/uni_marburg/powersort/FinnSort/FinnSort.java
This commit is contained in:
commit
8e047dca9d
@ -7,43 +7,43 @@ public class FinnSort {
|
|||||||
|
|
||||||
private static final ArrayList<Run> runs = new ArrayList<>();
|
private static final ArrayList<Run> runs = new ArrayList<>();
|
||||||
|
|
||||||
public static void sort(Integer[] a) {
|
static <T> void sort(T[] a, Comparator<? super T> c) {
|
||||||
|
|
||||||
int n = a.length;
|
int n = a.length;
|
||||||
int i = 0;
|
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));
|
runs.add(new Run(i, j, 0));
|
||||||
|
|
||||||
i = j;
|
i = j;
|
||||||
while (i < n) {
|
while (i < n) {
|
||||||
j = extendRunRight(a, i);
|
j = extendRunRight(a, i, c);
|
||||||
|
|
||||||
//printRuns();
|
//printRuns();
|
||||||
|
|
||||||
int p = power(runs.getLast(), new Run(i, j, 0), n);
|
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)) {
|
while (runs.size() >= 2 && p < power(runs.getLast(), runs.get(runs.size() - 2), n)) {
|
||||||
basicMerge(a, runs.removeFirst(), runs.removeFirst());
|
basicMerge(a, runs.removeFirst(), runs.removeFirst(), c);
|
||||||
}
|
}
|
||||||
|
|
||||||
runs.add(new Run(i, j, p));
|
runs.add(new Run(i, j, p));
|
||||||
i = j;
|
i = j;
|
||||||
}
|
}
|
||||||
while (runs.size() >= 2) {
|
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) {
|
private static <T> void basicMerge(T[] a, Run r1, Run r2, Comparator<? super T> c) {
|
||||||
ArrayList<Integer> run1 = new ArrayList<>(Arrays.asList(a).subList(r1.start, r1.end));
|
ArrayList<T> 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<T> run2 = new ArrayList<>(Arrays.asList(a).subList(r2.start, r2.end));
|
||||||
ArrayList<Integer> merge = new ArrayList<>();
|
ArrayList<T> merge = new ArrayList<>();
|
||||||
|
|
||||||
while (!run1.isEmpty() || !run2.isEmpty()) {
|
while (!run1.isEmpty() || !run2.isEmpty()) {
|
||||||
if (run2.isEmpty() || !run1.isEmpty() && run1.getFirst() < run2.getFirst()) {
|
if (run2.isEmpty() || !run1.isEmpty() && c.compare(run1.getFirst(), run2.getFirst()) < 0) {
|
||||||
merge.add(run1.removeFirst());
|
merge.add(run1.removeFirst());
|
||||||
} else {
|
} else {
|
||||||
merge.add(run2.removeFirst());
|
merge.add(run2.removeFirst());
|
||||||
@ -53,12 +53,12 @@ public class FinnSort {
|
|||||||
System.arraycopy(merge.toArray(), 0, a, min(r1.start, r2.start), merge.size());
|
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));
|
Run r = new Run(min(r1.start, r2.start), max(r1.end, r2.end), min(r1.power, r2.power));
|
||||||
runs.add(r);
|
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;
|
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++;
|
j++;
|
||||||
}
|
}
|
||||||
return j;
|
return j;
|
||||||
@ -87,12 +87,12 @@ public class FinnSort {
|
|||||||
}
|
}
|
||||||
System.out.println(s);
|
System.out.println(s);
|
||||||
}
|
}
|
||||||
public static void printList(Integer[] arr) {
|
public static <T> void printList(T[] arr, Comparator<? super T> c) {
|
||||||
String s = "";
|
String s = "";
|
||||||
int i = 0;
|
int i = 0;
|
||||||
while (i < arr.length) {
|
while (i < arr.length) {
|
||||||
String run = "[";
|
String run = "[";
|
||||||
int j = extendRunRight(arr, i);
|
int j = extendRunRight(arr, i, c);
|
||||||
for (int t = i; t < j; t++) {
|
for (int t = i; t < j; t++) {
|
||||||
run += arr[t] + ", ";
|
run += arr[t] + ", ";
|
||||||
}
|
}
|
||||||
@ -106,7 +106,7 @@ public class FinnSort {
|
|||||||
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};
|
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);
|
sort(numbers, Comparator.naturalOrder());
|
||||||
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