public class QuickSort { public static void quickSort(int[] arr, int start, int end) { if (start < end) { int pivotIndex = partition(arr, start, end); quickSort(arr, start, pivotIndex - 1); quickSort(arr, pivotIndex + 1, end); } }

private static int partition(int[] arr, int start, int end) {
    int pivot = arr[end];
    int i = start - 1;
    for (int j = start; j < end; j++) {
        if (arr[j] < pivot) {
            i++;
            swap(arr, i, j);
        }
    }
    swap(arr, i + 1, end);
    return i + 1;
}

private static void swap(int[] arr, int i, int j) {
    int temp = arr[i];
    arr[i] = arr[j];
    arr[j] = temp;
}

}

Java 快速排序算法实现代码详解

原文地址: https://www.cveoy.top/t/topic/outF 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录