Ten years of hard work in JAVA - quick sorting algorithm

Quick sort (quick)

The core idea : Find a base point, divide the sequence into two parts, one part is greater than the base point, and the other part is smaller than the base point, repeat the above operation for the separated sequence, and iterate until the sequence is indivisible.
**Time complexity O(nLog(n)) Assume n is always divided in half, assuming the maximum number of divisions is M, then there are 2^m=n —> m=log(n), and each layer can be exchanged up to n times. So time complexity = O(nlong(n)) **
Space complexity O(log(n))
stability (unstable) For example, 1 1 1 1 1 The first 1 will be exchanged

 public static void quickSort(int[] array, int min, int max) {
    
    
        if (max > min) {
    
    
            Integer partition = partition(array, min, max);
            quickSort(array, min, partition - 1);
            quickSort(array, partition + 1, max);
        }
    }

    //找基点函数
    static Integer partition(int[] array, int min, int max) {
    
    
        Integer current = array[min];
        while (min < max) {
    
    
            while (min < max && array[max] >= current) max--;
            array[min] = array[max];
            while (min < max && array[min] < current) min++;
            array[max] = array[min];
        }
        array[min] = current;
        return min;
    }

Guess you like

Origin blog.csdn.net/weixin_43485737/article/details/133338094