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