From 0ad3397ad09bd1c26da2327b579b065842fae42b Mon Sep 17 00:00:00 2001 From: Manuel Thalmann Date: Tue, 13 Dec 2022 01:35:15 +0100 Subject: [PATCH] Implement a `quickerSort` method --- .../java/ch/nuth/zhaw/exbox/SortServer.java | 27 ++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/app/src/main/java/ch/nuth/zhaw/exbox/SortServer.java b/app/src/main/java/ch/nuth/zhaw/exbox/SortServer.java index ffbd647..fb95a7b 100644 --- a/app/src/main/java/ch/nuth/zhaw/exbox/SortServer.java +++ b/app/src/main/java/ch/nuth/zhaw/exbox/SortServer.java @@ -79,11 +79,36 @@ public class SortServer implements CommandExecutor { private void quickerSort(int[] arr, int left, int right) { // To do Aufgabe 12.1 + 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) { // To do Aufgabe 12.1 - 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 boolean isSorted(int[] a) {