diff --git a/PR2pvl/src/QuickSort.java b/PR2pvl/src/QuickSort.java new file mode 100644 index 0000000..aa24b9a --- /dev/null +++ b/PR2pvl/src/QuickSort.java @@ -0,0 +1,51 @@ + + public class QuickSort { + + public static void quickSort(int[] arr, int low, int high) { + if (low < high) { + // Pivot-Index bestimmen + int pivotIndex = partition(arr, low, high); + + // Linke Seite sortieren + quickSort(arr, low, pivotIndex - 1); + + // Rechte Seite sortieren + quickSort(arr, pivotIndex + 1, high); + } + } + + private static int partition(int[] arr, int low, int high) { + int pivot = arr[high]; // letztes Element als Pivot + int i = low - 1; + + for (int j = low; j < high; j++) { + if (arr[j] <= pivot) { + i++; + + // Tauschen + int temp = arr[i]; + arr[i] = arr[j]; + arr[j] = temp; + } + } + + // Pivot an richtige Position setzen + int temp = arr[i + 1]; + arr[i + 1] = arr[high]; + arr[high] = temp; + + return i + 1; + } + + // Test + public static void main(String[] args) { + int[] arr = {8, 3, 1, 7, 0, 10, 2}; + + quickSort(arr, 0, arr.length - 1); + + for (int num : arr) { + System.out.print(num + " "); + } + } + //End of Class + } diff --git a/PR2pvl/src/SelectionSort.java b/PR2pvl/src/SelectionSort.java index c4f065e..35bcab1 100644 --- a/PR2pvl/src/SelectionSort.java +++ b/PR2pvl/src/SelectionSort.java @@ -93,33 +93,4 @@ public class SelectionSort { } - - - - - - - - - - - - - - - - - - - - - - - - - - - - - } \ No newline at end of file