Quick sorting algorithm

Posted by integravtec on Tue, 04 Jan 2022 16:16:38 +0100

The core idea of quick sort algorithm is to put the elements smaller than the benchmark to the left of the benchmark and the elements larger than the benchmark to the right of the benchmark. After a round of sorting, divide the data to be sorted into two independent parts. All the data in one part is smaller than all the data in the other part, and then quickly sort the two parts of data according to this method. The whole sorting process can be recursive to make all the data orderly

(1) First, set a boundary value, and divide the array into left and right parts through the boundary value. Generally, the first element is selected first.

int low=left;
int high=right;
int mid=arr[low];

(2) Find the subscript position of the number smaller than the reference value from the subscript on the right. If the subscript on the right is larger than the reference value, do not exchange the right tracking variable and decrease to the left

while(low<high&&arr[high]>=mid) high--;

If no value smaller than the reference value is found, then high==low, and the exchange will not be affected
 Modify the value of the low subscript position to find a number smaller than the reference value, that is, arr[low]=arr[right]
arr[low]=arr[right];

(3) Look for the subscript position of the number larger than the reference value from the left subscript position. If the low order ones on the left are smaller than the reference value, do not exchange the left tracking variable and increase to the right to continue the search

 while(low<high&&arr[low]<=mid) low++;

If no value larger than the reference value is found on the right side of the low subscript, then high==low does not affect the interaction
 Modify the high subscript position value to find a number larger than the reference value, that is, arr[right]=arr[low]
arr[right]=arr[low];

(4) After running here, low will reach the middle position, because low > = right includes those that have been ordered and assigned the middle position as the reference value. At this time, the left side of low is less than the reference value and the right side is greater than the reference value, and then continue the next cycle,

arr[low]=mid;
quickSort(arr,left,low-1);
quickSort(arr,low,right-1);

Reference codes are as follows:

public class MyClass {
    public static void main(String[] args){
        int[] arr=new int[]{6,35,13,4};
        
        quickSort(arr3,0,arr3.length-1);
        
        for(int j:arr3){
            System.out.print(j+",");
        }
    }
    
    public static void quickSort(int[] arr,int left,int right){
        if(left>right)return;
        int low=left;
        int high=right;
        int mid=arr[low];
        while(low<high){
            //Find the subscript position of the number smaller than the reference value from the high subscript on the right. If the high subscript on the right is larger than the reference value, do not exchange high--
            while(low<high&&arr[high]>=mid) high--;
            //If no value smaller than the reference value is found, then high==low, and the exchange will not be affected
            //Modify the value of the low subscript position to find a number smaller than the reference value, that is, arr[low]=arr[right]
            arr[low]=arr[right];
            //Find the subscript position of a number larger than the reference value from the left subscript position. If the lower subscript positions on the left are smaller than the reference value, do not exchange low++
            while(low<high&&arr[low]<=mid) low++;
            //If no value larger than the reference value is found on the right side of the low subscript, then high==low does not affect the interaction
            //Modify the high subscript position value to find a number larger than the reference value, that is, arr[right]=arr[low]
            arr[right]=arr[low];

        }
        /**
         * After running here, low will reach the middle position, because low > = right includes those that have been ordered
         * And assign the middle position as the reference value. At this time, the left side of low is less than the reference value, and the right side is greater than the reference value
         * Then continue the next cycle
         */
        arr[low]=mid;
        if (left-1>left) quickSort(arr,left,low-1);
        //if (low<right-1) quickSort(arr,low,right-1);
        if (low+1<right)  quickSort(arr,low+1,right);
    }
    
}

Complexity analysis:

Fast scheduling average complexity O(nlogn): you can refer to the blog:

https://blog.csdn.net/qq_20746945/article/details/89378662?utm_medium=distribute.pc_relevant_t0.none-task-blog-2%7Edefault%7EBlogCommendFromMachineLearnPai2%7Edefault-1.control&depth_1-utm_source=distribute.pc_relevant_t0.none-task-blog-2%7Edefault%7EBlogCommendFromMachineLearnPai2%7Edefault-1.control

For relevant pictures and texts, please refer to the blog:

https://zhangxy.blog.csdn.net/article/details/102880632?utm_medium=distribute.pc_relevant.none-task-blog-2%7Edefault%7EBlogCommendFromMachineLearnPai2%7Edefault-1.control&depth_1-utm_source=distribute.pc_relevant.none-task-blog-2%7Edefault%7EBlogCommendFromMachineLearnPai2%7Edefault-1.control

Topics: Algorithm