Sort _ Quick Sort _ Another way to take a dividing point

Source: Internet
Author: User

The most basic dividing point is determined by the last value of the array, which is now the last value of the array as a dividing point, but it takes three items to take a method of transformation, and finally its position value is the largest of three.

 Public classArrayins {Private Long[] A; Private intNelems;  PublicArrayins (intmaxSize) {a=New Long[MaxSize]; Nelems=0; }     Public voidInsertLongvalue) {A[nelems]=value; Nelems++; }     Public intsize () {returnNelems; }     Public voiddisplay () { for(intj=0;j<nelems;j++) {System.out.print (A[j]+" ");    } System.out.println (); }     Public voidQuickSort () {Recquicksort (0, NElems-1); }    //Recursive method     Public voidRecquicksort (intLeftintRight ) {        intSize=right-left+1; //This can be improved .        if(size<=3) {            //with three left, it's a manual judgment.Manualsort (left,right); }Else{            Longmedian=medianOf3 (left,right); //pivot can be improved            Longpivot=median; intPartition=partitionit (left, right, pivot);//obtained by dividing the method returnRecquicksort (left, partition-1);//Pre-section re-partitioningRecquicksort (partition, right);//The latter part is divided again        }    }    Private LongMEDIANOF3 (intLeftintRight ) {        //three items of data take the median size as a dividing point        intCenter= (left+right)/2; if(a[left]>A[center]) swap (left, center); if(a[left]>Right ) Swap (left, right); if(a[center]>A[right]) Swap (center, right); Swap (center, right-1); returnA[right-1]; }    Private voidManualsort (intLeftintRight ) {        intSize=right-left+1; //determine the size of the array that needs to be divided now        if(size<1)            return; if(size==2) {            if(a[left]>A[right])                {Swap (left, right); return ; }                    }Else {            if(A[left]>a[right-1]) swap (left, right-1); if(a[left]>A[right]) swap (left, right); if(A[right-1]>a[right]) swap (right-1, right); }            }     Public intPartitionit (intLeftintRightLongpivot) {        intLeftptr=Left ; intRightptr=right-1;  while(true) {            //on the left, look for a value greater than             while(a[++leftptr]<pivot); //on the right, find less than a specific value             while(a[--rightptr]>pivot); if(leftptr>=rightptr) Break; Else                //the value that the interchange points toswap (leftptr, rightptr); } swap (leftptr, right-1); returnleftptr; }     Public voidSwapintDex1,intdex2) {        Longtemp; Temp=A[DEX1]; A[DEX1]=A[dex2]; A[DEX2]=temp; }}
 Public classTest { Public Static voidMain (string[] args) {intmaxsize=100; Arrayins Arraypar=NewArrayins (maxSize); Arraypar.insert (60); Arraypar.insert (30); Arraypar.insert (80); Arraypar.insert (10); Arraypar.insert (70); Arraypar.insert (90); Arraypar.insert (00); Arraypar.insert (20); Arraypar.insert (40);        Arraypar.display ();        Arraypar.quicksort ();    Arraypar.display (); }}

Sort _ Quick Sort _ Another way to take a dividing point

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.