Compare commits
5 commits
98ee51154b
...
27d815812e
Author | SHA1 | Date | |
---|---|---|---|
Manuel Thalmann | 27d815812e | ||
Manuel Thalmann | 6759e4e9eb | ||
Manuel Thalmann | 55eedb5599 | ||
Manuel Thalmann | 0b11be8442 | ||
Manuel Thalmann | cae8f549ba |
|
@ -0,0 +1,152 @@
|
|||
package ch.nuth.zhaw.exbox;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
public class ParallelQuickerSortServer extends Thread implements CommandExecutor {
|
||||
int[] arr, sourceArr;
|
||||
int left, right;
|
||||
static int dataElems = 100000;
|
||||
static int insertion_threshold = 100;
|
||||
private final int SPLIT_THRESHOLD = 10000;
|
||||
private final int DATARANGE = 10000000;
|
||||
|
||||
public ParallelQuickerSortServer() {}
|
||||
|
||||
public ParallelQuickerSortServer(int[] arr, int left, int right) {
|
||||
this.arr = arr;
|
||||
this.left = left;
|
||||
this.right = right;
|
||||
}
|
||||
|
||||
public void run() {
|
||||
int mid = 0;
|
||||
Thread t1 = null;
|
||||
Thread t2 = null;
|
||||
|
||||
if (left < right) {
|
||||
// TODO Aufgabe 12.3
|
||||
}
|
||||
}
|
||||
|
||||
private void quickerSort(int[] arr, int left, int right) {
|
||||
// TODO Aus Aufgabe 12.1 übernehmen
|
||||
}
|
||||
|
||||
private int partition(int[] arr, int left, int right) {
|
||||
// TODO Aus Aufgabe 12.1 übernehmen
|
||||
return 0;
|
||||
}
|
||||
|
||||
private void insertionSort(int[] a, int l, int r) {
|
||||
for (int k = l + 1; k < r + 1; k++) {
|
||||
if (a[k] < a[k - 1]) {
|
||||
int x = a[k];
|
||||
int i;
|
||||
for (i = k; ((i > 0) && (a[i - 1] > x)); i--) {
|
||||
a[i] = a[i - 1];
|
||||
}
|
||||
a[i] = x;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int[] randomData() {
|
||||
int[] a = new int[dataElems];
|
||||
for (int i = 0; i < a.length; i++) {
|
||||
a[i] = (int) (Math.random() * DATARANGE);
|
||||
}
|
||||
return a;
|
||||
}
|
||||
|
||||
public int[] ascendingData() {
|
||||
int[] a = new int[dataElems];
|
||||
for (int i = 0; i < a.length; i++) {
|
||||
a[i] = i;
|
||||
}
|
||||
return a;
|
||||
}
|
||||
|
||||
public int[] descendingData() {
|
||||
int[] a = new int[dataElems];
|
||||
for (int i = 0; i < a.length; i++) {
|
||||
a[i] = a.length - i;
|
||||
}
|
||||
return a;
|
||||
}
|
||||
|
||||
private void swap(int[] arr, int i, int j) {
|
||||
int swapElement;
|
||||
|
||||
swapElement = arr[i];
|
||||
arr[i] = arr[j];
|
||||
arr[j] = swapElement;
|
||||
}
|
||||
|
||||
private boolean isSorted(int[] a) {
|
||||
for (int i = 0; i < a.length - 1; i++) {
|
||||
if (a[i] > a[i + 1]) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
private double measureTime(Supplier<int[]> generator) throws Exception {
|
||||
double elapsed = 0;
|
||||
|
||||
sourceArr = generator.get();
|
||||
|
||||
long startTime = System.currentTimeMillis();
|
||||
long endTime = startTime;
|
||||
int count = 0;
|
||||
while (endTime < startTime + 10000) {
|
||||
System.arraycopy(sourceArr, 0, arr, 0, arr.length);
|
||||
Thread rootThread = new ParallelQuickerSortServer(arr, 0, arr.length - 1);
|
||||
rootThread.start();
|
||||
try {
|
||||
rootThread.join();
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
count++;
|
||||
endTime = System.currentTimeMillis();
|
||||
}
|
||||
elapsed = (double) (endTime - startTime) / count;
|
||||
|
||||
if (!isSorted(arr)) throw new Exception("ERROR not eorted");
|
||||
return elapsed;
|
||||
}
|
||||
|
||||
public String execute(String arg) {
|
||||
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[1]);
|
||||
arr = new int[dataElems];
|
||||
sourceArr = new int[dataElems];
|
||||
insertion_threshold = Integer.parseInt(args[2]);
|
||||
try {
|
||||
try {
|
||||
double time = measureTime(generator.get(args[0]));
|
||||
return arg + " " + time + " ms\n";
|
||||
} catch (Exception ex) {
|
||||
return arg + " " + ex.getMessage();
|
||||
}
|
||||
} catch (Exception ex) {
|
||||
return arg + " " + ex.getMessage();
|
||||
}
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
ParallelQuickerSortServer sorter = new ParallelQuickerSortServer();
|
||||
String sort;
|
||||
sort = "RANDOM 10000 100"; System.out.println(sorter.execute(sort));
|
||||
sort = "ASC 10000 100"; System.out.println(sorter.execute(sort));
|
||||
sort = "DESC 10000 100"; System.out.println(sorter.execute(sort));
|
||||
}
|
||||
}
|
|
@ -8,14 +8,15 @@ import java.util.function.Supplier;
|
|||
public class SortServer implements CommandExecutor {
|
||||
private final int DATARANGE = 10000000;
|
||||
public int dataElems; // number of data
|
||||
public int insertion_threshold = 50;
|
||||
|
||||
public void swap(int[] a, int i, int j) {
|
||||
private void swap(int[] a, int i, int j) {
|
||||
int h = a[i];
|
||||
a[i] = a[j];
|
||||
a[j] = h;
|
||||
}
|
||||
|
||||
public void bubbleSort(int[] a) {
|
||||
private void bubbleSort(int[] a) {
|
||||
for (int max = a.length - 1; max >= 0; max--) {
|
||||
boolean isSorted = true;
|
||||
|
||||
|
@ -32,7 +33,7 @@ public class SortServer implements CommandExecutor {
|
|||
}
|
||||
}
|
||||
|
||||
public void insertionSort(int[] a) {
|
||||
private void insertionSort(int[] a) {
|
||||
for (int k = 0; k < a.length; k++) {
|
||||
int item = a[k];
|
||||
int newIndex;
|
||||
|
@ -45,7 +46,20 @@ public class SortServer implements CommandExecutor {
|
|||
}
|
||||
}
|
||||
|
||||
public void selectionSort(int[] a) {
|
||||
private void insertionSort(int[] a, int l, int r) {
|
||||
for (int k = l + 1; k < r + 1; k++) {
|
||||
if (a[k] < a[k - 1]) {
|
||||
int x = a[k];
|
||||
int i;
|
||||
for (i = k; ((i > 0) && (a[i - 1] > x)); i--) {
|
||||
a[i] = a[i - 1];
|
||||
}
|
||||
a[i] = x;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void selectionSort(int[] a) {
|
||||
// TODO Implement Aufgabe 3
|
||||
for (int i = 0; i < a.length; i++) {
|
||||
int minIndex = i;
|
||||
|
@ -62,13 +76,26 @@ public class SortServer implements CommandExecutor {
|
|||
}
|
||||
}
|
||||
|
||||
public void streamSort(int[] a) {
|
||||
private 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) {
|
||||
void quickerSort(int[] a){
|
||||
quickerSort(a, 0, a.length-1);
|
||||
}
|
||||
|
||||
private void quickerSort(int[] arr, int left, int right) {
|
||||
// To do Aufgabe 12.1
|
||||
}
|
||||
|
||||
private int partition (int[] arr, int left, int right) {
|
||||
// To do Aufgabe 12.1
|
||||
return 0;
|
||||
}
|
||||
|
||||
private boolean isSorted(int[] a) {
|
||||
for (int i = a.length - 2; i > 0; i--) {
|
||||
if (a[i] > a[i + 1]) {
|
||||
return false;
|
||||
|
@ -81,7 +108,7 @@ public class SortServer implements CommandExecutor {
|
|||
public int[] randomData() {
|
||||
int[] a = new int[dataElems];
|
||||
|
||||
for (int i = 0; i < dataElems; i++) {
|
||||
for (int i = 0; i < a.length; i++) {
|
||||
a[i] = (int) (Math.random() * DATARANGE);
|
||||
}
|
||||
|
||||
|
@ -91,7 +118,7 @@ public class SortServer implements CommandExecutor {
|
|||
public int[] ascendingData() {
|
||||
int[] a = new int[dataElems];
|
||||
|
||||
for (int i = 0; i < dataElems; i++) {
|
||||
for (int i = 0; i < a.length; i++) {
|
||||
a[i] = i;
|
||||
}
|
||||
|
||||
|
@ -101,8 +128,8 @@ public class SortServer implements CommandExecutor {
|
|||
public int[] descendingData() {
|
||||
int[] a = new int[dataElems];
|
||||
|
||||
for (int i = 0; i < dataElems; i++) {
|
||||
a[i] = dataElems - (i + 1);
|
||||
for (int i = 0; i < a.length; i++) {
|
||||
a[i] = a.length - i;
|
||||
}
|
||||
|
||||
return a;
|
||||
|
@ -113,7 +140,7 @@ public class SortServer implements CommandExecutor {
|
|||
// 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 {
|
||||
private double measureTime(Supplier<int[]> generator, Consumer<int[]> sorter) throws Exception {
|
||||
double elapsed = 0;
|
||||
|
||||
int[] a = generator.get();
|
||||
|
@ -125,8 +152,8 @@ public class SortServer implements CommandExecutor {
|
|||
// TODO Implement Aufgabe 1 und 2 (Tipp: siehe Consumer für Aufruf von
|
||||
int count = 0;
|
||||
|
||||
while (endTime < startTime + 1000) {
|
||||
System.arraycopy(a, 0, b, 0, dataElems);
|
||||
while (endTime < startTime + 10000) {
|
||||
System.arraycopy(a, 0, b, 0, a.length);
|
||||
sorter.accept(b);
|
||||
count++;
|
||||
endTime = System.currentTimeMillis();
|
||||
|
@ -134,8 +161,11 @@ public class SortServer implements CommandExecutor {
|
|||
// Sortiermethode)
|
||||
|
||||
elapsed = (double) ((endTime - startTime) / count);
|
||||
if (!isSorted(b))
|
||||
|
||||
if (!isSorted(b)) {
|
||||
throw new Exception("ERROR not sorted");
|
||||
}
|
||||
|
||||
return elapsed;
|
||||
}
|
||||
|
||||
|
@ -144,7 +174,8 @@ public class SortServer implements CommandExecutor {
|
|||
"BUBBLE", this::bubbleSort,
|
||||
"INSERTION", this::insertionSort,
|
||||
"SELECTION", this::selectionSort,
|
||||
"STREAM", this::streamSort);
|
||||
"STREAM", this::streamSort,
|
||||
"QUICKERSORT", this::quickerSort);
|
||||
Map<String, Supplier<int[]>> generator = Map.of(
|
||||
"RANDOM", this::randomData,
|
||||
"ASC", this::ascendingData,
|
||||
|
@ -152,6 +183,11 @@ public class SortServer implements CommandExecutor {
|
|||
|
||||
String[] args = arg.toUpperCase().split(" ");
|
||||
dataElems = Integer.parseInt(args[2]);
|
||||
|
||||
if (args.length >= 4) {
|
||||
insertion_threshold = Integer.parseInt(args[3]);
|
||||
}
|
||||
|
||||
try {
|
||||
double time = measureTime(generator.get(args[1]), sorter.get(args[0]));
|
||||
return arg + " " + time + " ms\n";
|
||||
|
@ -159,4 +195,20 @@ public class SortServer implements CommandExecutor {
|
|||
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 = "QUICKERSORT RANDOM 10000 100"; System.out.println(sorter.execute(sort));
|
||||
sort = "STREAM 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));
|
||||
sort = "QUICKERSORT ASC 10000 100"; System.out.println(sorter.execute(sort));
|
||||
sort = "STREAM ASC 10000"; System.out.println(sorter.execute(sort));
|
||||
}
|
||||
}
|
||||
|
|
64
app/src/test/java/ch/nuth/zhaw/exbox/ADS12_1_test.java
Normal file
64
app/src/test/java/ch/nuth/zhaw/exbox/ADS12_1_test.java
Normal file
|
@ -0,0 +1,64 @@
|
|||
package ch.nuth.zhaw.exbox;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
public class ADS12_1_test {
|
||||
SortServer sortServer = new SortServer();
|
||||
final int DATAELEMS = 10000;
|
||||
|
||||
@Test
|
||||
public void testRandomData() {
|
||||
sortServer.dataElems = DATAELEMS;
|
||||
sortServer.insertion_threshold = 50;
|
||||
int[] data = sortServer.randomData();
|
||||
sortServer.quickerSort(data);
|
||||
for (int i = 0; i< sortServer.dataElems - 1; i++) {
|
||||
assertTrue(data[i] <= data[i + 1], "Random Data");
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAscendingData() {
|
||||
sortServer.dataElems = DATAELEMS;
|
||||
sortServer.insertion_threshold = 50;
|
||||
int[] data = sortServer.ascendingData();
|
||||
sortServer.quickerSort(data);
|
||||
for (int i = 0; i< sortServer.dataElems - 1; i++) {
|
||||
assertTrue(data[i] <= data[i + 1], "ASC Data");
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDecendingData() {
|
||||
sortServer.dataElems = DATAELEMS;
|
||||
sortServer.insertion_threshold = 50;
|
||||
int[] data = sortServer.descendingData();
|
||||
sortServer.quickerSort(data);
|
||||
for (int i = 0; i< sortServer.dataElems - 1; i++) {
|
||||
assertTrue(data[i] <= data[i + 1], "DESC Data");
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCompleteData() {
|
||||
boolean found;
|
||||
sortServer.dataElems = DATAELEMS;
|
||||
sortServer.insertion_threshold = 50;
|
||||
int[] data = sortServer.randomData();
|
||||
int[] original = new int[data.length];
|
||||
System.arraycopy(data, 0, original, 0, data.length);
|
||||
sortServer.quickerSort(data);
|
||||
for (int i = 0; i <= sortServer.dataElems - 1; i++) {
|
||||
found = false;
|
||||
for (int j = 0; j <= sortServer.dataElems - 1; j++) {
|
||||
if (data[i] == original[j]) {
|
||||
found = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
assertTrue(found, "Data not found after sort");
|
||||
}
|
||||
}
|
||||
}
|
88
app/src/test/java/ch/nuth/zhaw/exbox/ADS12_3_test.java
Normal file
88
app/src/test/java/ch/nuth/zhaw/exbox/ADS12_3_test.java
Normal file
|
@ -0,0 +1,88 @@
|
|||
package ch.nuth.zhaw.exbox;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
public class ADS12_3_test {
|
||||
ParallelQuickerSortServer sortServer = new ParallelQuickerSortServer();
|
||||
final int DATAELEMS = 10000;
|
||||
|
||||
@Test
|
||||
public void testRandomData() {
|
||||
ParallelQuickerSortServer.dataElems = DATAELEMS;
|
||||
ParallelQuickerSortServer.insertion_threshold = 50;
|
||||
int[] data = sortServer.randomData();
|
||||
Thread rootThread = new ParallelQuickerSortServer(data, 0, data.length - 1);
|
||||
rootThread.start();
|
||||
try {
|
||||
rootThread.join();
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
for (int i = 0; i< ParallelQuickerSortServer.dataElems - 1; i++) {
|
||||
assertTrue(data[i] <= data[i + 1], "Sorted");
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAscendingData() {
|
||||
ParallelQuickerSortServer.dataElems = DATAELEMS;
|
||||
ParallelQuickerSortServer.insertion_threshold = 50;
|
||||
int[] data = sortServer.ascendingData();
|
||||
Thread rootThread = new ParallelQuickerSortServer(data, 0, data.length - 1);
|
||||
rootThread.start();
|
||||
try {
|
||||
rootThread.join();
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
for (int i = 0; i< ParallelQuickerSortServer.dataElems - 1; i++) {
|
||||
assertTrue(data[i] <= data[i + 1], "Sorted");
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDescendingData() {
|
||||
ParallelQuickerSortServer.dataElems = DATAELEMS;
|
||||
ParallelQuickerSortServer.insertion_threshold = 50;
|
||||
int[] data = sortServer.descendingData();
|
||||
Thread rootThread = new ParallelQuickerSortServer(data, 0, data.length - 1);
|
||||
rootThread.start();
|
||||
try {
|
||||
rootThread.join();
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
for (int i = 0; i< ParallelQuickerSortServer.dataElems - 1; i++) {
|
||||
assertTrue(data[i] <= data[i + 1], "Sorted");
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCompleteData() {
|
||||
boolean found;
|
||||
ParallelQuickerSortServer.dataElems = DATAELEMS;
|
||||
ParallelQuickerSortServer.insertion_threshold = 50;
|
||||
int[] data = sortServer.ascendingData();
|
||||
int[] original = new int[data.length];
|
||||
System.arraycopy(data, 0, original, 0, data.length);
|
||||
Thread rootThread = new ParallelQuickerSortServer(data, 0, data.length - 1);
|
||||
rootThread.start();
|
||||
try {
|
||||
rootThread.join();
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
for (int i = 0; i <= ParallelQuickerSortServer.dataElems - 1; i++) {
|
||||
found = false;
|
||||
for (int j = 0; j <= ParallelQuickerSortServer.dataElems - 1; j++) {
|
||||
if (data[i] == original[j]) {
|
||||
found = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
assertTrue(found, "Data not found after sort");
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue