Source code:
today read the "Big talk data structure" fast sorting algorithm, according to the book to the part of the code to improve, the specific implementation of the following: 1 /**2 * java for quick sorting3 */4 Public classquicksort_1 {5 6 Public Static voidMain (string[] args) {7 //TODO auto-generated Method Stub8 int[] list={34,3,53,2,23,7,14,10};9Quicksort_1 qs=Newquicksort_1 ();Ten Qs.quicksort (list); One for(inti=0;i<list.length;i++){ ASystem.out.print (list[i]+ ""); - } - } the - Public voidQuickSort (int[] list) { - if(List.length > 0) {//to see if an array is empty -QSort (list, 0, list.length-1); + } - } + A Public voidQSort (int[] list,intLowintHigh ) { at intpivot; - if(Low <High ) { -Pivot = partition (list, low, high);//divides a list array into a pivot value -QSort (list, low, pivot-1);//recursive ordering of low-word tables -QSort (list, pivot + 1, high);//recursive ordering of high-character tables - } in } - to Public intPartitionint[] list,intLowintHigh ) { + intPivotKey = List[low];//the first of the arrays as a middle axis - while(Low <High ) { the while(Low < High && List[high] >=PivotKey) { *high--; $ }Panax NotoginsengSwap (list, low, high);//Less than mid-axis records moved to the low end - while(Low < High && List[low] <=PivotKey) { thelow++; + } ASwap (list, low, high);//a record larger than the middle axis moves to the high end the } + returnLow//returns the position of the middle axis - } $ /** $ * Function: Swap the position of the array subscript low and high - * @paramList - * @param Low the * @param High - */Wuyi Private voidSwapint[] list,intLowintHigh ) { the //TODO auto-generated Method Stub - inttmp=List[low]; Wulist[low]=List[high]; -list[high]=tmp; About } $ - } -
The principle of quick sorting:
From the data sequence to be sorted by any data (such as the first data) as the cutoff value, all the data elements smaller than it is placed on the left, all the elements larger than it is placed on the right, so that a trip down the sequence to form about two sub-sequences, the left sequence of data element values are smaller than the cutoff value, The value of the data element in the right sequence is greater than the cutoff value.
Next, the left and right two sub-sequences are recursive, the center element is re-selected for two sub-sequences and adjusted with this rule, until the elements of each subsequence are left only one, sorting is complete.
Quick sort of Java