编辑代码

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 + " ");
        }
    }
}