Integrating my test classes with the Object version IMPL_M_1 and passed the tests

This commit is contained in:
M-H9 2025-01-16 11:49:22 +01:00
parent e526c67c3c
commit f851b9784d
3 changed files with 53 additions and 38 deletions

View File

@ -1,6 +1,7 @@
package de.uni_marburg.powersort.MSort; package de.uni_marburg.powersort.MSort;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections; import java.util.Collections;
import java.util.Comparator; import java.util.Comparator;
import java.util.List; import java.util.List;
@ -29,9 +30,9 @@ public class IMPL_M_1 {
*/ */
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(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.length;
//This ensures that the sum of runLengths multiplied by runLenFactor equals the list size n. If not, an AssertionError is thrown. //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; assert IntStream.of(runLengths).sum() * runLenFactor == n;
@ -39,17 +40,27 @@ public class IMPL_M_1 {
//IntStream.of(runLengths).forEach(System.out::println); //IntStream.of(runLengths).forEach(System.out::println);
for (int i = 0; i < n; i++) { for (int i = 0; i < n; i++) {
//putting i in set a, while a is always the last index of n //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 //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. // 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) { for (int l : runLengths) {
int L = l * runLenFactor; int L = l * runLenFactor;
List<Integer> sublist = A.subList(i, i + L); // Sort the subarray from startIndex to startIndex+L manually
Collections.sort(sublist); for (int i = startIndex; i < startIndex + L - 1; i++) {
i += L; 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 <T> void mergeInplace(T[] a, int i, int m, int j, Comparator<? super T> c) { 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); System.out.printf("Merge(%d, %d, %d)%n", i, m, j);
MERGE_COST += j - i; MERGE_COST += j - i;
// Create temporary arrays for merging // Create temporary arrays for merging
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")

View File

@ -7,6 +7,8 @@ import java.util.Arrays;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
import de.uni_marburg.powersort.benchmark.NaturalOrder;
public class PowerSortT { public class PowerSortT {
public static void main(String[] args) { public static void main(String[] args) {
@ -22,7 +24,7 @@ public class PowerSortT {
// Test for fillWithAscRunsHighToLow // Test for fillWithAscRunsHighToLow
public static void testFillWithAscRunsHighToLow() { public static void testFillWithAscRunsHighToLow() {
List<Integer> A = new ArrayList<>(Collections.nCopies(10, 0)); Integer[] A = new Integer[10];
int[] runLengths = {2, 3, 5}; int[] runLengths = {2, 3, 5};
int runLenFactor = 1; int runLenFactor = 1;
fillWithAscRunsHighToLow(A, runLengths, runLenFactor); fillWithAscRunsHighToLow(A, runLengths, runLenFactor);
@ -31,24 +33,24 @@ public class PowerSortT {
// Test for merge // Test for merge
public static void testMerge() { public static void testMerge() {
List<Integer> run1 = new ArrayList<>(Arrays.asList(1, 4, 6)); Integer[] run1 ={1,4,6};
List<Integer> run2 = new ArrayList<>(Arrays.asList(2, 3, 5)); Integer []run2 = {2, 3, 5};
// List<Integer> result = merge(run1, run2); Integer[] result = merge(run1, run2, NaturalOrder.INSTANCE);
// 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)); Integer[] A = {1,4,6,2,3,5};
//mergeInplace(A, 0, 3, 6); mergeInplace(A, 0, 3, 6,NaturalOrder.INSTANCE);
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)); Integer [] A = {1, 2, 3, 6, 5, 4};
// int endIndex = extendRun(A, 0); int endIndex = extendRun(A, 0,NaturalOrder.INSTANCE);
// 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);
} }
@ -72,18 +74,18 @@ public class PowerSortT {
// Test for mergeTopmost2 // Test for mergeTopmost2
public static void testMergeTopmost2() { public static void testMergeTopmost2() {
List<Integer> A = new ArrayList<>(Arrays.asList(1, 3, 5, 2, 4, 6)); Integer[] A = {1, 3, 5, 2, 4, 6};
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,NaturalOrder.INSTANCE);
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)); Integer [] A = {10, 9, 8, 7, 6, 5, 4, 3, 2, 1};
// powerSort(A); powerSort(A, NaturalOrder.INSTANCE);
System.out.println("Test powerSort: " + A); System.out.println("Test powerSort: " + A);
} }
} }

View File

@ -20,26 +20,28 @@ class PowerSortTest {
// extendRun(list, 0); // extendRun(list, 0);
//System.out.println(list); //System.out.println(list);
// example from slides he wrote this // example from slides he wrote this
int[] runs = {5, 3, 3, 14, 1, 2}; // 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 = new int[]{9, 16, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7};
int totalSize = Arrays.stream(runs).sum();
List<Integer> a = new ArrayList<>(IntStream.range(0, Arrays.stream(runs).sum()).boxed().collect(Collectors.toList())); Integer[] a = new Integer[totalSize];
System.out.println(); System.out.println();
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,NaturalOrder.INSTANCE);
System.out.println("Sorted Array"+Arrays.toString(a));
System.out.println("Merge cost: " + MERGE_COST); System.out.println("Merge cost: " + MERGE_COST);
} }
@Test @Test
public void testWithFinnInputList() { 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}); 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};
//powerSort(numbers, NaturalOrder.INSTANCE); System.out.println("Original array"+Arrays.toString(numbers));
System.out.println("Result: "); powerSort(numbers, NaturalOrder.INSTANCE);
System.out.println("Result: "+Arrays.toString(numbers));
// System.out.println(new ArrayList<>(List.of(numbers))); // System.out.println(new ArrayList<>(List.of(numbers)));
} }
} }