commit
c349e21796
|
|
@ -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
|
||||||
|
}
|
||||||
|
|
@ -93,33 +93,4 @@ public class SelectionSort {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
Loading…
Reference in New Issue