Easy Learning Quick Sort (ii)--Quick Sort optimization

Source: Internet
Author: User

Easy to learn quick sort in the previous article (i)--the algorithm for fast sorting is introduced in the basic quick sort, and a question is raised at the end: for example, an array of this kind:1 x 6 9, which generally selects the first element of the array (that is, 1) as the Datum point, It is obvious that the first element of the array is 1 as the Datum point, 1 is the smallest number in the array, then the order does not achieve the desired effect. How to choose this datum point? In general, the chosen number is preferably in the middle of all the numbers in the sorting interval (that is, the median), so that the sorting effect is best. Next, there are two ways to optimize the quick sort: three-digit method and small interval optimization.

Optimizing the 1:3-digit method

Basic idea: Select the first element of this set of data, the middle element, the last element, and take the middle element as the datum point in these three numbers. For example, there is a set of elements:1 x 6 9 , the first element, the middle element. The last element is: 1,23,10, then this median should be ten.

Select the implementation code for the median, as follows:

/**Gets the index of the median between left and right*/Static intGetmedianindex (int[] arr,intLeftintRight ) {    intMid = left + (right-left) >> 1; if(Arr[left] >Arr[mid]) {        if(Arr[left] <Arr[right]) {            returnLeft//Mid < Left < right}Else {            if(Arr[right] >Arr[mid]) {                returnRight//Mid < Right < left            }             returnMid//Right < mid < left        }    } Else {        if(Arr[mid] <Arr[right]) {            returnMid//Left < mid < right}Else {            if(Arr[left] >Arr[right]) {                returnLeft//Right < Left < mid            }            returnRight//Left < Right < mid        }    }}

The following code is just some boring logical judgment, if a number m is the median, then there is L <= M <= R, in fact, the above code is based on this feature to judge.

Optimization Two: Inter-cell optimization

When the number of elements in a sub-interval is small (typically more than 10, say 13), it is much more efficient to use an insertion sort than a quick sort. Because if the number of elements of the interval is 13 o'clock, and then continue to divide, the interval will be relatively small. The following code is an implementation of a simple insert sort:

/**Insert Sort in the left-to-right interval in an array*/Static voidInsertionsort (int[] arr,intLeftintRight ) {     for(intm = left + 1; M <= right; m++) {        intpos =m;  while(pos! = Left && arr[pos-1] >Arr[pos]) {            inttemp = arr[pos-1]; Arr[pos-1] =Arr[pos]; Arr[pos]=temp; POS--; }    }}
The result of the final optimization

Combine the above two optimizations together, the final completion of the optimization of fast sorting, as the following code only gives a key part of the code, the completion of the code please click the relevant resources below the sample code download for download.

Static voidQuickSort (int[] arr,intLeftintRight ) {    if(Left >=Right ) {        return; }    intInsertionthreshold = 13; //If this interval has only 13 elements, insert sort directly    if(Right-left + 1 <=insertionthreshold)        {Insertionsort (arr, left, right); return; }    intFirst = left and last =Right ; //three count in    intMID =Getmedianindex (arr, left, right);    Swap (arr, Mid, first); intBase =Arr[first]; //... The rest of the code omits the}Static voidSwapint[] arr,intIndex1,intindex2) {    inttemp =ARR[INDEX1]; ARR[INDEX1]=Arr[index2]; ARR[INDEX2]=temp;}
Related Resources
    • This article example source code download

Easy Learning Quick Sort (ii)--Quick Sort optimization

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.