public class QuickSort {
public static void quickSort(int[] arr, int low, int high) {
if (low < high) {
int pivotIndex = partition(arr, low, high);
quickSort(arr, low, pivotIndex - 1);
quickSort(arr, pivotIndex + 1, high);
}
}
private static int partition(int[] arr, int low, int high) {
int pivot = arr[low];
int left = low + 1;
int right = high;
while (true) {
while (left <= right && arr[left] < pivot) {
left++;
}
while (left <= right && arr[right] > pivot) {
right--;
}
if (left > right) {
break;
}
int temp = arr[left];
arr[left] = arr[right];
arr[right] = temp;
left++;
right--;
}
int temp = arr[low];
arr[low] = arr[right];
arr[right] = temp;
return right;
}
public static void main(String[] args) {
int[] arr = {5, 2, 8, 9, 1, 3};
quickSort(arr, 0, arr.length - 1);
System.out.println("排序结果:");
for (int num : arr) {
System.out.print(num + " ");
}
}
}