When learning Java to an array, we learned two sorting methods: Select sort and bubble sort, bubble sort is the advanced stage of selecting sort, and streamline the process of operation. Understand that the Java language has provided a way of sorting: through Util. Arrays.sort can be automatically sorted on an array, and the sorting method uses a quick sort.
Quick Sort is the advanced stage of bubble sort, so what is the basic principle of fast sorting? By looking up the data and understanding the algorithm, I have also tried to write some algorithm principles of fast sorting.
1 PackageArray;2 //Quick Sort Principle3 Importjava.util.Arrays;4 Public classQuickSort5 {6 Public Static voidMain (string[] arg)7 {8 int[] sort={56,656,23,45,523,46,1,34,78};//sort this random ordinal group. 9 intlow=0;//defines a starting angle and the end of a corner label. Ten intTall=sort.length-1; One Suanfa (sort,low,tall); A System.out.print (arrays.tostring (sort)); - - } the Public Static voidSUANFA (int[] Sort,intLowintTall//bring the array and the corners of the ends into the new method - { - intKey=sort[low];//determine a key value, which is compared to other values using this value. The general selection key is the first value or the last value. - intA=low;//Define a, B, both pointers, and both ends begin to move toward the middle. A numeric comparison with a key value and 2 pointers. + intb=tall; - while(A<B)//Two The pointer cannot overlap or pass through, the position of the key when coincident is already determined. + { A while(A<b&&sort[b]>key)//the pointer to the right moves to the left, compared to key. at { -b--; - } - if(Key>=sort[b])//When the value of key is greater than the value of the right hand pointer. The value of the 2-end pointer Exchange position. - { - inttamp=Sort[a]; insort[a]=Sort[b]; -sort[b]=tamp; to } + while(A<b&&sort[a]<key)//the left pointer moves to the right, compared to key. - { thea++; * } $ if(Key<=sort[a])//When the value of key is less than the value of the right hand pointer. The value of the 2-end pointer Exchange position. Panax Notoginseng { - inttamp=Sort[a]; thesort[b]=Sort[a]; +sort[a]=tamp; A } the } + //This determines the position of key, the left of key is less than the number of keys, the right is all greater than the number of keys, divided into 2 parts. A recursive loop determines the position of all values. - if(b<tall) $ { $SUANFA (sort,b+1, tall); - } - if(a>Low ) the { -SUANFA (sort,low,a-1);Wuyi } the } -}
The printed results are [1, 23, 34, 45, 46, 523, 78, 656, 656], which proves to be correct
Algorithms for fast Sorting