The precondition technology of dividing _ fast sort

Source: Internet
Author: User

A value defined as a dividing point, placing less than it on one side, greater than it on the other side, is not a sort.
In the process of dividing, there are two marks, each starting from the left and right, they are looking for values above the dividing point, below the value of the dividing point.

 Public classArraypar {Private Long[] A; Private intNelems;  PublicArraypar (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 intPartitionit (intLeftintRightLongpivot) {        intleftptr=left-1;//Why should-1        intrightptr=right+1;//Why do you want +1 ?//the reason the initial value is not left,right is that their value changes after the Loop ++leftptr,--rightptr,swap () is the value after the change//if Swap (0,10), the original intent, in the wrong case, the final swap (1,9), in the correct initial value case is swap (0,10)         while(true) {            //on the left, look for a value greater than             while(Leftptr<right && a[++leftptr]<pivot); //on the right, find less than a specific value             while(Rightptr>left && a[--rightptr]>pivot); if(leftptr>=rightptr) Break; Else                //the value that the interchange points toswap (leftptr, rightptr); }        returnleftptr; /** Can be written like this (but according to the idea is more clear) * * int leftptr=left;        int rightptr=right;            while (true) {///left looking for while (leftptr<right && A[leftptr++]<pivot) greater than a specific value;            Find the while (Rightptr>left && A[rightptr--]>pivot) Less than the specified value on the right.            if (leftptr>=rightptr+2) break;        else//Swap points for the value swap (leftPtr-1, rightptr+1);         } return leftPtr-1; *          *          */            }     Public voidSwapintDex1,intdex2) {        Longtemp; Temp=A[DEX1]; A[DEX1]=A[dex2]; A[DEX2]=temp; }                                                                                                }
 Public classTest { Public Static voidMain (string[] args) {intmaxsize=100; Arraypar Arraypar=NewArraypar (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.partitionit (0, Arraypar.size ()-1, 50);    Arraypar.display (); }}
























}

The precondition technology of dividing _ fast sort

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.