diff --git a/app/src/main/java/ch/nuth/zhaw/exbox/SortServer.java b/app/src/main/java/ch/nuth/zhaw/exbox/SortServer.java new file mode 100644 index 0000000..7adab9b --- /dev/null +++ b/app/src/main/java/ch/nuth/zhaw/exbox/SortServer.java @@ -0,0 +1,114 @@ +package ch.nuth.zhaw.exbox; + +import java.util.Arrays; +import java.util.Map; +import java.util.function.Consumer; +import java.util.function.Supplier; + +public class SortServer implements CommandExecutor { + private final int DATARANGE = 10000000; + public int dataElems; // number of data + + public void swap(int[] a, int i, int j) { + int h = a[i]; + a[i] = a[j]; + a[j] = h; + } + + public void bubbleSort(int[] a) { + // TODO Implement Aufgabe 1 + } + + public void insertionSort(int[] a) { + // TODO Implement Aufgabe 3 + } + + public void selectionSort(int[] a) { + // TODO Implement Aufgabe 3 + } + + public void streamSort(int[] a) { + // zum Vergleichen (falls Sie Zeit und Lust haben) + int[] b = Arrays.stream(a).sorted().toArray(); + System.arraycopy(b, 0, a, 0, a.length); + } + + public boolean isSorted(int[] a) { + // TODO Implement Aufgabe 1 + return true; + } + + public int[] randomData() { + int[] a = new int[dataElems]; + // TODO Implement Aufgabe 1 + return a; + } + + public int[] ascendingData() { + int[] a = new int[dataElems]; + // TODO Implement Aufgabe 1 + return a; + } + + public int[] descendingData() { + int[] a = new int[dataElems]; + // TODO Implement Aufgabe 1 + return a; + } + + // measure time of sorting algorithm + // generator to generate the data + // consumer sorts the data + // return elapsed time in ms + // if data is not sorted an exception is thrown + public double measureTime(Supplier generator, Consumer sorter) throws Exception { + double elapsed = 0; + + int[] a = generator.get(); + int[] b = new int[dataElems]; + + long startTime = System.currentTimeMillis(); + long endTime = startTime; + + // TODO Implement Aufgabe 1 und 2 (Tipp: siehe Consumer für Aufruf von Sortiermethode) + + elapsed = (double)(endTime - startTime); + if (!isSorted(b)) throw new Exception ("ERROR not sorted"); + return elapsed; + } + + public String execute(String arg) { + Map> sorter = Map.of( + "BUBBLE", this::bubbleSort, + "INSERTION", this::insertionSort, + "SELECTION", this::selectionSort, + "STREAM", this::streamSort + ); + Map> generator = Map.of( + "RANDOM", this::randomData, + "ASC", this::ascendingData, + "DESC", this::descendingData + ); + + String[] args = arg.toUpperCase().split(" "); + dataElems = Integer.parseInt(args[2]); + try { + double time = measureTime(generator.get(args[1]), sorter.get(args[0])); + return arg + " " + time + " ms\n"; + } catch (Exception ex) { + return arg + " " + ex.getMessage(); + } + } + + public static void main(String[] args) { + SortServer sorter = new SortServer(); + String sort; + sort = "BUBBLE RANDOM 10000"; System.out.println(sorter.execute(sort)); + sort = "SELECTION RANDOM 10000"; System.out.println(sorter.execute(sort)); + sort = "INSERTION RANDOM 10000"; System.out.println(sorter.execute(sort)); + + sort = "BUBBLE ASC 10000"; System.out.println(sorter.execute(sort)); + sort = "SELECTION ASC 10000"; System.out.println(sorter.execute(sort)); + sort = "INSERTION ASC 10000"; System.out.println(sorter.execute(sort)); + } +} diff --git a/app/src/test/java/ch/nuth/zhaw/exbox/ADS11_2_test.java b/app/src/test/java/ch/nuth/zhaw/exbox/ADS11_2_test.java new file mode 100644 index 0000000..ce742cb --- /dev/null +++ b/app/src/test/java/ch/nuth/zhaw/exbox/ADS11_2_test.java @@ -0,0 +1,72 @@ +package ch.nuth.zhaw.exbox; + +import org.junit.jupiter.api.Test; + +import java.util.function.Consumer; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; + +/** + * @author K Rege + * @version 1.00 2018/3/17 + * @version 1.01 2021/8/1 + */ +public class ADS11_2_test { + final int DATAELEMS = 10000; + SortServer sortServer = new SortServer(); + + @Test + public void testRandomData() { + sortServer.dataElems = DATAELEMS; + int[] data = sortServer.randomData(); + assertEquals(sortServer.dataElems, data.length, "Anzahl Daten"); + assertTrue(data[0] != data[1], "Distribution"); + } + + @Test + public void testAscendingData() { + sortServer.dataElems = DATAELEMS; + int[] data = sortServer.ascendingData(); + assertEquals(sortServer.dataElems, data.length, "Anzahl Daten"); + for (int i = 0; i< sortServer.dataElems-1; i++) { + assertTrue(data[i] <= data[i + 1], "Distribution"); + } + } + + @Test + public void testDescendingData() { + sortServer.dataElems = DATAELEMS; + int[] data = sortServer.descendingData(); + assertEquals(sortServer.dataElems, data.length, "Anzahl Daten"); + for (int i = 0; i< sortServer.dataElems-1; i++) { + assertTrue(data[i] >= data[i + 1], "Distribution"); + } + } + + @Test + public void testBubbleSort() { + sortServer.dataElems = DATAELEMS; + int[] data = sortServer.randomData(); + sortServer.bubbleSort(data); + for (int i = 0; i< sortServer.dataElems-1; i++) { + assertTrue(data[i] <= data[i + 1], "Sorted"); + } + } + + private double testQuadratic(String msg, Consumer sorter) throws Exception { + sortServer.dataElems = DATAELEMS; + double time1 = sortServer.measureTime(sortServer::randomData, sorter); + + sortServer.dataElems = DATAELEMS*2; + double time2 = sortServer.measureTime(sortServer::randomData, sorter); + + assertTrue(time1 > time2 / 6 && time1 < time2 / 2, msg + " Time O(n^2)"); + return time1; + } + + @Test + public void testMeasureTime() throws Exception { + testQuadratic("BUBBLESORT", sortServer::bubbleSort); + } +} diff --git a/app/src/test/java/ch/nuth/zhaw/exbox/ADS11_3_test.java b/app/src/test/java/ch/nuth/zhaw/exbox/ADS11_3_test.java new file mode 100644 index 0000000..391add0 --- /dev/null +++ b/app/src/test/java/ch/nuth/zhaw/exbox/ADS11_3_test.java @@ -0,0 +1,94 @@ +package ch.nuth.zhaw.exbox; + +import org.junit.jupiter.api.Test; +import java.util.function.Consumer; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; + +/** + * @author K Rege + * @version 1.00 2018/3/17 + * @version 1.01 2021/8/1 + */ +public class ADS11_3_test { + final int DATAELEMS = 10000; + SortServer sortServer = new SortServer(); + + @Test + public void testRandomData() { + sortServer.dataElems = DATAELEMS; + int[] data = sortServer.randomData(); + assertEquals(sortServer.dataElems, data.length, "Anzahl Daten"); + assertTrue(data[0] != data[1], "Distribution"); + } + + @Test + public void testAscendingData() { + sortServer.dataElems = DATAELEMS; + int[] data = sortServer.ascendingData(); + assertEquals(sortServer.dataElems, data.length, "Anzahl Daten"); + for (int i = 0; i< sortServer.dataElems-1; i++) { + assertTrue(data[i] <= data[i + 1], "Distribution"); + } + } + + @Test + public void testDescendingData() { + sortServer.dataElems = DATAELEMS; + int[] data = sortServer.descendingData(); + assertEquals(sortServer.dataElems, data.length, "Anzahl Daten"); + for (int i = 0; i< sortServer.dataElems-1; i++) { + assertTrue(data[i] >= data[i + 1], "Distribution"); + } + } + + @Test + public void testBubbleSort() { + sortServer.dataElems = DATAELEMS; + int[] data = sortServer.randomData(); + sortServer.bubbleSort(data); + for (int i = 0; i< sortServer.dataElems-1; i++) { + assertTrue(data[i] <= data[i + 1], "Sorted"); + } + } + + @Test + public void testInsertionSort() { + sortServer.dataElems = DATAELEMS; + int[] data = sortServer.randomData(); + sortServer.insertionSort(data); + for (int i = 0; i< sortServer.dataElems-1; i++) { + assertTrue(data[i] <= data[i + 1], "Sorted"); + } + } + + @Test + public void testSelectionSort() { + sortServer.dataElems = DATAELEMS; + int[] data = sortServer.randomData(); + sortServer.selectionSort(data); + for (int i = 0; i< sortServer.dataElems-1; i++) { + assertTrue(data[i] <= data[i + 1], "Sorted"); + } + } + + private double testQuadratic(String msg, Consumer sorter) throws Exception { + sortServer.dataElems = DATAELEMS; + double time1 = sortServer.measureTime(sortServer::randomData, sorter); + + sortServer.dataElems = DATAELEMS*2; + double time2 = sortServer.measureTime(sortServer::randomData, sorter); + + assertTrue(time1 > time2 / 6 && time1 < time2 / 2, msg + " Time O(n^2)"); + return time1; + } + + @Test + public void testMeasureTime() throws Exception { + double time1 = testQuadratic("BUBBLESORT", sortServer::bubbleSort); + double time2 = testQuadratic("SELECTIONSORT", sortServer::selectionSort); + double time3 = testQuadratic("INSERTIONSORT", sortServer::insertionSort); + assertTrue(time1 > 1.5 * time2, "BubbleTime > 1.5 * SelectionTime"); + assertTrue(time2 > 1.5 * time3, "SelectionTime > 1.5 * InsertionTime"); + } +}