Sorting algorithm---Quick sort (java version)

Quick sort

principle

Quick Sort (Quick Sort) algorithm, referred to as quick sort, uses the idea of ​​divide and conquer. The idea of ​​quick sort is: if we want to sort the sequence between m->n, we choose any between m->n An element data is used as the partition point (Pivot), and then we traverse all the elements between m->n, put the elements smaller than pivot to the left, the elements larger than pivot to the right, and pivot to the middle, so that the entire sequence is Divided into three parts, the elements between m->k-1 are smaller than pivot, the middle is pivot, and the elements between k+1->n are larger than pivot. Then according to the idea of ​​divide-and-conquer recursion, the element sequence of the interval on both sides is processed until the interval is reduced to 1, which means that the entire sequence is already in order.

The algorithm is described as follows:

  • Pick an element from the sequence and call it a "pivot";
  • Re-order the sequence, all elements smaller than the reference value are placed in front of the reference, and all elements larger than the reference value are behind the reference (the same number can go to either side). After the partition exits, the benchmark is in the middle of the sequence. This is called a partition operation;
  • Recursively sort the sub-sequences of elements smaller than the reference value and the sub-sequences of elements larger than the reference value until the interval is reduced to 1.

Code

public class QuickSort {
    
    
    public static void main(String[] args) {
    
    
        int[] arr = {
    
    5, 2, 6, 9, 0, 3, 4};
        quickSort(arr,0,arr.length-1);
        System.out.println(Arrays.toString(arr));
    }
    public static void quickSort(int[] arr, int begin, int end) {
    
    
        if (arr.length <= 1 || begin >= end) {
    
    
            return;
        }
        //进行分区得到分区下标
        int pivotIndex = partition(arr, begin, end);
        //分区左侧进行快排
        quickSort(arr, begin, pivotIndex - 1);
        //分区右侧进行快排
        quickSort(arr,pivotIndex+1,end);
    }
    public static int partition(int[] arr, int begin, int end) {
    
    
        //默认使用 最后一个元素作为基准点
        int pivot = arr[end];
        //定义分区后的pivot元素下标
        int pivotIndex = begin;
        for (int i = begin; i < end; i++) {
    
    
            判断如果该区间内如果有元素小于 pivot 则将该元素从区间头开始一直向后填充 有点类似选择排序
            if (arr[i] < pivot) {
    
    
                if (i > pivotIndex) {
    
    
                    swap(arr,i,pivotIndex);
                }
                pivotIndex++;
            }
        }
        swap(arr,pivotIndex,end);
        //得到基准点
        return pivotIndex;
    }

    //交换元素
    public static void swap(int[] arr, int i, int j) {
    
    
        int temp = arr[j];
        arr[j] = arr[i];
        arr[i] = temp;
    }
}

1: What is the time complexity of quick sort?

The time complexity of fast sorting is the best and the average complexity is O(nlogn), only in extreme cases it will become O(n^2).

2: What is the space complexity of quick sort?

Through the code implementation of fast scheduling, we found that fast scheduling does not require additional storage space, and all operations can be completed in a predetermined space. Therefore, the space complexity of fast scheduling is O(1), which means that fast scheduling is one An in-place sorting algorithm.

3: Is quicksort a stable sorting algorithm?

Because the partitioning process involves exchange operations, if there are two identical elements in the array, such as the sequence 6, 3, 5, 9, 4, after the first partition operation, the two 6s are relative The order will change. Therefore, quicksort is not a stable sorting algorithm.

4: The similarities and differences between fast sorting and merging

First of all, fast sorting and merging both use the idea of ​​divide and conquer recursion. The corresponding partition operation in fast sorting. The recursion formula and recursive code are also very similar, but the process of merge sorting is from bottom to top, from part to whole. , Deal with the sub-problems first, and then merge. The quick sort is the opposite. Its processing process is from top to bottom, from the whole to the part, first partitioning, and then dealing with the sub-problems. Although merge sort is a stable sorting algorithm with O(nlogn) time complexity, it is an out-place sorting algorithm. The main reason is that the merge function cannot be executed in place (in the array). Quick sorting can realize in-situ sorting by designing clever in-place (in-array) partition functions, which solves the problem of merging and sorting occupying too much memory.

Guess you like

Origin blog.csdn.net/qq_33626996/article/details/113177707