1, the algorithm think
By a trip to sort the data to be sorted into separate two parts, where the left part of all the data is smaller than the right part of all the data, and then this method of the two parts of the data are quickly sorted, the entire sorting process can be recursive, so as to achieve the entire data into an ordered sequence.
2. Algorithm implementation
Packagetest; Public classQuickSort { Public Static voidMain (string[] args) {int[] n ={1,3,6,2,9,6,8,7}; Quicksort (N,0,7); for(inti:n) {System.out.print (i+ "\ T"); } } Public Static voidQuicksortint[] n,intLeftintRight ) { intDP; if(left<Right ) {DP=partition (N,left,right); Quicksort (N,LEFT,DP-1); Quicksort (N,DP+1, right); } } Public Static intPartitionint[] n,intLeftintRight ) { intPivot =N[left]; while(left<Right ) { while(left<right&&n[right]>=pivot) right--; if(left<Right ) N[left++]=N[right]; while(left<right&&n[left]<=pivot) left++; if(left<Right ) N[right--]=N[left]; } N[left]=pivot; returnLeft ; } }
Quick Sort (Ascending)