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 b1cdab4..4dc8dcd 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 @@ -1,6 +1,7 @@ package de.uni_marburg.powersort.MSort; import java.util.ArrayList; +import java.util.Arrays; import java.util.Collections; import java.util.Comparator; import java.util.List; @@ -29,9 +30,9 @@ public class IMPL_M_1 { */ protected static int MERGE_COST = 0; - public static void fillWithAscRunsHighToLow(List A, int[] runLengths, int runLenFactor) { + public static void fillWithAscRunsHighToLow(Integer[] A, int[] runLengths, int runLenFactor) { //A has a fixed size, but it doesn't have any meaningful values - int n = A.size(); + int n = A.length; //This ensures that the sum of runLengths multiplied by runLenFactor equals the list size n. If not, an AssertionError is thrown. assert IntStream.of(runLengths).sum() * runLenFactor == n; @@ -39,17 +40,27 @@ public class IMPL_M_1 { //IntStream.of(runLengths).forEach(System.out::println); for (int i = 0; i < n; i++) { //putting i in set a, while a is always the last index of n - A.set(i, n - i); + A[i] = n - i; } - int i = 0; + //For each value l in the array runLengths, do the following // runLengths = {2, 3, 5}, the loop will run three times, with l taking values 2, 3, and 5 respectively. + int startIndex = 0; for (int l : runLengths) { int L = l * runLenFactor; - List sublist = A.subList(i, i + L); - Collections.sort(sublist); - i += L; + // Sort the subarray from startIndex to startIndex+L manually + for (int i = startIndex; i < startIndex + L - 1; i++) { + for (int j = startIndex; j < startIndex + L - 1 - (i - startIndex); j++) { + if (A[j] > A[j + 1]) { + // Swap elements + int temp = A[j]; + A[j] = A[j + 1]; + A[j + 1] = temp; + } + } + } + startIndex += L; } } @@ -100,7 +111,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); + System.out.printf("Merge(%d, %d, %d)%n", i, m, j); MERGE_COST += j - i; // Create temporary arrays for merging @SuppressWarnings("unchecked") diff --git a/app/src/test/java/de/uni_marburg/powersort/MSort/PowerSortT.java b/app/src/test/java/de/uni_marburg/powersort/MSort/PowerSortT.java index feb81dc..0664717 100644 --- a/app/src/test/java/de/uni_marburg/powersort/MSort/PowerSortT.java +++ b/app/src/test/java/de/uni_marburg/powersort/MSort/PowerSortT.java @@ -7,6 +7,8 @@ import java.util.Arrays; import java.util.Collections; import java.util.List; +import de.uni_marburg.powersort.benchmark.NaturalOrder; + public class PowerSortT { public static void main(String[] args) { @@ -22,7 +24,7 @@ public class PowerSortT { // Test for fillWithAscRunsHighToLow public static void testFillWithAscRunsHighToLow() { - List A = new ArrayList<>(Collections.nCopies(10, 0)); + Integer[] A = new Integer[10]; int[] runLengths = {2, 3, 5}; int runLenFactor = 1; fillWithAscRunsHighToLow(A, runLengths, runLenFactor); @@ -31,24 +33,24 @@ public class PowerSortT { // Test for merge public static void testMerge() { - List run1 = new ArrayList<>(Arrays.asList(1, 4, 6)); - List run2 = new ArrayList<>(Arrays.asList(2, 3, 5)); - // List result = merge(run1, run2); - // System.out.println("Test merge: " + result); + Integer[] run1 ={1,4,6}; + Integer []run2 = {2, 3, 5}; + Integer[] result = merge(run1, run2, NaturalOrder.INSTANCE); + System.out.println("Test merge: " + result); } // Test for mergeInplace public static void testMergeInplace() { - List A = new ArrayList<>(Arrays.asList(1, 4, 6, 2, 3, 5)); - //mergeInplace(A, 0, 3, 6); + Integer[] A = {1,4,6,2,3,5}; + mergeInplace(A, 0, 3, 6,NaturalOrder.INSTANCE); System.out.println("Test mergeInplace: " + A); } // Test for extendRun public static void testExtendRun() { - List A = new ArrayList<>(Arrays.asList(1, 2, 3, 6, 5, 4)); - // int endIndex = extendRun(A, 0); - // System.out.println("Test extendRun (from 0): " + endIndex); + Integer [] A = {1, 2, 3, 6, 5, 4}; + int endIndex = extendRun(A, 0,NaturalOrder.INSTANCE); + System.out.println("Test extendRun (from 0): " + endIndex); System.out.println("Modified List: " + A); } @@ -72,18 +74,18 @@ public class PowerSortT { // Test for mergeTopmost2 public static void testMergeTopmost2() { - List A = new ArrayList<>(Arrays.asList(1, 3, 5, 2, 4, 6)); + Integer[] A = {1, 3, 5, 2, 4, 6}; List runs = new ArrayList<>(); runs.add(new int[]{0, 3, 1}); runs.add(new int[]{3, 3, 1}); - // mergeTopmost2(A, runs); + mergeTopmost2(A, runs,NaturalOrder.INSTANCE); System.out.println("Test mergeTopmost2: " + A); } // Test for powerSort public static void testPowerSort() { - List A = new ArrayList<>(Arrays.asList(10, 9, 8, 7, 6, 5, 4, 3, 2, 1)); - // powerSort(A); + Integer [] A = {10, 9, 8, 7, 6, 5, 4, 3, 2, 1}; + powerSort(A, NaturalOrder.INSTANCE); System.out.println("Test powerSort: " + A); } } 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 da12352..134834e 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 @@ -18,28 +18,30 @@ class PowerSortTest { public void testWithOneInputList() { // List list = new ArrayList<>(List.of(5, 2, 8, 12, 1, 6)); // extendRun(list, 0); -//System.out.println(list); + //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}; + // int[] runs = {5, 3, 3, 14, 1, 2}; + int [] runs = new int[]{9, 16, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7}; + int totalSize = Arrays.stream(runs).sum(); - List a = new ArrayList<>(IntStream.range(0, Arrays.stream(runs).sum()).boxed().collect(Collectors.toList())); + Integer[] a = new Integer[totalSize]; - 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); + System.out.println(); + fillWithAscRunsHighToLow(a, runs, 1); + MERGE_COST = 0; + System.out.println("Sorting with Powersort:"); + powerSort(a,NaturalOrder.INSTANCE); + System.out.println("Sorted Array"+Arrays.toString(a)); + System.out.println("Merge cost: " + MERGE_COST); } @Test - public void testWithFinnInputList() { - //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, NaturalOrder.INSTANCE); - System.out.println("Result: "); - // System.out.println(new ArrayList<>(List.of(numbers))); + public void testWithFinnInputList() { + 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}; + System.out.println("Original array"+Arrays.toString(numbers)); + powerSort(numbers, NaturalOrder.INSTANCE); + System.out.println("Result: "+Arrays.toString(numbers)); + // System.out.println(new ArrayList<>(List.of(numbers))); } }