Sorting Algorithm Diagram (5): Step-by-step Analysis of Quick Sort


1 Introduction to Quick Sort

Quick sort is an improvement on bubble sort. The basic idea is: divide the data to be sorted into two independent parts by one-pass sorting, all the data in one part is smaller than all the data in the other part, and then quickly sort the two parts according to this method, the whole process can be Recursively, so that the entire data becomes an ordered sequence.


2 Introduction and Diagram of Thought

The quick sorting algorithm implements sorting through multiple comparisons and exchanges, and its sorting process is as follows:
(1) First, set a cutoff value, and divide the array into left and right parts through the cutoff value.
(2) Collect the data greater than or equal to the cutoff value to the right of the array, and collect the data smaller than the cutoff value to the left of the array. At this time, each element in the left part is less than the cutoff value, and each element in the right part is greater than or equal to the cutoff value.
(3) Then, the left and right data can be sorted independently. For the array data on the left side, a boundary value can be taken to divide this part of the data into left and right parts, and the smaller value is placed on the left side, and the larger value is placed on the right side. The array data on the right can also be processed similarly.
(4) Repeat the above process, it can be seen that this is a recursive definition. After sorting the left part by recursion, recursively sort the order of the right part. When the sorting of the data in the left and right parts is completed, the sorting of the entire array is also completed.

Just looking at the brief introduction of ideas is actually not very easy to understand, the following is an example to illustrate

Under normal circumstances, we will use a number in the array as the reference number (for convenience, the leftmost number in the array will be used as the reference number), and then retrieve from both sides. Follow the steps below:

  • First retrieve from the right smaller than the reference number
  • Then retrieve from the left that is larger than the reference number
  • Once retrieved, stop and swap the two retrieved elements
  • Repeat the above steps until the search meets, then replace the reference number, and update the interval, recursively
  • The final sequence will become ordered

Idea diagram:

This picture comes from the Internet. For convenience, take the sequence: {6,1,8,0,0,9,5,3,7}as an example
insert image description here
to analyze the first sorting in detail:Steps with 6 as the base number

  1. The red block marks the reference number, and the initial positions of left and right are as shown in the figure:
    insert image description here

  2. right keeps moving to the left, looking for a number smaller than the reference number, as shown in the figure, found 3
    insert image description here

  3. At this time, left starts to move, and keeps moving to the right, looking for a number larger than the reference number, and finds 8. At this time, left and right both find the corresponding numbers and exchange them:
    insert image description here

  4. Right continues to search for a number smaller than the reference number 6 to the left. After finding it, left continues to search for a number larger than the reference number to the right. When both left and right find the corresponding number, they exchange again.
    insert image description here

  5. Repeat the above steps, right continues to go left, but at this time, left and right meet and point to the position of 5, then exchange the reference number with the number at this position, so that it can be observed that the left side of 6 is higher than 6 The small ones are all bigger than 6 on the right.
    insert image description here

  6. This process needs to be done recursively until the sequence is in order. That is, with 5 as the base number, recurse the interval on the left of 6, then recurse the interval on the right, and repeat until left > right to exit.

3 Implementation code and running results

import java.util.Arrays;

/**
 * @author 兴趣使然黄小黄
 * @version 1.0
 * 快速排序
 */
public class QuickSort {
    
    

    public static void main(String[] args) {
    
    
        int[] arr = {
    
    6,1,8,0,0,9,5,3,7};
        quickSort(arr, 0, arr.length-1);
        System.out.println("排序后: " + Arrays.toString(arr));
    }

    //快速排序
    public static void quickSort(int[] arr, int left, int right) {
    
    
        //边界条件
        if (left > right){
    
    
            return;
        }

        //定义基准数和左右指针
        int l = left;
        int r = right;
        int base = arr[left];

        //循环,将比基准数小的放在左边,比基准数大的放在右边
        while (l != r){
    
    
            //先从右边找比基准数小的,停下
            while (arr[r] >= base && l < r){
    
    
                r--;
            }
            //从左边找比基准数大的,停下
            while (arr[l] <= base && l < r){
    
    
                l++;
            }
            //此时已经找到对应的l 和 r,进行交换
            int temp = arr[l];
            arr[l] = arr[r];
            arr[r] = temp;
        }
        //至此,基准数两边都按照需要排好了,只需要将基准数与lr相遇的位置进行交换
        arr[left] = arr[l];
        arr[l] = base;
        //打印中间结果
        System.out.println(Arrays.toString(arr));
        //先向左找
        quickSort(arr, left, r-1);
        //向右递归
        quickSort(arr, l+1, right);
    }
}

Realize the result:
insert image description here


write at the end

This article is included in the Java data structure点击订阅专栏 and is being continuously updated.
 Creation is not easy, if you have any questions, welcome to private message, thank you for your support!

insert image description here

Guess you like

Origin blog.csdn.net/m0_60353039/article/details/127717659