[Know] sort quick sort Detailed

Quick Sort ideas

1, to select the reference value, and the first mark, identifying the end of the low, High;

2, the forward start determination, when the value is greater than the high reference value, high--, or replace the low value of high value;

3, front to back and then determining, when the value is less than the low reference value, low ++, or to replace a high value to low value;

4, the reference value is assigned to low;

5, when the low <high ​​when recursively calling steps 2 to 4;

Quick Sort understand

Everyone understand the difference, some say fast row belongs exchange sort, merge sort was said to be, are justified. I understand that while stock piles fast row, while the exchange. In standard reference value th greater than the reference value on the right, on the left than the reference value. Each sub-stack is a sort time, until about each stack only 0 or 1 values ​​are sorted ends.

Complexity of Quick Sort

Quick drain of time and the complexity of the selected reference value of the absolute relationship, when the reference value is exactly the value of all the elements of middle size, he is very average sort, split in two, and then again on both sides of the average score, the final points are the same depth, the time complexity is O (nlogn), otherwise lead to imbalance and depth on both sides of the reference value, is a number of oblique extreme cases, in this case time complexity is O (n ^ 2 ).

Quick Sort optimization

Since Mentioned above, select the reference value of efficiency and quick sort of absolute relationship, then optimize Nature from the start how to choose the right reference value.

Method a: The value of the preference (or tail) portion as a reference value. Such drawbacks Obviously, no one can say what the reference value in the end clear select Yes.

Method two: selecting a random value. It is to select a random value as a reference value in all elements. This method is a slightly compared to the first safe side, slightly reduces the probability to get drunk or minimum value, but also a "blind Mongolia" exists.

Method three: taking a median of three numbers, i.e. to select all elements of the three values, the three values ​​are sorted, select the intermediate value as a reference value. The benefit of this is to ensure that the selected reference value is not the largest or smallest value.

Method four: poly (greater than three) the number of taken. Ibid.

Quick sort of practical application

A lot of people in the actual work will be used Arrays.sort () method to sort, in fact, there have actually used quick sort.

Sequence

As shown above, when the number of elements to be sorted is greater than QUICKSORT_THRESHOLD (286) value, using a biaxial quick sorting method will be optimized. Interested students can study on their own jdk source.

For less than QUICKSORT_THRESHOLD (286) when the value of what sort of a way of doing things? The answer is direct insertion sort.

Which of course includes various sorting methods appropriate to consider the scene, but this horse is God boundary value 286 it? Reference Stack Overflow answer, and that is the former conducted a number of tests and found that this value is the cut-off point performance of the algorithm.

Quick sort JAVA implementation code

public class Main {
    public static void main(String[] args) {
        int[] arr = {1, 432, 2, 31, 5, 234, 12};
        quickSort(arr, 0, arr.length - 1);
        for (int a : arr) {
            System.out.println(a);
        }
    }

    /**
     * 快速排序思路:
     * 在一组元素中,选取一个基准值,把大于基准值的元素放到右侧,小于基准值的元素放到左侧,重复操作直到完成排序。
     * <p>
     * 使用方法:
     * 1、找基准值,并设置首、尾标识(low、high)
     * 2、从后半部分开始,当扫描值大于基准值时就让high-1,如果比基准值小,则把high位置的值赋值给low
     * 3、然后从前向后臊面,如果扫描至小于low则low+1,如果大于基准值则将low位置的值付给high位置
     * 4、递归操作
     *
     * @param arr
     * @param low
     * @param high
     */
    private static void quickSort(int[] arr, int low, int high) {
        // 指定递归退出条件
        if (low < high) {
            // 获取排序后基准值的位置
            int pivot = partition(arr, low, high);
            // 对小于基准值的元素再次进行排序
            quickSort(arr, low, pivot - 1);
            // 对大于基准值的元素再次进行排序
            quickSort(arr, pivot + 1, high);
        }
    }

    /**
     * 返回排序后基准值的位置
     *
     * @param arr
     * @param low
     * @param high
     * @return
     */
    private static int partition(int[] arr, int low, int high) {
        int pivotKey = arr[low];
        while (low < high) {
            // 当尾部元素大于基准值时high-1
            while (low < high & arr[high] >= pivotKey) {
                high--;
            }
            // 否则将尾部元素赋值给首部位置
            arr[low] = arr[high];
            // 当首部元素小于基准值时low+1
            while (low < high && arr[low] <= pivotKey) {
                low++;
            }
            // 否则将首部元素赋值给尾部位置
            arr[high] = arr[low];
        }
        // 将基准值插入到首部和尾部中间位置
        arr[low] = pivotKey;
        // 返回基准值的位置
        return low;
    }
}

With source code: https://github.com/bruceq/leetcode.git

Published 60 original articles · won praise 57 · views 80000 +

Guess you like

Origin blog.csdn.net/qixinbruce/article/details/103850092