Why do you have a two-way quick sort? When the array to be sorted has a large number of repetitions, if it is just single channel, a large number of duplicate values will be in one of the two segmented arrays, which in extreme cases degrades to O (n^2) level, which has a greater effect on sorting performance. When there are a large number of duplicate values in the array to be sorted, we can use the two-way method for quick sorting. That is, two pointers move, one end of the value of the margin is small to continue to move, one end of the score bounds to continue to move, the situation does not meet the conditions of each other to exchange, so that the equivalent value will be more evenly distributed at the ends of the array.
The specific Java code implements the following values:
Package Com.newtouch.data.sort;import Com.newtouch.data.test.sorttesthelper;public class Quicksorttwoways {private static int partition (comparable[] arr, int l, int r) {//random in Arr[l ... R] Range, select a numeric value as the pivot swap (arr, l, (int) (Math.random () * (R-l + 1)) + L); Comparable v = arr[l]; Random in Arr[l ... R] in the range, select a numeric value as the fixed-point pivot int i = L + 1, j = r; while (true) {//Note the boundary here, Arr[i].compareto (V) < 0, cannot be Arr[i].compareto (v) <= 0 while (i <= R && Arr[i].compareto (v) < 0) i++; Note the boundary here, Arr[j].compareto (v) > 0, cannot be Arr[j].compareto (v) >= 0//think about why? while (J >= L + 1 && arr[j].compareto (v) > 0) j--; if (J > J) break; Swap (arr, I, j); i++; j--; } swap (arr, L, J); Return J; }//Recursive use quick sort, to arr[l ... R] to the range of private static void sort (CompArable[] arr, int l, int r) {int p = partition (arr, L, R); Sort (arr, l, p-1); Sort (arr, p + 1, R); } public static void sort (comparable[] arr) {int n = arr.length; Sort (arr, 0, n-1); } private static void Swap (object[] arr, int i, int j) {Object t = arr[i]; Arr[i] = Arr[j]; ARR[J] = t; }//test QuickSort public static void Main (string[] args) {//two-way fast sorting algorithm is also an O (NLOGN) complexity algorithm//can be easily located within 1 seconds 1 million order of magnitude data int N = 1000000; integer[] arr = Sorttesthelper.generaterandomarray (N, 0, 100000);
Sorttesthelper.testsort ("Com.newtouch.data.sort.QuickSortTwoWays", arr);
}
Test helper Classes:
Packagecom.newtouch.data.test;ImportJava.lang.reflect.Method;ImportJava.lang.Class;ImportJava.util.Random; Public classSorttesthelper {//Sorttesthelper does not allow any instances to be generated PrivateSorttesthelper () {}//generates a random array of n elements, each of which has a random range of [RangeL, RangeR] Public StaticInteger[] Generaterandomarray (intNintRangeL,intRangeR) { assertRangeL <=RangeR; Integer[] Arr=NewInteger[n]; for(inti = 0; I < n; i++) Arr[i]=NewInteger (int) (Math.random () * (Ranger-rangel + 1) +RangeL)); returnarr; } //generate an almost-ordered array//first, a fully ordered array containing [0...n-1] is generated, followed by a random exchange of swaptimes on the data//Swaptimes defines the degree of disorder of an array://Swaptimes = = 0 o'clock, the array is fully ordered//the larger the swaptimes, the more the array tends to be unordered Public StaticInteger[] Generatenearlyorderedarray (intNintswaptimes) {integer[] arr=NewInteger[n]; for(inti = 0; I < n; i++) Arr[i]=NewInteger (i); for(inti = 0; i < swaptimes; i++) { intA = (int) (Math.random () *N); intB = (int) (Math.random () *N); intt =Arr[a]; Arr[a]=Arr[b]; ARR[B]=T; } returnarr; } //Print all the contents of an ARR array Public Static voidPrintArray (object[] arr) { for(inti = 0; i < arr.length; i++) {System.out.print (arr[i]); System.out.print (‘ ‘); } System.out.println (); return; } //determine if arr array is ordered Public Static Booleanissorted (comparable[] arr) { for(inti = 0; i < arr.length-1; i++) if(Arr[i].compareto (arr[i + 1]) > 0) return false; return true; } //test sortclassname the corresponding sorting algorithm to sort the correctness of the results obtained by the ARR array and the algorithm run time Public Static voidTestsort (String sortclassname, comparable[] arr) {//Through the reflection mechanism of Java, by sorting the class name, run the sort function Try { //a class object that obtains a sort function by SortclassnameClass Sortclass =Class.forName (sortclassname); //Sorting method by class object of sort functionMethod SortMethod = Sortclass.getmethod ("Sort",NewClass[]{comparable[].class}); //The sort parameter has only one, which is a comparable array of arrObject[] params =NewObject[]{arr}; LongStartTime =System.currenttimemillis (); //Call sort functionSortmethod.invoke (NULL, params); LongEndTime =System.currenttimemillis (); assertissorted (arr); System.out.println (Sortclass.getsimplename ()+ ":" + (Endtime-starttime) + "MS"); } Catch(Exception e) {e.printstacktrace (); } }}
Implementation of Java Two-way fast sequencing