Vernacular resolved quickly sort

Fast sorting algorithms referred fast row, it is to choose an arbitrary element in the array as a threshold , and then through the array of elements to less than a threshold into the left, greater than a threshold is put on the right, thus completing a sort, and finally sequentially around the threshold element portion can be sorted by recursively.

Now think about how we achieve at such a sort, without considering the complexity of the space, a way we are most likely to think of two new temporary array are stored in two parts about the sort of data, such as the critical value of 4, we of less than 4 into an array, the array is greater than 4 into another, and then copy the data 2 to the original temporary array to array, so that when the cycle is completed, all the elements in the array becomes the orderly. But this sort of maximum space complexity O (n).

Today we are speaking another space complexity is O (1) operation, did not talk much, we directly on the code:

 public static void quickSort(int arr[],int left,int right){
        if(left>=right)return;

        int q=partition(arr,left,right);

        quickSort(arr,left,q-1);
        quickSort(arr,q+1,right);

    }

    private static int partition(int[] arr, int left, int right) {
        int sortIndex=left;
        int arrIndex=left;
        int pivot=arr[right];

        while (arrIndex<right){
            if(arr[arrIndex]<pivot){
                int tmp=arr[arrIndex];
                arr[arrIndex]=arr[sortIndex];
                arr[sortIndex]=tmp;
                sortIndex++;
            }
            arrIndex++;
        }
        arr[right]=arr[sortIndex];
        arr[sortIndex]=pivot;
        return sortIndex;
    }

Wherein the array arr, left and right to be sorted are arr the left and right boundaries (when the array length is 6, the left and right respectively 0 and 5), when the left than or equal to right when , termination condition is reached directly returned .

Now we look at the array  6, 2, 3, 5, 1, 4  fast discharge process performed, wherein the partition partition method is performed:

                                                  6 2 3 5 1 4 left = 0 right = 5
                    first partition 2 3 1 4 6 5 left = 0 right = 5
                    first recursive 2 3 1 left = 0 right = 2
                    the second partition 132 left = 0 right = 2
                    second recursive left = 0 right = 0 left> = right, return termination condition is reached
                    the third recursive 3 2 left = 1 right = 2
                    third partition 2 3 left = 1 right = 2
                    fourth recursive left = 1 right = 0 left> = right, return termination condition is reached
                    Fifth recursive left = 2 right = 2 left> = right, return termination condition is reached
                    sixth recursive 6 5 left = 4 right = 5
                    fourth partition 5 6 left = 4 right = 5 left> = right, to achieve termination condition returns
                    seventh recursive left = 4 right = 3 left> = right, return termination condition is reached
                    eighth recursive left = 5 right = 5 left> = right, return termination condition is reached

          the above is a detailed process of performing a fast sort , is the last element of the array as a threshold value for the quick sort, it can be seen when the last partition becomes a completed array 1, 2, 3, 4, 5, 6 ordered a.

Now our first partition above the array, for example, analyze the execution of partition method:

                                
                                
                                          sortIndex pivot
            the first cycle 6,235,146 and comparison 4, 6 is greater than 4, no exchange of data
                                          arrIndex                             
                                                    
                                
                                          sortIndex pivot
            the second cycle                    6 2         . 3 and 4. 5. 1 2 Comparative 4, 2 less than 4, 2, and 6 data exchange 
                                                       arrIndex
                                                    
                                
                                                      sortIndex Pivot
            third cycle 2        3 6         . 5. 1 and 4 4 Comparative 3, 3 is less than 4, 3, and 6 a data exchange
                                                                 arrIndex         

                                                                                        
                                
                                                                Pivot sortIndex
            2. 3. 1. 6 5 5 4 4 Comparative fourth cycle and, 5 more than 4, no exchange of data
                                                                            arrIndex

                                                                                        
                                                                sortIndex pivot
            fifth. 3 cycle 2         6         . 5         1         4 1 4 and comparison, less than 1 4, 1 6 and data exchange
                                                                                      arrIndex         
                                                     
                                                                                        
                                                                         sortIndex pivot
            sixth cycle 2 3 1 5 6 4 arrIndex = 5, right = 5, arrIndex <right It does not hold cycle ends
                                                                                                arrIndex         
                                                     
                                                                                        

                                                                        sortIndex                  
            last. 3. 1 2         . 4         . 6        5                      4 and 5 data exchange
                                                                             pivot     
                   
                                                     

The above is the 4 critical point of  become 2, 3, 1, 4, 5, 66, 2, 3, 5, 1, 4 detailed process performed, marked red fast discharge occurs during the data exchange.

Wherein n 1-arrIndex front element and array subscripting, respectively iterate critical point comparison 4, if less than the critical point, the target will sortIndex exchange element and the current element, and sortIndex + 1'd, or is not data exchange. As can be seen from the above implementation process is used to store sortIndex subscript index less than a threshold 4, whenever there is less than 4, where the elements sortIndex occurs a data exchange element, and plus one, so that when only complete traversal sortIndex elements need to be located and the critical value of 4 exchange, will be able to reach the core idea in the left half of the critical point 4 is less than 4, the right half of the effect is greater than 4, which is fast row

to sum up:

Read my previous merge sort can be seen, quick sort and merge sort code is very similar, but a merge sort sequence is to be split into individual elements and then combined into a sort of orderly sequence, and quick sort the entire sequence is split when a single element has the sorted. Also in the above partition method can be seen, it does not apply for additional memory space, by comparing the original exchange sorted array, the spatial complexity is O (1). Merge sort need to apply additional memory space to sort, the spatial complexity is O (n). Because their time complexity is O (log n), and the fast row in memory utilization higher than merge sort, merge sort parallelism so fast application will be more extensive.

Published 25 original articles · won praise 51 · views 20000 +

Guess you like

Origin blog.csdn.net/Royal_lr/article/details/103119925