First, the principle of rapid sequencing
Using the divide-and-conquer algorithm for quick ordering, the last element is identified, and then from the beginning, the element that is smaller than this one is placed on the left, and the element larger than this one is placed on the right, and then the array is divided into two parts with this identity, and then the array of the front and back parts of the identity is sorted. This way, you can get an ordered array.
Second, the rapid sequencing of pseudo-code implementation
1 QUICKSORT (A, P, R)2 ifP <R3 Then q←partition (A, P, R)4QUICKSORT (A, p, q-1) 5QUICKSORT (A, q + 1, R)6 PARTITION (A, P, R)7 X←a[r]8I←p-19 forJ←p to R-1Ten Do ifa[j]≤x OneThen i←i + 1 A Exchange A[i]←→a[j] -Exchange A[i + 1]←→a[r] - returni + 1
Third, the fast sorting Java code implementation
1 ImportJava.util.Comparator;2 3 4 Public classQuickSort {5 6 Public Static<T>voidQuickSort (t[] T,intPintR, Comparator<?SuperT>c) {7 if(P < R-1){8 intQ =partition (T, p, R, c);9 quickSort (t, p, Q, c);TenQuickSort (T, q+1, R, c); One A } - } - Public Static<T>intPartition (t[] T,intPintR, Comparator<?SuperT>c) { theT x = t[r-1]; - inti = P-1; - for(intj = P; J < R-1; J + +){ - if(C.compare (T[j], x) < 0){ +i = i + 1; -T temp =T[i]; +T[i] =T[j]; AT[J] =temp; at } - } -T temp = t[i+1]; -T[I+1] = t[r-1]; -T[R-1] =temp; - returni + 1; in } - to Public Static<T>voidQuickSort (t[] T, comparator<?SuperT>c) { +QuickSort (T, 0, T.length, c); - } the * Public Static voidMain (string[] args) { $ //test the code herePanax Notoginsenginteger[] INTs =NewInteger[]{2, 0, 5, 23, 1, 4, 8, 56, 19}; -QuickSort (INTs,NewComparator<integer>(){ the Public intCompare (integer O1, integer o2) { + returnO1-O2; A } the }); + - for(inti = 0; i < ints.length; i + +){ $System.out.print (Ints[i] + ""); $ } - } -}
Operation Result:
java-Quick Sort