Algorithm notes-------Quick sort

Quick sort

Introduction

Quicksort (Quicksort) is an improvement to bubble sort

The basic idea is : divide the data to be sorted into two independent parts by sorting , and all the data in one part is smaller than all the data in the other part , and then quickly sort the two parts of the data according to this method. The entire sorting process can be carried out recursively , so that the entire data becomes an ordered sequence

Take the teacher’s example: -9,78,0,23, -567,70, these data are sorted from small to large

What does fast sorting mean, we take a value (which can be replaced) 0, we put the value smaller than 0 to the left, and the larger than 0 to the right . Note: This place is smaller than 0, which means that the left is not necessarily in order. , The right side is not necessarily in order! ! !

Then this process will be recursive, and the same operation will be performed on the left, and the same will be done on the right

Diagram

img

This dynamic graph is based on the first number, so be careful not to make a mistake

Thinking analysis

Still this array **-9,78,0,23, -567,70**, we follow the 0-bit benchmark as the teacher said

The subscript of 9 is 0, and the subscript of 70 is 5, so 5/2=2 The number with subscript 2 is 0, we take 0 as the reference value

We also need two auxiliary indexes (not necessarily two). One used to traverse the data to the left of 0 is to find a number greater than 0

The other is to find a number smaller than 0 on the right. What is the result?

The first time : the index on the left finds the position of 78, and the position on the right is -567. Then we swap it and reread the process until the left is smaller than 0, and the right is larger than 0. Of -9, -567, 0, 23, 78, 70

Then perform the same operation again on both sides of the set value, which is the first step of recursion. Then you will get the final sort result -9, -567, 0, 23, 70, 78

The derivation process

/**
 * @author 王庆华
 * @version 1.0
 * @date 2020/12/30 19:48
 * @Description TODO
 * @pojectname 快速排序算法
 */
public class QuickSort {
    
    
    public static void main(String[] args) {
    
    
        int[] arr = {
    
    -9,78,0,23,-567,70};
        quickSort(arr,0,arr.length-1);
        System.out.println(Arrays.toString(arr));
    }

    public static void quickSort(int[] arr,int left,int right){
    
    
        int l = left;//左下标
        int r = right;//右下标
        int temp = 0;//临时变量,交换时使用
        //pivot 中轴
        int pivot = arr[(left+right)/2];
        //while循环的目的是让比pivot值小的放到基准值的左边,比他大的放右边
        while (l<r){
    
    
            //在pivot左边一直找,找到大于等于pivot的值才退出
            while (arr[l] < pivot){
    
    
                l+=1;
            }
            //在pivot右边一直找,找到小于等于pivot的值才退出
            while (arr[r] > pivot){
    
    
                r-=1;
            }
            //左边已经全部符合都小于基准值了
            if (l>=r){
    
    
                break;
            }
            //交换
            temp = arr[l];
            arr[l] = arr[r];
            arr[r] = temp;
            //如果交换后,发现这个arr[l] == pivot 值相等,r--:前移一步
            if (arr[l] == pivot){
    
    
                r-=1;
            }
            //如果交换后,发现这个arr[r] == pivot 值相等,l++:后移一步
            if (arr[r] == pivot){
    
    
                l+=1;
            }
        }
    }
}
[-9, -567, 0, 23, 78, 70]

Final code

/**
 * @author 王庆华
 * @version 1.0
 * @date 2020/12/30 19:48
 * @Description TODO
 * @pojectname 快速排序算法
 */
public class QuickSort {
    
    
    public static void main(String[] args) {
    
    
        int[] arr = {
    
    -9,78,0,23,-567,70};
        quickSort(arr,0,arr.length-1);
        System.out.println(Arrays.toString(arr));
    }

    public static void quickSort(int[] arr,int left,int right){
    
    
        int l = left;//左下标
        int r = right;//右下标
        int temp = 0;//临时变量,交换时使用
        //pivot 中轴
        int pivot = arr[(left+right)/2];
        //while循环的目的是让比pivot值小的放到基准值的左边,比他大的放右边
        while (l<r){
    
    
            //在pivot左边一直找,找到大于等于pivot的值才退出
            while (arr[l] < pivot){
    
    
                l+=1;
            }
            //在pivot右边一直找,找到小于等于pivot的值才退出
            while (arr[r] > pivot){
    
    
                r-=1;
            }
            //左边已经全部符合都小于基准值了,右边符合大于基准值
            if (l>=r){
    
    
                break;
            }
            //交换
            temp = arr[l];
            arr[l] = arr[r];
            arr[r] = temp;
            //如果交换后,发现这个arr[l] == pivot 值相等,r--:前移一步
            if (arr[l] == pivot){
    
    
                r-=1;
            }
            //如果交换后,发现这个arr[r] == pivot 值相等,l++:后移一步
            if (arr[r] == pivot){
    
    
                l+=1;
            }
        }
        //如果l == r 必须l++,r--,需要错开,不错开的话会栈溢出的
        if (l == r){
    
    
            l += 1;
            r -= 1;
        }
        //向左递归
        if (left < r){
    
    
            quickSort(arr,left,r);
        }
        if (right>l){
    
    
            quickSort(arr,l,right);
        }
    }
}

It’s just that a recursive operation is added at the end of the derivation process, which is quite abstract and there are many steps. We can make a breakpoint when sorting to understand

Guess you like

Origin blog.csdn.net/qq_22155255/article/details/112620186