Java 快速排序

过程分析:

未经过处理的数组

[44, 70, 93, 2, 29, 49, 0, 35, 74, 83]

第一次位移后

[44, 2, 93, 70, 29, 49, 0, 35, 74, 83]

第二次位移

[44, 2, 29, 70, 93, 49, 0, 35, 74, 83]

第三次位移

[44, 2, 29, 0, 93, 49, 70, 35, 74, 83]

第四次位移

[44, 2, 29, 0, 35, 49, 70, 93, 74, 83]

完成一次划分后交换44与35位置,保持数组有序

[35, 2, 29, 0, 44, 49, 70, 93, 74, 83]

划分后[35, 2, 29, 0]  44  [49, 70, 93, 74, 83]

递归调用

分别对[35, 2, 29, 0]与[49, 70, 93, 74, 83]再次进行划分

[35, 2, 29, 0] 划分为 [0,2, 29]   35   []

[49, 70, 93, 74, 83] 划分为 []  49  [70, 93, 74, 83]

此时整个数组状态 :[0,2, 29]   35   []  44 []  49  [70, 93, 74, 83]

对[0,2, 29] 与 [70, 93, 74, 83]继续进行划分

[0,2, 29] 划分为[] 0 [2,29]

[70, 93, 74, 83] 划分为 [] 70 [93, 74, 83]

此时整个数组状态 : [] 0 [2,29] 35 [] 44 [] 49 [] 70 [93, 74, 83]

对[2,29]与[93, 74, 83]继续划分

[2,29] 划分为 [] 2 [29]

[93, 74, 83] 划分为 [83,74] 93 []

此时整个数组状态 :[] 0 [] 2 [29] 35 [] 44 [] 49 [] 70 [83,74] 93 []

最后对[83,74]进行划分

[83,74] 划分为 [74] 83 []

最终数组状态 : [] 0 [] 2 [29] 35 [] 44 [] 49 [] 70 [74] 83 [] 93 []

排序后的数组:[0, 2, 29, 35, 44, 49, 70, 74, 83, 93]

import java.util.Arrays;

public class QuickSort {
    //交换数组元素
    public static void swap(int[] array, int i, int j) {
        if (i == j)
            return;
        int temp = array[i];
        array[i] = array[j];
        array[j] = temp;
    }

    /**
     * 以起点元素作为中心点,依次与子数组中每个元素比较,将比中心点小的元素移至数组左边, 
     * 并记录中心轴正确位置,最后交换位置使得数组保持有序状态。
     * 
     * @param array
     *            待划分的数组
     * @param low
     *            起始位置
     * @param high
     *            结束位置
     * @return partition_position 划分后中心轴的位置
     */
    public static int partition(int[] array, int low, int high) {
        // 当前划分位置
        int partition_position = low;
        // 中心轴
        int pivot = array[partition_position];
        for (int i = low + 1; i < high + 1; i++) {
            if (array[i] < pivot) {
                partition_position++;
                swap(array, partition_position, i);
            }

        }
        swap(array, partition_position, low);//最后将中心轴移至正确位置
        return partition_position;
    }

    public static void quicksort(int[] array) {
        quicksort(array, 0, array.length - 1);//注意是length - 1不是length
    }

    public static void quicksort(int[] array, int low, int high) {
        if (low < high) {
            int pivot = partition(array, low, high);
            quicksort(array, low, pivot - 1);
            quicksort(array, pivot + 1, high);
        }

    }

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        int[] data = new int[30];
        for (int i = 0; i < 30; i++) {
            data[i] = (int) (Math.random() * 100);
        }
        // 排序前
        System.out.println(Arrays.toString(data));
        quicksort(data);
        // 排序后
        System.out.print(Arrays.toString(data));
    }

}

执行结果:

[30, 59, 77, 61, 55, 16, 97, 4, 46, 32, 30, 99, 19, 59, 66, 52, 38, 60, 19, 57, 85, 56, 36, 42, 92, 47, 25, 49, 3, 3]
[3, 3, 4, 16, 19, 19, 25, 30, 30, 32, 36, 38, 42, 46, 47, 49, 52, 55, 56, 57, 59, 59, 60, 61, 66, 77, 85, 92, 97, 99]

原文地址:https://www.cnblogs.com/maple42/p/4080258.html