import QuickSort

This commit is contained in:
Daniel Langbein 2024-12-17 19:14:30 +00:00
parent 68d86d6e6b
commit 6d25796bac
Signed by: langfingaz
GPG Key ID: 6C47C753F0823002
3 changed files with 49 additions and 0 deletions

View File

@ -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;
}
}

View File

@ -5,6 +5,7 @@ import de.uni_marburg.powersort.benchmark.NaturalOrder;
public enum SortEnum { public enum SortEnum {
BUBBLE_SORT, BUBBLE_SORT,
QUICK_SORT,
MERGE_SORT, MERGE_SORT,
TIM_SORT, TIM_SORT,
FINN_SORT, FINN_SORT,
@ -13,6 +14,7 @@ public enum SortEnum {
public SortImpl getSortImpl() { public SortImpl getSortImpl() {
return switch (this) { return switch (this) {
case BUBBLE_SORT -> array -> BubbleSort.sort(array, NaturalOrder.INSTANCE); 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 MERGE_SORT -> array -> MergeSort.legacyMergeSort(array, NaturalOrder.INSTANCE);
case TIM_SORT -> array -> TimSort.sort(array, 0, array.length, NaturalOrder.INSTANCE, null, 0, 0); case TIM_SORT -> array -> TimSort.sort(array, 0, array.length, NaturalOrder.INSTANCE, null, 0, 0);
case FINN_SORT -> array -> FinnSort.sort(array, NaturalOrder.INSTANCE); case FINN_SORT -> array -> FinnSort.sort(array, NaturalOrder.INSTANCE);

View File

@ -0,0 +1,7 @@
package de.uni_marburg.powersort.sort;
public class QuickSortTest extends AbstractSortTest {
QuickSortTest() {
sortAlg = SortEnum.ASORT;
}
}