Detailed quick sort--java version

In all sorting algorithms, quick sorting is of course very important, after all, the speed is as the name suggests-fast.
It was difficult to understand his thoughts when I first came into contact with quicksort. After careful consideration, I finally knew what was going on. Here are some basic content about quicksort.

The idea of ​​quick sort

To understand an algorithm, the idea of ​​the algorithm must first be known.
Assuming that an integer array is given, the array should be sorted in ascending order. Now use the idea of ​​quick sort to solve this problem: It is
divided into the following steps:

1. Get a key value from the array.
2. Values ​​greater than the key are placed after the key.
3. Values ​​less than the key are placed in front of the key.

After the previous step 123, the front of the key value is smaller than the key, and the back of the key value is larger than the key. So there is no need to change the position of the key, and there is no need to sort the keys again. Note that the subscript of the first element of the array is lowindex, the subscript of the last element of the array is highindex, and the subscript of the key value is keyIndex. Then the subscripts of the array elements that need to be sorted are [lowindex,keyIndex-1] and [keyIndex+1,highIndex] (the position of the key does not need to be moved)

Then do the above 123 operation on the elements of these two intervals, and finally these two intervals will also get two key values ​​that have been determined. Continue to divide the interval and repeat the 123 operation until there is only 1 in the divided interval. Or 0 elements, which means that this cell is already in order. Then adjust until all the cells are in order. Then the entire array is ordered.

From the basic idea above, we can conceive how to write the code? From the basic description, is it very similar to the idea of ​​recursion, then if recursion is used, which key value is the object that is operated in the recursion to keep changing? If you think about it, there is only keyIndex. In this way, the interval to be operated on each recursion is different.
So we can write a recursive scheme as follows:

public static void quickSort(int[] arr){
    
    
        sortInternal(arr,0,arr.length-1);
    }
    public static void sortInternal(int[] arr,int lowIndex,int highIndex){
    
    
        int size=highIndex-lowIndex+1;
        if(size<=1){
    
    
            return;
        }
        int keyIndex=partition(arr,lowIndex,highIndex);
        sortInternal(arr,lowIndex ,keyIndex-1);
        sortInternal(arr,keyIndex+1,highIndex);
    }

The framework already exists, so how to determine the location of this key value.

Method of determining the subscript of the key

There are many ways to determine the location of the specific subscript of the key. Here we first describe the most basic hover method. There will be opportunities to add other methods later.
Since I don't know which value to choose as the key, I simply choose the first element of the array as the key value for comparison (other values ​​are also ok, here is just to describe the idea and choose the first one).
This becomes an array, with elements less than or equal to the key value placed in the front, and elements greater than the key value placed in the back-----assuming this process is called comparing keys. In the end, a boundary is formed. All the fronts are less than or equal to the key, and all the backs are greater than or equal to the key, and then the position of the initially selected key is exchanged with the last element in the interval less than or equal to ---- assuming this process is called swap key. This ensures that the value before the key is less than or equal to him, and the value after the key is greater than him.
The following figure describes the process
Insert picture description here
. The figure omits the results of each cycle, and only draws the state at the end of the cycle. You must manually walk through each level of the cycle manually. This will make the understanding deeper.
The "key exchange phase operation" has been completed in the above figure. So the position of this key is already fixed, the front ones are smaller than him, and the back ones are bigger than him. So only use his subscript. Then continue to repeat the operation for the two intervals adjacent to him but not including him.
The following is the partition code, you can refer to:

public static int partition(int[] arr, int lowIndex, int highIndex){
    
    
        int headIndex=lowIndex;
        int tailIndex=highIndex;
        int key=arr[lowIndex];
        while(headIndex<tailIndex){
    
    
            if(arr[tailIndex]>key){
    
    
                tailIndex--;
            }else if(arr[tailIndex]<=key){
    
    
                if(arr[headIndex]>key){
    
    
                    swap(arr,headIndex,tailIndex);
                    headIndex++;
                    tailIndex--;
                }
                else if(arr[headIndex]<=key){
    
    
                    headIndex++;
                }
            }
        }
        swap(arr,lowIndex,tailIndex);
        return tailIndex;
    }

Related properties and characteristics of fast queue

1. Quick sorting is a quicker sorting method, because it has fewer comparisons and a larger exchange of elements. His time complexity is O(nlog(n)).
And quick sort is more suitable for large amounts of data. It is not suitable to use it when the amount of data is relatively small. So when the number is less than 16, it is more efficient to use insertion sort. The comparison of numbers less than 16 in the sort method in java uses insertion sort.
2. About the partition () method. If you think of an array as a tree. Then the partition method is to take out the root of a binary search tree (the first time it is not standardized, and then it gradually becomes standardized). So his average time complexity is O(logn). If it is a reversed array, that is O(n) time complexity.
3. The partition() method also has other methods, which will be added next time.

Finally, if you feel that you understand, welcome one-click triple connection, and welcome pointers.

Guess you like

Origin blog.csdn.net/weixin_43815275/article/details/109398316