From 7c4ad841028044667f7a614fed3078ed268341a4 Mon Sep 17 00:00:00 2001 From: schrom01 Date: Mon, 12 Dec 2022 20:05:22 +0100 Subject: [PATCH] solved Lab --- .../zhaw/ads/ParallelQuickerSortServer.java | 51 +++++++++++++++++++ src/ch/zhaw/ads/SortServer.java | 28 +++++++++- 2 files changed, 77 insertions(+), 2 deletions(-) diff --git a/src/ch/zhaw/ads/ParallelQuickerSortServer.java b/src/ch/zhaw/ads/ParallelQuickerSortServer.java index b6d9a44..0378875 100644 --- a/src/ch/zhaw/ads/ParallelQuickerSortServer.java +++ b/src/ch/zhaw/ads/ParallelQuickerSortServer.java @@ -26,14 +26,65 @@ 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) { + e.printStackTrace(); + } + } + if(t2 != null) { + try { + t2.join(); + } catch (InterruptedException e) { + e.printStackTrace(); + } } } private void quickerSort(int[] arr, int left, int right) { + if(arr.length <= 50) { + insertionSort(arr, left, right); + } else { + if(left < right) { + int mid = partition(arr, left, right); + quickerSort(arr, left, mid - 1); + quickerSort(arr, mid, right); + } + } // TODO Aus Aufgabe 12.1 übernehmen } private int partition(int[] arr, int left, int right) { + 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; // TODO Aus Aufgabe 12.1 übernehmen } diff --git a/src/ch/zhaw/ads/SortServer.java b/src/ch/zhaw/ads/SortServer.java index d6f8d07..30a6d78 100644 --- a/src/ch/zhaw/ads/SortServer.java +++ b/src/ch/zhaw/ads/SortServer.java @@ -75,11 +75,35 @@ public class SortServer implements CommandExecutor { } private void quickerSort(int[] arr, int left, int right) { - // To do Aufgabe 12.1 + if(arr.length <= 50) { + insertionSort(arr); + } else { + if(left < right) { + int mid = partition(arr, left, right); + quickerSort(arr, left, mid - 1); + quickerSort(arr, mid, right); + } + } + // Todo Aufgabe 12.1 } private int partition (int[] arr, int left, int right) { - // To do Aufgabe 12.1 + 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; + // Todo Aufgabe 12.1 } private boolean isSorted(int[] a) {