From 6d25796bac30c946da7055e9033f34ea6f815f83 Mon Sep 17 00:00:00 2001 From: Daniel Langbein Date: Tue, 17 Dec 2024 19:14:30 +0000 Subject: [PATCH] import QuickSort --- .../uni_marburg/powersort/sort/QuickSort.java | 40 +++++++++++++++++++ .../uni_marburg/powersort/sort/SortEnum.java | 2 + .../powersort/sort/QuickSortTest.java | 7 ++++ 3 files changed, 49 insertions(+) create mode 100644 app/src/main/java/de/uni_marburg/powersort/sort/QuickSort.java create mode 100644 app/src/test/java/de/uni_marburg/powersort/sort/QuickSortTest.java diff --git a/app/src/main/java/de/uni_marburg/powersort/sort/QuickSort.java b/app/src/main/java/de/uni_marburg/powersort/sort/QuickSort.java new file mode 100644 index 0000000..815b8b7 --- /dev/null +++ b/app/src/main/java/de/uni_marburg/powersort/sort/QuickSort.java @@ -0,0 +1,40 @@ +package de.uni_marburg.powersort.sort; + +public class QuickSort { + /** + * Based on https://www.baeldung.com/java-quicksort + */ + public static void sort(Integer[] arr) { + quickSort(arr, 0, arr.length-1); + } + + private static void quickSort(Integer[] arr, int begin, int end) { + if (begin < end) { + int partitionIndex = partition(arr, begin, end); + + quickSort(arr, begin, partitionIndex-1); + quickSort(arr, partitionIndex+1, end); + } + } + + private static int partition(Integer[] arr, int begin, int end) { + int pivot = arr[end]; + int i = (begin-1); + + for (int j = begin; j < end; j++) { + if (arr[j] <= pivot) { + i++; + + int swapTemp = arr[i]; + arr[i] = arr[j]; + arr[j] = swapTemp; + } + } + + int swapTemp = arr[i+1]; + arr[i+1] = arr[end]; + arr[end] = swapTemp; + + return i+1; + } +} diff --git a/app/src/main/java/de/uni_marburg/powersort/sort/SortEnum.java b/app/src/main/java/de/uni_marburg/powersort/sort/SortEnum.java index 0ceb21a..06e346b 100644 --- a/app/src/main/java/de/uni_marburg/powersort/sort/SortEnum.java +++ b/app/src/main/java/de/uni_marburg/powersort/sort/SortEnum.java @@ -5,6 +5,7 @@ import de.uni_marburg.powersort.benchmark.NaturalOrder; public enum SortEnum { BUBBLE_SORT, + QUICK_SORT, MERGE_SORT, TIM_SORT, FINN_SORT, @@ -13,6 +14,7 @@ public enum SortEnum { public SortImpl getSortImpl() { return switch (this) { case BUBBLE_SORT -> array -> BubbleSort.sort(array, NaturalOrder.INSTANCE); + case QUICK_SORT -> array -> QuickSort.sort((Integer[]) array); // TODO rm cast case MERGE_SORT -> array -> MergeSort.legacyMergeSort(array, NaturalOrder.INSTANCE); case TIM_SORT -> array -> TimSort.sort(array, 0, array.length, NaturalOrder.INSTANCE, null, 0, 0); case FINN_SORT -> array -> FinnSort.sort(array, NaturalOrder.INSTANCE); diff --git a/app/src/test/java/de/uni_marburg/powersort/sort/QuickSortTest.java b/app/src/test/java/de/uni_marburg/powersort/sort/QuickSortTest.java new file mode 100644 index 0000000..033fb04 --- /dev/null +++ b/app/src/test/java/de/uni_marburg/powersort/sort/QuickSortTest.java @@ -0,0 +1,7 @@ +package de.uni_marburg.powersort.sort; + +public class QuickSortTest extends AbstractSortTest { + QuickSortTest() { + sortAlg = SortEnum.ASORT; + } +}