PackageKpp.sort;/*** Quick Sort * General selection of the first element as the pivot element, save to pivot, determine the head and tail pointer left,right, * First order the entire array element, when the elements on both sides of the pivot element ordered, and then the pivot element on each side of the two arrays to perform sorting * collation * 1. The right pointer iterates from the left, if it is smaller than the current pivot element pivot, assigns data[right] to Data[left] * The 2.left pointer traverses from left to right, if larger than the current pivot element pivot, assigns Data[left] to data[ Right] * 3. When the left and right pointers come together, such as index, Pivot is assigned to Data[index] * *@authorKPP **/ Public classQuickSort { Public Static voidMain (string[] args) {//TODO auto-generated Method Stub intArray[] = {49,38,65,97,176,213,227,49,78,34,12,164,11,18,1}; QuickSort (Array,0, Array.length-1); for(inti = 0; i < Array.Length; i++) {System.out.print (Array[i]+" "); } } Private Static intQuickSort (intData[],intLeftintRight ) { intpivot; if(Left <Right ) {Pivot=sort (data,left,right); SYSTEM.OUT.PRINTLN (pivot); QuickSort (Data,left,pivot-1); QuickSort (Data,pivot+1, right); } return0; } Private Static intSortintData[],intLeftintRight ) { intPivot =Data[left]; while(Left <Right ) { while(Left < Right&&data[right] >=pivot) { Right--; } Data[left]=Data[right]; while(Left < Right&&data[left] <=pivot) { Left++; } Data[right]=Data[left]; } //Left==right, to prove that the ancestral temple sorted, the pivot element is assigned to Data[left]Data[left] =pivot; returnLeft ; } }
Quick Sort Java Implementation