Sort the quick sort (top)

Source: Internet
Author: User

Objective

This blog is based on the blog of Wu Fan, and its blog address.

The hill sort is equivalent to the optimization of the direct insert sort, they belong to the Insert sort class, the heap sort is equivalent to the simple selection sort optimization, they belong to the selection sort class. The quick sort is actually an upgrade of the bubbling sort, and they all belong to the Exchange sort class. That is, it is also through the continuous comparison and mobile exchange to achieve the sorting, but its implementation, increase the record comparison and moving distance, the key is a large record from the front side directly to the back, the key is smaller records from the back to the front, thus reducing the total number of comparisons and the number of mobile exchanges, Instead of comparing and swapping around 22 like bubble sort.

Plainly speaking, in fact, is in the bubble sort based on the memory of the function, similar to the heap sort in a simple selection of sorting , bubble sort 22 or so after comparison is not remember the size of the relationship between the next round when comparing the two compared to the same as their first comparison, do not know each other; and the quick sort, he When a pivot value is selected so that the element on both sides of the pivot value has a size relationship, the left side of the pivot value does not need to be compared to the right of the pivot value .

Basic ideas

  By sorting the pending records into two separate parts, one of which is smaller than the keywords in the other part of the record, the two parts of the record can be sorted separately to achieve the order of the whole sequence. Select a keyword, By sequencing (comparing and moving) the left side of the keyword is smaller (or smaller) than he is, and then the left and right elements are sorted separately, until the entire sequence is ordered .

is probably not very well understood, so let's look at the code, assuming that the initial sequence is {5,3,7,9,1,6,4,8,2 } .

Code implementation

/*** Quick Sort *@paramarr*/    Private Static voidQuickSort (int[] arr) {QSort (arr,0,arr.length-1); }    /*** Sequence arr[low for sequences in arr: High] for quick sorting *@paramarr *@paramLow *@param High*/    Private Static voidQSort (int[] arr,intLowintHigh ) {        intPivotKey; if(Low <High ) {            //find the location of the pivot value, at which point Arr[low,pivotkey-1] is less than (greater than) Arr[pivotkey],arr[pivotkey+1...high] is greater than (less than) Arr[pivotkey]PivotKey =partition (Arr,low,high); QSort (Arr,low,pivotkey-1);//quick sorting of arr[low...pivotkey-1]QSort (Arr,pivotkey+1,high);//quick sorting of Arr[pivotkey+1...high]        }    }

The core of this piece of code is "PivotKey =partition (Arr,low,high); "Before executing it, the array value of ARR is{5,3,7,9,1,6,4,8,2}。partition function to do is to first select one of the key words, such as selecting the first keyword 5,and then try to put it in a position so that its left value is smaller than it, and the value on the right is larger than it, and we call that keyword pivot
After the execution of partition (arr,0,9), the array becomes {2,3,4,1,5,6,9,8,7} and returns the value of the pivot value of subscript 4 to PivotKey, and the number 4 indicates that element 5 is placed in the position labeled 4 in the array. At this point, the program turns the original array into two decimal groups {2,3,4,1} and {6,9,8,7} that are located on the left and right of the element, and then quickly sorts the sub-sequences separately.

Here, it should be said that it is not difficult to understand. the function of partition is equivalent to giving the program memory function to improve the speed. Let's take a look at how fast sorting is the most critical partition method implementation.

1     /**2 * Find the subscript for the pivot value and return3 * Swap the elements in Arr[low...high] to move the pivot value to the correct position4      * @paramarr5      * @param Low6      * @param High7      * @return8      */9     Private Static intPartitionint[] arr,intLowintHigh ) {Ten         intPivotvalue = Arr[low];//Select Arr[low] as the pivot value One         //scan from both ends to the center to move the pivot value to the correct position A          while(Low <High ) { -              while(Lowpivotvalue) { -High--; the             } -Swap (Arr,low,high);//swap records that are smaller than the pivot value to the low end -              while(Lowpivotvalue) { -Low + +; +             } -Swap (Arr,low,high);//switching records larger than pivot values to high-end +         } A         returnLow ; at}

Performing process Simulations

The whole fast sort key is the partition method, so let's simulate the execution of this method.

1) Initial time, low=0,high=8,pivotvalue=arr[low]=5, such as

 

2) Execute 13~15 line, arr[high]>=pivotvalue not satisfied, jump out of the loop, execute 16 lines, Exchange 5 and 2, at this time, sequence as

Execute 17~19 line, loop conditions meet, low++, Low=1, continue to perform the exchange, conditions are still satisfied, low++,low=2, continue to execute the loop, when the loop condition is not satisfied, jump out of the loop, execute 20 lines, Exchange arr[low=2] and arr[high=8] i.e. 7 and 5, the process

3) The inner loop is finished, the outer loop continues, the 13~15 line is returned, the Low

The condition satisfies, executes the loop body, high--,high=7, continues to execute the loop, satisfies the condition, executes the loop body, high--, then high=6, resumes the loop, the condition is not satisfied, exchanges arr[low=2] and arr[high=6], namely exchanges 5 and 4, the execution process is as follows

Next execute 17~19 line, loop condition satisfies, low++, then low=3, continue execution loop, condition not satisfied, jump out of loop, execute 20 lines, Exchange arr[low=3] and arr[high=6], namely Exchange 9 and 5, execute the procedure as follows

4) The internal loop ends, the external loop continues, the condition is met, and the internal loop continues

Execute 13~15 Line, condition satisfies, high--, then high=5, continue execution loop, condition satisfies, high--, at this time high=4, condition not satisfied, jump out loop, execute 16 lines, Exchange arr[low=3] and arr[high=4], namely Exchange 5 and 1, The execution process is as follows

Next executes the 18~19 line, the condition satisfies, low++, then low=4, continues the execution loop, the condition is not satisfied, jumps out the loop, exchanges arr[low=4] and arr[high=4], namely exchanges 5 and 5, the process diagram does not provide, relatively simple easy to understand

5) The internal loop, continue to execute the external loop, the condition is not satisfied, out of the outer loop, return low=4, since this partition method execution, then our keyword 5 has found the correct position is 4, and the position of the index 4 is returned, the sequence is { 2,3,4,1,5,6,9,8,7}

The subsequence {2,3,4,1} and {6,9,8,7} are sorted in a quick sequence, as in the above, and no demonstration is done here, and you crossing the master to simulate for yourself!

Sort the quick sort (top)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.