From a410e816f414f8c29062157ef02ea10645e083dd Mon Sep 17 00:00:00 2001 From: Manuel Thalmann Date: Tue, 13 Dec 2022 01:48:38 +0100 Subject: [PATCH] Implement the `ParallelQuickerSortServer` --- .../zhaw/exbox/ParallelQuickerSortServer.java | 54 ++++++++++++++++++- 1 file changed, 53 insertions(+), 1 deletion(-) 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) {