Add files for Exercise 11

This commit is contained in:
Manuel Thalmann 2022-12-05 21:58:04 +01:00
parent 92e901392b
commit 677b73ee6e
3 changed files with 280 additions and 0 deletions

View file

@ -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<int[]> generator, Consumer<int[]> 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<String, Consumer<int[]>> sorter = Map.of(
"BUBBLE", this::bubbleSort,
"INSERTION", this::insertionSort,
"SELECTION", this::selectionSort,
"STREAM", this::streamSort
);
Map<String, Supplier<int[]>> 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));
}
}

View file

@ -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<int[]> 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);
}
}

View file

@ -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<int[]> 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");
}
}