Sorting algorithm six fast sort __ data structure and algorithm

Source: Internet
Author: User
1, the basic idea of rapid sortingThe fast sort c.r.a.hoare a sort of division Exchange in 1962. Using Divide and CurePolicy (Divide-and-conquermethod) The basic idea of this method is: (1) first from a number of numbers as a reference number (2) partitioning process, will be more than the number of large numbers to its right, less than or equal to its number all put it to the left, to get a index position, Divide the array into the left and right parts (excavation fill number + partition method) (3) and repeat the second step to the left and the back until the interval is only one number. What is harder to understand (recursive implementation) is the interval division of the second step, where Digging hole filling numberThe method. Example Analysis:
Int[] arr = {3,7,5,1,6,2,4};
0 1 2 3 4 5 6
3 7 5 1 6 2 4
(1) Take the first number as the base number.    Initialized to low = 0;high = 6;priv = Arr[low] = 3; Explanation: Since the data in arr[0] has been saved to priv, it can be seen as digging a hole in the position where the array index is 0 (2) to find the first number that is smaller or equal to the Priv from high start to the left. When high=5 is less than Priv, the arr[5] is dug out to Arr[low], that is, arr[low]=arr[high] = 2, at which point the position of index 5 is a pit, and an element needs to be found to fill (3) from low start to the right to find the first one larger than priv or equal to priv number. When the low=1, arr[low] = 7>priv, the condition is arr[1] assigned to Arr[high], that is arr[high] = arr[low] = 7; At this point, the location of the index low=1 is a pit. At this point the array becomes
0 1 2 3 4 5 6
2 3 5 1 6 7 4
Low = 1,high = 5 Priv = 3 Repeat the previous step: first from the back to the first to be less than or equal to the base element, and then the first to be more than equal to the base element. Start looking forward from high, when the high=3, in line with the conditions, will arr[3] dug up, fill in the last pit, that is arr[low] =arr[1] = Arr[high] = 1, at this time arr[3] place for the pit from low start looking backwards, when low = 2, meet the conditions, will be arr[2] Dig, fill to the last pit namely arr[3] = arr[2] = 5 at this time high=3.low=2. Start looking for the Low==high, so quit. Finally, the last pit arr[2] is populated with a datum element. ARR[2] = Priv = 3; The resulting array is:
0 1 2 3 4 5 6
2 1 3 5 6 7 4
The first round uses arr[low] to divide the whole array into two parts, with the arr[2] demarcation, the number on the left is less than arr[2], and the right number is greater than arr[2. The next step is to repeat the above steps for the left and right two intervals. Recursive operation. 2. Fast Sort ImplementationThe digging pit fills the number method to realize concretely: (1) The left edge of the low array, the high array right boundary, Priv = Arr[low] to dig up the datum to form a pit Arr[low] (2) high from the back to find the first one smaller than it, found after digging this number to fill in the previous pit Arr[low] (3) Low from the front backward to find the first one larger than it, after finding out this number, fill in the previous pit Arr[high] (4) Repeat Execution (2), (3) step, know Low==high, the base number Priv into Arr[low], return low, Low is the boundary between the left and right parts.
Java code implementation: public int partition (int[] Arr,int Low,int High) is the concrete implementation of the pit fill number, which returns the position of the boundary point                               PUBL IC void Qsort (int[] arr,int Low,int High) is a concrete implementation of the partition method, using recursion
public class QuickSort {/** * @param arr to be sorted array * @param low is just beginning to represent the first element of the sort range. Move slowly to the right and find the first one that is larger than the pivot value Val @param High is just beginning to represent the last element of the sort range. Move slowly to the left and find the first one that is smaller than the pivot value Val @return Returns the location of the split point value/public int partition (int[] Arr,int low,int high) {int val = arr[
			low];//sets the pivot element to be the first element of each sequence while (Low 
3, fast sorting characteristicsTime complexity O (N*LOGN), Space complexity: O (Logn), Stability: unstable

The quick sort is considered to be the best in all sorting methods with the same order of magnitude (average complexity O (N*LOGN)).
Select Pivot element: But if the initial record is basically ordered so that each time the first element is selected as a pivot element, then a "one-sided" situation occurs when the subsequence is divided by a pivot, and the quick sort is completely bubbling, which is the worst case Time Complexity of O (n*n). Therefore, the choice of pivot elements is generally based on the principle of "three to take", that is, the comparison of the first element, the last element, the middle element of the value, take the middle size of the three.


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.