【ALGO】快速排序算法复习

 

1 循环不变量 


import java.lang.reflect.Array;

public class quickSort {

    private static final int[] array = new int[]{9,-8,7,6,500,1,2,3,4};

    public static void quickSort(int[] array, int begin, int end){

        if(begin >= end){
            return;
        }

        int partition = partition(array, begin, end);

        quickSort(array, begin, partition - 1);
        quickSort(array, partition + 1, end);
    }

    private static int partition(int[] array, int begin, int end) {
        int head = begin;
        int nextIndex = begin;
        while(nextIndex++ < end){
            if(array[nextIndex] < array[head]){
                swap(array, nextIndex, head);
                head++;
            }
        }
        return head;
    }


    public static void main(String[] args) {
        quickSort(array, 0, 8);
        for (int i : array) {
            System.out.println(i);
        }
    }

    private static void swap(int[] array, int head, int tail) {
        int temp = array[head];
        array[head] = array[tail];
        array[tail] = temp;
    }
}

2 总结

  1 每次parititon划分

  2 递归迭代

上一篇:AnalyticDB for MySQL:PB级云数仓核心技术和场景解析


下一篇:【ALGO】Interior Point Methods