Quick Sort)

Source: Internet
Author: User
Tags sorts

1. algorithm ideas
Quick sorting is A sort by Division and exchange proposed by C. R. A. Hoare in 1962. It adopts a sub-Governance Policy, usually called Divide-and-ConquerMethod ).

(1) Basic Idea of divide and conquer Law
The basic idea of the division and control law is to break down the original problem into several subproblems with smaller sizes but similar structures as the original problem. Recursively solve these subproblems, and then combine the solutions of these subproblems into the solutions of the original problem.

(2) Basic Idea of fast sorting
Set the unordered area to R [low .. high], and describe the basic idea of fast sorting using the division and control method as follows:
① Decomposition:
In R [low .. select a record in high] as the benchmark to divide the current disordered zoning into two smaller subintervals (left and right) R [low .. pivotpos-1) and R [pivotpos + 1 .. high], and make the keywords of all records in the left subinterval less than or equal to the benchmark record (may be recorded as the benchmark) keyword. key, the key of all records in the subinterval on the right is greater than or equal to limit. the benchmark record is located at the correct position (pivotpos), and does not need to be sorted in the future.
Note:
The key to division is to determine the location of the benchmark record, TPOs. The division result can be simply expressed as (note that partition = R [pivotpos]):
R [low... pivotpos-1]. keys ≤ R [small TPOs]. key ≤ R [small TPOs + 1 .. high]. keys
Here, low ≤ TPOs ≤ high.
② Solution:
Use recursive call to quickly sort the Left and Right subintervals R [low... pivotpos-1] and R [small TPOs + 1 .. high.
③ Combination:
Because when the two recursive calls in the "solving" Step end, the left and right subintervals are ordered. For quick sorting, the "Combination" step does not need to be done, and can be considered as a null operation.

2. Quick Sorting Algorithm QuickSort
Void QuickSort (SeqList R, int low, int high)
{// Fast sorting of R [low... high]
Int pivotpos; // the location of the benchmark record after Division
If (low Extends TPOs = Partition (R, low, high); // divide R [low... high]
QuickSort (R, low, pivotpos-1); // recursively sorts the left Interval
QuickSort (R, pivotpos + 1, high); // recursively sorts the right range
}
} // QuickSort

Note:
To sort the entire file, you only need to call QuickSort (R, 1, n) to sort the R [l. n.

3. Partition Algorithm
(1) Simple Partitioning Method
① Specific practices
Step 1: (initialize) set two pointers, I and j. Their initial values are the lower and upper bounds of the interval, that is, I = low and I = high; select the first record R [I] (R [low]) in the unordered zone as the benchmark record and save it in the variable comment;
Step 2: Make j scan left from high until 1st keywords are found less than limit. key record R [j], move R [j]) to the position indicated by I, which is equivalent to R [j] and reference R [I] (that is, benchmark) the keyword is exchanged so that the keyword is smaller than the benchmark keyword. the key record is moved to the left of the benchmark. After the switch, the record in R [j] is equivalent to the cursor. Then, the I pointer starts scanning right from the position I + 1, until 1st keywords are found to be greater than limit. key record R [I], moving R [I] to the position indicated by I, which is equivalent to exchanging R [I] and reference R [j], move the record whose keyword is greater than the benchmark keyword to the right of the benchmark, after the exchange, R [I] is equivalent to storing the cursor; then let the pointer j from position J-1 start to left scan, in this way, the scanning direction is changed alternately, and the two ends move closer to the center until I = j, I is the final position of the benchmark worker, where the worker is placed to complete a division.

Division algorithm:
Int Partition (SeqList R, int I, int j)
{// When calling Partition (R, low, high), divide R [low... high,
// Return the location of the benchmark record
ReceType benchmark = R [I]; // use the first record of the interval as the benchmark'
While (I <j) {// scanning from both ends of the interval to the middle until I = j
While (I <j & R [j]. key> = keys. key) // equals
J --; // scan from the right to the left to find records whose 1st keywords are smaller than keyword. key. R [j]
If (I <j) // indicates the keyword <keyword. key
R [I ++] = R [j]; // equivalent to switching R [I] and R [j]. After switching, the I pointer adds 1
While (I <j & R [I]. key <= keys. key) // equals
I ++; // scan from left to right to find records with 1st keywords greater than keywords. key R [I]
If (I <j) // indicates that R [I] is found, so that R [I]. key> keys. key
R [j --] = R [I]; // equivalent to switching R [I] and R [j]. After switching, the j pointer is reduced by 1.
} // Endwhile
R [I] = benchmark; // the benchmark record has been located.
Return I;
} // Partition

4. Algorithm Analysis
The time of quick sorting is mainly used in partitioning operations, and the interval of k length is divided. A comparison of K-1 times keyword is required.

(1) Worst time complexity
The worst case is that the benchmark selected by each division is the record with the smallest (or largest) keyword in the currently unordered area, the division result is that the subinterval on the left of the benchmark is null (or the subinterval on the right is empty), and the number of records in the subinterval obtained from the Division is not empty, only one fewer record count than the unordered partition before division.
Therefore, there must be n-1 division for quick sorting. the start time of division I is the time zone length n-I + 1, the number of comparisons required is n-I (1 ≤ I ≤ N-1), so the total number of comparisons reaches the maximum value:
Cmax = n (n-1)/2 = O (n2)
If, based on the partitioning algorithm given above, the first records in the current unordered area are taken as the benchmark each time, when the records of the file are arranged in ascending order (or descending order, the benchmark obtained by each division is the record with the smallest (or largest) keyword in the unordered area, and the comparison times required for quick sorting are the most.

(2) The best time complexity
In the best case, the benchmark obtained for each partition is the "median" Record of the current unordered partition. The result is that the length of the left and right unordered subintervals of the benchmark is roughly equal. Total number of keyword comparisons:
0 (nlgn)
Note:
It is easier to analyze the comparison times with a recursive tree. Because the Left and Right subintervals are roughly the same after each division, the height of the recursive tree is O (lgn ), the sum of the number of key word comparisons required for the Division of nodes at each layer of the recursive tree does not exceed n. Therefore, the total number of keywords required for the entire sorting process is compared. C (n) = O (nlgn ).
Because the number of records moved by quick sorting is not greater than the number of comparisons, the worst time complexity of quick sorting is 0 (n2), and the best time complexity is O (nlgn ).

(3) Selection of benchmark keywords
Selecting the benchmark keyword of the partition in the unordered area is the key to determining the algorithm performance.
  ① Rule of "getting three"
The "Three get in progress" rule compares the keywords at the beginning, end, and center of the interval in the current interval, and takes the records corresponding to the values of the three as the benchmark, before the Division starts, the benchmark record is exchanged with the 1st records of the region. The subsequent division process is identical with the preceding Partition algorithm.

  
② Obtain the random number k (low ≤ k ≤ high) between low and high, and use R [k] as the reference.
The best way to select a benchmark is to use a random function to generate a random number k (low ≤ k ≤ high) between low and high, and use R [k] as the benchmark, this is equivalent to forcing R [low .. records in high] are randomly distributed. The quick sorting obtained by this method is generally called random quick sorting. Specific algorithms [see tutorial materials]
Note:
The fast sorting of randomization is slightly different from the general quick sorting algorithm. However, after randomization, the performance of the algorithm is greatly improved, especially for initial ordered files, it is generally impossible to cause the worst case. Randomization is not only applicable to fast sorting, but also to other algorithms that require random distribution of data.

(4) Average time complexity
Although the worst time for fast sorting is O (n2), it is the fastest in the internal Sorting Algorithm Based on keyword comparison in terms of average performance. Its average time complexity is O (nlgn ).

(5) spatial complexity
Quick sorting requires a stack inside the system to implement recursion. If each division is uniform, the height of the recursive tree is O (lgn). Therefore, the stack space is O (lgn) after recursion ). In the worst case, the height of the recursive tree is O (n), and the required stack space is O (n ).

(6) Stability
Fast sorting is unstable, for example, [2,2, 1].

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.