Java algorithm-Quick sort

Source: Internet
Author: User

Fast sorting is also a sort of "divide and conquer" algorithm implemented by the merge method, its charm is that it can be in each partition (the core of the sorting algorithm) can be a single array element to determine its final correct position (positioning, the next cycle will not consider this element).

The quick-sort partition operation is performed in the following logic, assuming that the array for this order is arr:

1) Select an element (for simplicity, select the first element of this partition, i.e., arr[0]) as the datum element, and the next step will determine the final position after the sort is completed;

2) 1) Next you need to traverse [1...n-1] corresponding array elements to help find the arr[0] value (in v substitution) corresponding to the location, define I as the index of the current access array, LT is the largest index value less than V, the GT is a minimum index value of V, then during the traversal process, If I find that the value I points to is equal to V, then the I value is added 1 to continue the next comparison, if I point to a value smaller than V, then I and LT corresponding elements are exchanged, and then two indexes plus 1, if I point to a value larger than V, then I with the GT corresponding element is exchanged, then I self-increment, GT self-reduction. After the loop traversal is complete (i > GT), the corresponding value of [0...lt-1] is guaranteed to be smaller than V, [LT]. The value between GT] is equal to V, and [gt+1...n-1] corresponds to a value greater than V.

3) The [0...lt-1] and [gt+1...n-1] two sub-arrays are sorted, so recursively, until the length of the sub-sub-subarray is 0.

Here is a specific example of a partition:

Initial (i = 1, lt = 0, GT = 8): [41, 59, 43, 26, 63, 30, 29, 26, 42] (need to determine the position of the 0th[41]) First trip (i= 1, lt = 0, GT = 8): [(1st[59) > 41,1st[59]<->8th[42],gt-----------) Second trip (i= 1, lt = 0, GT = 7): [(1st[42) > 41,1st[42]<->7th[26],gt-----------) Third trip (i= 1, lt = 0, GT = 6): [(1st[26) <, 1st[26]<->0st[41],i++, lt++, Max, Max, (+),) Fourth trip (i= 2, lt = 1, GT = 6): [(2nd[43) > 41,2nd[43]<->6th[29],gt-----------) Fifth trip (I= 2, lt = 1, GT = 5): [(2nd[29) < (2nd[29]<->1st[41],i++,lt++), +,-----) Six trips (I= 3, lt = 2, GT = 5): [(3rd[26] < 41,3rd[26]<->2nd[41],i++,lt++---------) Seventh trip (i= 4, lt = 3, GT = 5): [(4th[63] > 41,4th[63]<->5th[30],gt-----------) Eighth trip (I= 4, lt = 3, GT = 4): [(4th[30] < 41,4th[30]<->3rd[41],i++,lt++)--------

As can be seen, after a partition, with 41 as the dividing line, 41 to the left is smaller than its elements, 41 to the right are larger or equal to the element (of course, this instance is more special, not appear and 41 equal elements). Quick sort as the name implies is the sort speed is very fast, I will put on my machine to run the time comparison chart of the various sorting methods. It is worth mentioning that the JDK in the arrays tool built in the sort method is to join the insertion sort and three quick sort implementation, interested students can look at the JDK source code.

Quick sort use the divide-and-conquer strategy to divide a sequence into two sub-sequences.

Think of the whole sequence as an array, the 0th position as the middle axis, and the last one, if it is smaller than it, does not do any processing than it; The end is exchanged again and the smaller one, than it is smaller than that, which does not exchange much larger than it. This cycle, a trip to the completion of the order, the left side is smaller than the middle axis, the right is larger than the middle axis, and then the division of the method, respectively, the two separate array to sort.

Implementation code:

/*** Quick Sort <br/> * <ul> * <li> Select an element from the series, called "Datum" </li> * <li> Reorder series, all elements are placed in front of the datum in a smaller position than the base value. All elements that are larger than the base value are placed behind the datum (the same number can be on either side). After this split, * The benchmark is its last position. This is called a split (partition) operation. </li> * <li> recursively sorts sub-columns that are less than the base value element and that are larger than the base value element. </li> * </ul> * *@paramnumbers *@paramStart *@paramEnd*/   Public Static voidQuickSort (int[] numbers,intStartintend) {       if(Start <end) {           intbase = Numbers[start];//Selected Datum value (first value as Datum)        intTemp//Record interim intermediate values        inti = start, j =end;  Do {                while((Numbers[i] < base) && (I <end)) I++;  while((Numbers[j] > Base) && (J >start)) J--; if(I <=j) {Temp=Numbers[i]; Numbers[i]=Numbers[j]; NUMBERS[J]=temp; I++; J--; }           }  while(I <=j); if(Start <j) QuickSort (Numbers, start, j); if(End >i) QuickSort (numbers, I, end); }   }

Java algorithm-Quick sort

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.