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 index 815b8b7..8d3f3dd 100644 --- a/app/src/main/java/de/uni_marburg/powersort/sort/QuickSort.java +++ b/app/src/main/java/de/uni_marburg/powersort/sort/QuickSort.java @@ -1,40 +1,42 @@ package de.uni_marburg.powersort.sort; +import java.util.Comparator; + public class QuickSort { /** * Based on https://www.baeldung.com/java-quicksort */ - public static void sort(Integer[] arr) { - quickSort(arr, 0, arr.length-1); + public static void sort(final T[] arr, Comparator c) { + quickSort(arr, c, 0, arr.length - 1); } - private static void quickSort(Integer[] arr, int begin, int end) { + private static void quickSort(T[] arr, Comparator c, int begin, int end) { if (begin < end) { - int partitionIndex = partition(arr, begin, end); + int partitionIndex = partition(arr, c, begin, end); - quickSort(arr, begin, partitionIndex-1); - quickSort(arr, partitionIndex+1, end); + quickSort(arr, c, begin, partitionIndex - 1); + quickSort(arr, c, partitionIndex + 1, end); } } - private static int partition(Integer[] arr, int begin, int end) { - int pivot = arr[end]; - int i = (begin-1); + private static int partition(T[] arr, Comparator c, int begin, int end) { + T pivot = arr[end]; + int i = (begin - 1); for (int j = begin; j < end; j++) { - if (arr[j] <= pivot) { + if (c.compare(arr[j], pivot) <= 0) { i++; - int swapTemp = arr[i]; + T swapTemp = arr[i]; arr[i] = arr[j]; arr[j] = swapTemp; } } - int swapTemp = arr[i+1]; - arr[i+1] = arr[end]; + T swapTemp = arr[i + 1]; + arr[i + 1] = arr[end]; arr[end] = swapTemp; - return i+1; + 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 06e346b..8ae042a 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 @@ -14,7 +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 QUICK_SORT -> array -> QuickSort.sort(array, NaturalOrder.INSTANCE); 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);