Fast Sorting is one of the most efficient sorting algorithms, and it uses the principle of recursion to divide an array into two parts without restriction until all the data is sorted.
A quick sort is an improvement to the bubbling sort. The basic idea is to divide the sorted data into two separate parts by a sort of sequence, in which all the data is smaller than the other part of the data, and then the two parts of the data are quickly sorted by this method, and the whole sort process can be recursive to achieve the whole data into ordered sequence.
If the array to be sorted is a[1] ... A[n-1], the first arbitrary selection of data (usually the first data) as the intermediate data, and then put all the smaller than it in front of it, all the larger than its number is placed behind it, this process is called a quick sort. An algorithm for fast sorting is as follows:
(1) Set two variables I, J, when the sort begins: i = 1,j = n–1.
(2) The first array element as the intermediate data, assigned to Privot, that is, pivot = a[0].
(3) Forward search from J, that is, after starting the forward search (j--), find the first value less than X.
(4) From I start backward search, that is, start backward search (i++), find the first value greater than X, and the number found in the previous step exchange.
(5) Repeat steps 3rd, 4, until I >= J.
(6) Then the number of J is exchanged with pivot.
(7) Finally, the former array of J and J to the last array, and then recursive fast sorting.
The following is the code implementation for this topic:
1 PackageCom.fhcq.quicksort;2 3 Public classQuickSort {4 5 Public Static voidMain (string[] args) {6 //TODO auto-generated method stub7 8 int[] A =New int[] {5, 9, 8, 4, 7, 3, 6, 2};//define Arrays9Print (a);//The order before printingTenSort (A, 0, a.length-1);//Sort OnePrint (a);//Print a sorted result A} - - //Printing method the Private Static voidPrintint[] before) { - //TODO auto-generated method stub - for(inti = 0; i < before.length; i++) {//Traversal -System.out.print (Before[i] + " ");//print, separated by spaces +} -System.out.println ();//Line break +} A at //Sorting methods - Static voidSortint[] A,intLowintHigh) { - //TODO auto-generated method stub - if(Low >= High) {//low is less than or equal to high, it returns directly - return; -} in if((high-low) = = 1) {//If there are only two digits, compare directly - if(A[0] > A[1]) { toSwap (A, 0, 1); + return; -} the} * intPivot = A[low];//Take the first number as the middle number $ //Zoshi The current subscript number, starting with the 2nd digit, starting from the lastPanax Notoginseng intleft = low + 1; - intright = high;//Right thumb current subscript number the while(Left < right) {//Left /right cycle + //From the left to start looking A while(Left < right && left <= high) {//If the left small less right operand is always circulating the if(A[left] > Pivot) {//Find a large number + Break; -} $left++;//left subscript go to the right. $} - //From the right to start looking - while(left <= right && right > Low) {//If left is greater than right, it loops the if(A[right] <= pivot) {//Find a small number - Break;Wuyi} theright--;//Right subscript go to the left a little -} Wu if(Left < right) {//If you are not finished, exchange numbers -Swap (A,right,left); About} $ -Swap (a,low,right);//Exchange intermediate numbers -Sort (a,low,right);//Sort the preceding array -Sort (A,right+1,high);//Sort behind array A} +} the - //drop-down method $ Private Static voidSwapint[] Array,intIintj) { the //TODO auto-generated method stub the the intTemp thetemp = Array[i]; -Array[i] = Array[j]; inARRAY[J] = temp; the} the About} the
Implementing a fast sorting algorithm with Java