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