快速排序

2020-12-12  本文已影响0人  Lnstark
    public static void main(String[] args) {
        int len = 100;
        int arr[] = new int[len];
        Random r = new Random();
        for (int i = 0; i < len; i++) {
            arr[i] = r.nextInt(len);
        }

        quickSort1(arr, 0, len - 1);
        System.out.println(Arrays.toString(arr));
    }

   
    private static void quickSort1(int[] a, int left, int right) {
        int l = left, r = right;
        if (l >= r) return;
        int temp = a[l];
        while (l < r) {
            while (l < r && a[r] > temp) r--;
            if (l < r) a[l++] = a[r];
            while (l < r && a[l] <= temp) l++;
            if (l < r) a[r--] = a[l];
        }
        a[l] = temp;
        quickSort1(a, left, l - 1);
        quickSort1(a, l + 1, right);
    }

背诵并默写全文quickSort1方法

上一篇 下一篇

猜你喜欢

热点阅读