Quick Sort Summary

Source: Internet
Author: User

Fast sorting, as its name is, is the fastest sort on average, in the case of 2LN2 (Nlogn), about 1.39nlogn, in the same complexity as O (NLOGN) sorting algorithm, is the least constant coefficient algorithm, the worst case can be as high as O (n^2), However, the probability of worst-case occurrence can be greatly reduced by using the improved stochastic fast-scheduling algorithm. Not only that, the fast sort is also an algorithm that can run in-place, and its operation requires only O (1). In practical use, the performance of fast sorting is much better than other algorithms such as merge sort, so the fast sorting is the most used sorting algorithm in industry so far.

The quick sort uses the idea of divide and conquer, the whole sorting problem is decomposed for several sub-problems, in each sub-problem, we select a pivot point (usually the first element), after a processing, we should ensure that the left element of the pivot point is always not greater than the pivot point, while ensuring that the right element is not less than the pivot point , it is obvious that the pivot point has been correctly placed in its correct position after a single processing, so the problem is decomposed to sort the elements on the left side of the pivot point and to sort the elements on the right of the pivot point. Then we use recursive thinking to deal with the remaining sub-problems.

In the worst case scenario, where the element is properly seated or has been reversed in place, each partitioning problem will only have a sub-problem of size 0 and a sub-problem of scale n-1. So the time complexity of the whole problem presents a arithmetic progression, i.e. O (n^2). To avoid this situation, we can use randomization to select the pivot point strategy before each processing. Specifically, the element in question is randomly selected to swap position with the first element before each pivot point is selected, and then the first element is selected as the pivot point.

Specifically for the code, we can define two pointers to the currently operating elements low and high, let low point to the first element, let high point to the last element, and then take the value of the first element out of the backup as the pivot point, so the position of the first element is empty, Then, from the last element to start the comparison, find the first element that does not satisfy the right element of the pivot point attribute, that is, the element is strictly smaller than the axis point, assign it to the location of the axis point, so that the position is empty, and then from the location of the axis point back to compare, find strictly greater than the axis point of the and assigns it to the currently vacated position. In this cycle, until the two elements of the current operation are equal, the element on the left side of the pointer low must not be greater than the pivot point, the element to the right of the pointer is necessarily not less than the pivot point, and since the two element pointers are equal, the position to which it points is bound to be empty. At this point only need to assign a backup of the pivot point to this position to complete a partition algorithm, and then as long as the end of the recursive solution to the remaining problems, and the recursive base, that is, the element pointer is the case of an element, added to the beginning of the algorithm, the completion of our fast sorting algorithm.

voidQuickSort (intAintLowintHigh ) {    if(High <=Low )return; intFirst =Low ; intLast =High ; intKey =A[first];  while(Low <High ) {         while(Low < High&&key <=A[high]) high--; A[low]=A[high];  while(Low < High&&a[low] <=key) Low++; A[high]=A[low]; } A[high]=key; Pivot point in place QuickSort (A, first, high-1);//using tail recursive processing sub-problemQuickSort (A, high +1, last);}

Quick Sort Summary

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.