The idea of a quick line must be understood by everyone, with two pointers before and after, toward the middle. My partition function ensures that all the same numbers are compared once, and that they get closer together.
Code:
Public classMain { Public Static int[] Partition1 (int[] arr,intBeginintEndintpivotvalue) { intsmall = begin-1; intCur =begin; intBig = end+1; while(cur!=big) { if(Arr[cur] <pivotvalue) {Swap (arr,++small, cur++); } Else if(Arr[cur] >pivotvalue) {Swap (arr, cur,--big); } Else{cur++; } } int[] Range =New int[2]; range[0] =Small; range[1] =Big; returnrange; } Public Static voidQuickSort (int[] Array,intLowintHigh ) { if(Array = =NULL|| Array.length==0) { return; } if(low>=High ) { return; } int[] res =Partition1 (array, low, high, array[low]); QuickSort (array, low, res[0]); QuickSort (Array, res[1], high); } Public Static voidMain (string[] args) {int[] A = {34,2,25,1,12,34,12,56,23,15,34,12,12,54,34}; QuickSort (A,0, A.length-1); for(inti=0; i<a.length; i++) {System.err.print (A[i]+" "); } } Private Static voidSwapint[] Array,intLowintHigh ) { inttemp =Array[low]; Array[low]=Array[high]; Array[high]=temp; } }
Quick Sort--java