Sorting algorithm--Quick sort

Source: Internet
Author: User

Sorting algorithm--Quick sort

Today the introduction of fast sorting, which is the most commonly used in the actual sorting algorithm, fast, high efficiency. Like a name, fast sorting is the best sort of algorithm.

Thought

The idea of quick sorting is the thought of division and treatment.

A quick sort is to find an element (which can theoretically be arbitrarily found) as a datum (pivot), and then partition the array so that the values of the elements on the left of the datum are not larger than the datum, and the element values to the right of the datum are not less than the datum values, so that the elements of the datum are adjusted to the correct position after Recursive quick sorting, and other n-1 elements are also adjusted to the correct position after sorting. Finally, each element is in the correct position after sorting, and the sort is complete. So the core algorithm of the fast sorting algorithm is the partitioning operation, that is, how to adjust the position of the datum and adjust the final position of the return datum to divide and conquer the recursion.

For example, this may not be too good to understand. Suppose that the order to sort is listed as

2 2 4 9 3 6 7 1 5 First use 2 as the benchmark, using the I-j two pointers are scanned from both sides, separating the elements smaller than 2 and the elements larger than 2. First compare 2 and 5, 5:2, J shift left

2 2 4 9 3 6 7 1 5 compare 2 and less than 2, so put 1 in the position of 2

2 1 4 9 3 6 7 1 5 Compare 2 and bis more than 2, so move 4 to the back

2 1 4 9 3 6 7 4 5 Compare 2 and 7,2 and 6,2 and 3,2 and 9, all greater than 2, meet the conditions, and therefore unchanged

After the first round of quick sorting, the elements change to look like this

[1] 2 [4 9 3 6 7 5]

After that, the 2 left side of the element is fast, because there is only one element, so the end of the fast line. The right side of the queue, recursive, and eventually produce the final result.

Code

 int quicksort (Vector<int> &v, int left, int right) {
if (left < right) {
int key = V[left];
int low = left;
int high = right;
while (Low < High) {
while (Low < High & & V[high] > key) {
high--;
}
V[low] = V[high];
while (Low < high && V[low] < key) {
low++;
}
V[high] = V[low];
}
V[low] = key,
quicksort (V,left,low-1);
Quicksort (V,low+1,right);
}
}


Analysis

The time of fast sorting is mainly spent on the division operation, the interval of length k is divided, the total need k-1 the comparison of the key words.

The worst-case scenario is that each time the selected datum is the smallest (or largest) record of the keyword in the current unordered region, the result is that the sub-interval to the left of the Datum is empty (or the right sub-interval is empty), and the number of records in another non-empty sub-interval is divided by only one less than the number of records Time Complexity of O (n*n)

In the best case, the datum for each partition is the "median" record of the current unordered region, and the result is that the length of the two unordered sub-ranges of the datum is roughly equal to the left and right. Total number of keyword comparisons: O (NLGN)

Although the worst time for fast sorting is O (N2), in terms of average performance, it is the fastest in the internal sorting algorithm based on the keyword comparison, and hence the name of the fast sort. Its average time complexity is O (NLGN).

Sorting 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.