Quick Sort (Quicksort) is an improvement to the bubbling sort. It is first found in the array of a key number, the first order will be smaller than the number of keys on its left, the key is larger than the number on its right. When the first trip is finished, the left and right are sorted recursively, until the entire array is in order until the end of the sequence.
A quick sort of algorithm is:
1) Set two variables I, J, when sorting begins: I=0,j=n-1;2) takes the first array element as the key data, assigns the value toKey, i.e.Key=A[0];3) forward search from J, that is, after the start of a forward search (j--), find the first less thanKeyValue a[j], a[j] and A[i] interchange, 4) from I start backward search, that is, to start backward search (i++), to find the first one greater thanKeyA[i], will a[i] and A[j] interchange; 5) Repeat 3rd, 4, until i=j; (3,4 step, no value matching the criteria, that is, 3 a[j] is not less thanKey, 4 A[i] not greater thanKeyChange the value of J, I so that j=j-1,i=i+1 until it is found. Locate the value that matches the condition, and the J pointer position does not change when I exchange it. In addition, I==J this process must be exactly when the i+ or J completes, at which time the loop ends). Diagram: The following is the implementation of code to quickly sort the class:
PackageCom.dxx.order; Public classQuicsort { Public int[] Sortarrs (int[]arrs,intLowintHigh ) { intL =Low ; inth =High ; intPovit =Arrs[low]; //When L while(l<h) { //The key and the right H-digit comparison, if less than the right H-bit number, then h-1, that is, the right side of the flag to move left one while(Lpovit) H--; if(l<h) { //When the key point is greater than the number of H flag bits, the position of the interchange two number, l++, compares the key point to the number of the left L flag bit inttemp =Arrs[l]; ARRS[L]=Arrs[h]; ARRS[H]=temp; L++; } //In turn, compare the key number to the number on the left while(L
povit) L++; if(l<h) { //When the key point is less than the number of the L flag bit, the position of the interchange two number, h--, when l=h when a trip sort ends inttemp =Arrs[l]; ARRS[L]=Arrs[h]; ARRS[H]=temp; H--; } } if(L>low) Sortarrs (Arrs, Low, l-1); if(H, high); returnArrs; } Public voidPrintarrs (int[]arrs] { for(intI:arrs) {System.out.print (i+ " "); } System.out.println (); } }
Main program Run class:
Package Com.dxx.order; Public class maintest { publicstaticvoid main (string[] args) { int arrs[] = {14,3,2,5,12,8,6,7,10,11,1,9,13}; New Quicsort (); Quicsort.printarrs (ARRS); = Quicsort.sortarrs (arrs,0,arrs.length-1); Quicsort.printarrs (ARRS); }}
Quickly sort Java