Java enables quick sorting

Source: Internet
Author: User

Optimized for some details 10% faster than the previous quick row

/** * @authorCLY * Quick Sort*/ Public classMyquicksort {/*** Treats row array sort (ascending) *@paramarr Queue Array *@paramThe starting position of the pivot pivot in the array to be sorted (sort start bit) *@paramend bit of this quick row (sort end bit)*/     Public Static voidSortint[] arr,intPivotintend) {        intTmp_pivot =pivot; intTmp_end =end; //true when pivot is to the left of the array, false on the right        BooleanFlag =true; //The whole process is the end toward the pivot.         while(tmp_pivot!=tmp_end) {            if(flag) {//pivot on the left .                 while(tmp_pivot<tmp_end) {                    //If it is, the pivot is swapped to the right, and the number is changed to the left, which is smaller than the pivot.                    if(arr[tmp_pivot]>Arr[tmp_end]) {                        intTMP =Arr[tmp_pivot]; Arr[tmp_pivot]=Arr[tmp_end]; Arr[tmp_end]=tmp; intTmp_index =Tmp_pivot; Tmp_pivot=Tmp_end; Tmp_end=Tmp_index; Tmp_end++;  Break; }Else{//find the last number on the right to see if it's smaller than the pivottmp_end--; }} Flag=false; }Else{//pivot on the right .                 while(tmp_pivot>tmp_end) {                    //If it is, the pivot is swapped to the left, and the number larger than the pivot is swapped to the left.                    if(arr[tmp_pivot]<Arr[tmp_end]) {                        intTMP =Arr[tmp_pivot]; Arr[tmp_pivot]=Arr[tmp_end]; Arr[tmp_end]=tmp; intTmp_index =Tmp_pivot; Tmp_pivot=Tmp_end; Tmp_end=Tmp_index; Tmp_end--;  Break; }Else{//find the next left number to see if it's bigger than the pivottmp_end++; }} Flag=true; }        }        //at this point the number of the pivot to the left is smaller than it, the number on the right is larger than it. //if the entire array length is less than 2, it means that it is in the bottom line.         if(end-pivot<2) {            return; }        //if the current pivot is to the right of the starting point, it means that there are values on the left side of the pivot, which can be quickly        if(tmp_pivot>pivot) {sort (arr, pivot, Tmp_pivot-1);//make a quick row of the number on the left        }        //if the current pivot is to the left of the end point, it indicates a value on the right side of the pivot, which can be quickly        if(tmp_pivot<end) {sort (arr, Tmp_pivot+1, end);//to the right of the number of fast row        }    }}

Java enables quick sorting

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.