diff --git a/app/src/main/java/de/uni_marburg/powersort/FinnSort/FinnSort.java b/app/src/main/java/de/uni_marburg/powersort/FinnSort/FinnSort.java index cb236e8..1981b0b 100644 --- a/app/src/main/java/de/uni_marburg/powersort/FinnSort/FinnSort.java +++ b/app/src/main/java/de/uni_marburg/powersort/FinnSort/FinnSort.java @@ -5,10 +5,12 @@ import static java.lang.Math.*; public class FinnSort { - private static final ArrayList runs = new ArrayList<>(); + private static ArrayList runs; public static void sort(T[] a, Comparator c) { + runs = new ArrayList<>(); + int n = a.length; int i = 0; int j = extendRunRight(a, i, c); @@ -42,14 +44,22 @@ public class FinnSort { ArrayList run2 = new ArrayList<>(Arrays.asList(a).subList(r2.start, r2.end)); ArrayList merge = new ArrayList<>(); - while (!run1.isEmpty() || !run2.isEmpty()) { - if (run2.isEmpty() || !run1.isEmpty() && c.compare(run1.getFirst(), run2.getFirst()) < 0) { + 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);