定义一个partition()方法,找到数组中第一个元素的正确位置,返回索引p,即p前面的元素都比它小,p后面的元素都比它大

然后再递归调用快速排序,对左右两个分区间进行同样的操作,最后完成排序

import java.util.Arrays;

public class Algorithm {

    public static void main(String[] args) {

        Integer[] arr = {3, 2, 5, 1, 0};
        QuickSort.sort(arr);

        System.out.println(Arrays.toString(arr));
    }
}

class QuickSort {

    private QuickSort(){}

    public static<E extends Comparable<E>> void sort(E[] arr){

        sort(arr, 0, arr.length - 1);
    }

    public static<E extends Comparable<E>> void sort(E[] arr, int left, int right){

        if (left >= right){
            return;
        }

        int p = partition(arr, left, right);

        sort(arr, left, p - 1);
        sort(arr, p + 1, right);
    }

    /**
     * 数组划分为两个区间,以元素arr[left]为界
     * 循环不变量:arr[left + 1, j] < arr[left],arr[j + 1, i] >= arr[left]
     */
    public static <E extends Comparable<E>> int partition(E[] arr, int left, int right){

        int j = left;
        E temp;

        for (int i = left + 1; i <= right; i++) {

            /**
             * 如果有元素比arr[left]小,那就将其加到左边,否则不动
             */
            if (arr[i].compareTo(arr[left]) < 0){

                j++;
                temp = arr[i];
                arr[i] = arr[j];
                arr[j] = temp;
            }
        }

        /**
         * 最后,arr[left]应该放在j的位置
         * 此时,左区间arr[left, j - 1] < arr[j],右区间arr[j + 1, right] > arr[j]
         */
        temp = arr[j];
        arr[j] = arr[left];
        arr[left] = temp;

        return j;
    }
}

归并排序法和普通快速排序法性能比较

import java.util.Arrays;
import java.util.Random;

public class Algorithm {

    public static void main(String[] args) {

        Integer[] testScale = {10000, 100000};

        for (Integer n : testScale) {

            Integer[] randomArr = ArrayGenerator.generatorRandomArray(n, n);

            Integer[] sortedArr = ArrayGenerator.generatorSortedArray(n, n);

            Integer[] arr1 = Arrays.copyOf(randomArr, randomArr.length);
            Integer[] arr3 = Arrays.copyOf(randomArr, randomArr.length);

            Integer[] arr2 = Arrays.copyOf(sortedArr, sortedArr.length);
            Integer[] arr4 = Arrays.copyOf(sortedArr, sortedArr.length);

            System.out.println("测试随机数组排序性能");
            System.out.println();

            Verify.testTime("MergeSort", arr1);
            Verify.testTime("QuickSort", arr3);

            System.out.println();

            System.out.println("测试有序数组排序性能");
            System.out.println();

            Verify.testTime("MergeSort", arr2);
            Verify.testTime("QuickSort", arr4);

            System.out.println();
        }
    }
}

class QuickSort {

    private QuickSort(){}

    public static<E extends Comparable<E>> void sort(E[] arr){

        sort(arr, 0, arr.length - 1);
    }

    public static<E extends Comparable<E>> void sort(E[] arr, int left, int right){

        if (left >= right){
            return;
        }

        int p = partition(arr, left, right);

        sort(arr, left, p - 1);
        sort(arr, p + 1, right);
    }

    public static <E extends Comparable<E>> int partition(E[] arr, int left, int right){

        int j = left;
        E temp;

        for (int i = left + 1; i <= right; i++) {

            if (arr[i].compareTo(arr[left]) < 0){

                j++;
                temp = arr[i];
                arr[i] = arr[j];
                arr[j] = temp;
            }
        }

        temp = arr[j];
        arr[j] = arr[left];
        arr[left] = temp;

        return j;
    }
}

class MergeSort {

    private MergeSort(){}

    public static<E extends Comparable<E>> void sort(E[] arr){

        E[] temp = Arrays.copyOf(arr, arr.length);

        sort(arr, 0, arr.length - 1, temp);
    }

    private static<E extends Comparable<E>> void sort(E[] arr, int left, int right, E[] temp){

        if (left >= right){
            return;
        }

        int mid = left + (right - left) / 2;

        sort(arr, left, mid, temp);
        sort(arr, mid + 1, right, temp);

        if (arr[mid].compareTo(arr[mid + 1]) > 0) {
            merge(arr, left, mid, right, temp);
        }
    }

    public static<E extends Comparable<E>> void merge(E[] arr, int left, int mid, int right, E[] temp) {

        int i = left;
        int j = mid + 1;

        System.arraycopy(arr, left, temp, left, right - left + 1);

        for (int n = left; n < right + 1; n++) {

            if (i == mid + 1){
                arr[n] = temp[j];
                j++;
            }
            else if (j == right + 1) {
                arr[n] = temp[i];
                i++;
            }
            else if (temp[i].compareTo(temp[j]) <= 0) {
                arr[n] = temp[i];
                i++;
            }
            else{
                arr[n] = temp[j];
                j++;
            }
        }
    }
}

class ArrayGenerator {

    private ArrayGenerator (){}

    public static Integer[] generatorRandomArray (Integer n, Integer maxBound){

        Integer[] arr = new Integer[n];

        Random random = new Random();

        for (int i = 0; i < n; i++) {

            arr[i] = random.nextInt(maxBound);
        }

        return arr;
    }

    public static Integer[] generatorSortedArray (Integer n, Integer maxBound){

        Integer[] arr = new Integer[n];

        for (int i = 0; i < n; i++) {

            arr[i] = i;
        }

        return arr;
    }
}

class Verify {

    private Verify (){}

    public static<E extends Comparable<E>> boolean isSorted(E[] arr){

        for (int i = 0; i < arr.length - 1; i++) {
            if (arr[i].compareTo(arr[i + 1]) > 0) {
                return false;
            }
        }

        return true;
    }

    public static<E extends Comparable<E>> void testTime(String AlgorithmName, E[] arr) {

        long startTime = System.nanoTime();

        if (AlgorithmName.equals("MergeSort")) {
            MergeSort.sort(arr);
        }

        if (AlgorithmName.equals("QuickSort")) {
            QuickSort.sort(arr);
        }

        long endTime = System.nanoTime();

        if (!Verify.isSorted(arr)){
            throw new RuntimeException(AlgorithmName + "算法排序失败!");
        }

        System.out.println(String.format("%s算法,测试用例为%d,执行时间:%f秒", AlgorithmName, arr.length, (endTime - startTime) / 1000000000.0));
    }
}