diff --git a/app/src/main/java/ch/nuth/zhaw/exbox/ParallelQuickerSortServer.java b/app/src/main/java/ch/nuth/zhaw/exbox/ParallelQuickerSortServer.java index d43eeb2..7b3f45b 100644 --- a/app/src/main/java/ch/nuth/zhaw/exbox/ParallelQuickerSortServer.java +++ b/app/src/main/java/ch/nuth/zhaw/exbox/ParallelQuickerSortServer.java @@ -26,16 +26,68 @@ public class ParallelQuickerSortServer extends Thread implements CommandExecutor if (left < right) { // TODO Aufgabe 12.3 + mid = partition(arr, left, right); + + if (mid - left > SPLIT_THRESHOLD) { + t1 = new ParallelQuickerSortServer(arr, left, mid - 1); + t1.start(); + } else { + quickerSort(arr, left, mid - 1); + } + + if (right - mid > SPLIT_THRESHOLD) { + t2 = new ParallelQuickerSortServer(arr, mid, right); + t2.start(); + } else { + quickerSort(arr, mid, right); + } + } + + if (t1 != null) { + try { + t1.join(); + } catch (InterruptedException e) {} + } + + if (t2 != null) { + try { + t2.join(); + } catch (InterruptedException e) {} } } private void quickerSort(int[] arr, int left, int right) { // TODO Aus Aufgabe 12.1 übernehmen + if (right - left < insertion_threshold) { + insertionSort(arr, left, right); + } else { + int l = partition(arr, left, right); + quickerSort(arr, left, l - 1); + quickerSort(arr, l, right); + } } private int partition(int[] arr, int left, int right) { // TODO Aus Aufgabe 12.1 übernehmen - return 0; + int pivot = arr[(left + right) / 2]; + + while (left <= right) { + while (arr[left] < pivot) { + left++; + } + + while (arr[right] > pivot) { + right--; + } + + if (left <= right) { + swap(arr, left, right); + left++; + right--; + } + } + + return left; } private void insertionSort(int[] a, int l, int r) {