diff --git a/app/src/main/java/de/uni_marburg/powersort/MSort/IMPL_M_1.java b/app/src/main/java/de/uni_marburg/powersort/MSort/IMPL_M_1.java index 54b8ea4..b1cdab4 100644 --- a/app/src/main/java/de/uni_marburg/powersort/MSort/IMPL_M_1.java +++ b/app/src/main/java/de/uni_marburg/powersort/MSort/IMPL_M_1.java @@ -54,19 +54,34 @@ public class IMPL_M_1 { } - static List merge(List run1, List run2, Comparator c) { - List result = new ArrayList<>(); - while (!run1.isEmpty() && !run2.isEmpty()) { + static T[] merge(T[] run1, T[] run2, Comparator c) { + /** + * We need this because: + * In Java, you cannot create generic arrays directly (can't write new T[size]) + * The workaround is to create an Object array and cast it to T[] + * This casting generates a warning because the compiler can't verify the type safety at runtime due to Java's type erasure + * **/ + @SuppressWarnings("unchecked") + T[] result = (T[]) new Object[run1.length + run2.length]; + int i = 0, j = 0, k = 0; + + while (i < run1.length && j < run2.length) { //This comparison only works if the lists are sorted - if (c.compare(run1.getFirst(),run2.getFirst()) < 0) { - result.add(run1.removeFirst()); + if (c.compare(run1[i], run2[j]) <= 0) { + result[k++] = run1[i++]; } else { - result.add(run2.removeFirst()); + result[k++] = run2[j++]; } } // can be improved by finding out which one is empty and only add the other one - result.addAll(run1); - result.addAll(run2); + // Copy remaining elements + while (i < run1.length) { + result[k++] = run1[i++]; + } + while (j < run2.length) { + result[k++] = run2[j++]; + } + return result; } @@ -87,7 +102,7 @@ public class IMPL_M_1 { static void mergeInplace(T[] a, int i, int m, int j, Comparator c) { // System.out.printf("Merge(%d, %d, %d)%n", i, m, j); MERGE_COST += j - i; -// Create temporary arrays for merging + // Create temporary arrays for merging @SuppressWarnings("unchecked") T[] left = (T[]) new Object[m - i]; @SuppressWarnings("unchecked") @@ -98,22 +113,11 @@ public class IMPL_M_1 { 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++]; - } - } + // Merge the runs + T[] merged = merge(left, right, c); - // Copy remaining elements - while (l < left.length) { - a[k++] = left[l++]; - } - while (r < right.length) { - a[k++] = right[r++]; - } + //copy back to original array + System.arraycopy(merged, 0, a, i,merged.length); } diff --git a/app/src/main/java/de/uni_marburg/powersort/MSort/IMPL_M_2.java b/app/src/main/java/de/uni_marburg/powersort/MSort/IMPL_M_2.java index b892e84..a552fca 100644 --- a/app/src/main/java/de/uni_marburg/powersort/MSort/IMPL_M_2.java +++ b/app/src/main/java/de/uni_marburg/powersort/MSort/IMPL_M_2.java @@ -79,7 +79,7 @@ public class IMPL_M_2{ * 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 = 24; + private static final int MIN_MERGE = 32; /** * The array being sorted. diff --git a/app/src/test/java/de/uni_marburg/powersort/MSort/PowerSortTest.java b/app/src/test/java/de/uni_marburg/powersort/MSort/PowerSortTest.java index 07d5ab6..da12352 100644 --- a/app/src/test/java/de/uni_marburg/powersort/MSort/PowerSortTest.java +++ b/app/src/test/java/de/uni_marburg/powersort/MSort/PowerSortTest.java @@ -6,6 +6,7 @@ import java.util.List; import java.util.stream.Collectors; import java.util.stream.IntStream; +import de.uni_marburg.powersort.benchmark.NaturalOrder; import org.junit.jupiter.api.Test; import static de.uni_marburg.powersort.MSort.IMPL_M_1.MERGE_COST; @@ -34,11 +35,11 @@ class PowerSortTest { @Test public void testWithFinnInputList() { - List 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}); + //T [] 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}); - // powerSort(numbers); + //powerSort(numbers, NaturalOrder.INSTANCE); System.out.println("Result: "); - System.out.println(new ArrayList<>(List.of(numbers))); + // System.out.println(new ArrayList<>(List.of(numbers))); } }