The way to regain the algorithm--quick sort

Source: Internet
Author: User

*************************************** Reprint Please specify the Source: Http://blog.csdn.net/lttree ********************************************



Chapter One: Division and recursion


Quick Sort


Algorithm Description:

Just like a merge sort, it's sort of a bunch of data.

The principle of the fast-line is to find a standard that is centered on this standard to expand. Like before the exercise, the teacher will say, to XXX as the benchmark, into the gymnastics formation scattered, at this time, xxx about the people will be to it on par, and outward diffusion. The principle of fast-running is similar, the most primitive is to get the first element as the standard, and then smaller than this element on its left, large on the right.

Note that the first element here is the leftmost element in the parameter passed in, not the first element of the entire array.

In general, it is Three steps :

<1>Divide (decomposition): an element within an array a[m] is a datum, dividing the entire array a[0:n-1] into three parts: all elements within the array that are less than a[m] are set a[0:m-1],a[m], and all of the arrays are greater than a[m] The collection of elements of a[m+1,n-1].

<2> Conquer (Recursive solver): Sort a[l:m-1] and a[m+1,r by recursive call to fast sort algorithm

<3> Merge: Because the sort for a[l:m-1] and A[m+1,r] is in-place, you do not need to perform any other calculations to have the merge complete.


Program code:

Template <class type>void Swap (type& a,type& b) {    Type temp = A;    A = b;    b = temp;} Template <class type>int Partition (Type arr[], int l, int r) {//    the initial data given is such as ARR has 6 numbers, L is 0,r to 5,    //is the following table for the array The position of the 0~5 (total of 6 elements) is sorted by    int i = l, j = r+1;    Type temp = arr[l];    while (true)    {        //from go after, less than the number of first element (temp) subscript while        (Arr[++i] < temp && I < R);        From the back forward, the number of subscripts is greater than the first element (temp)        while (Arr[--j] > Temp);        if (i >= j) break    ;        Swap        (Arr[i],arr[j]) of two subscript elements;    }    Finally, the number of the first element is placed in the middle    arr[l] = arr[j];    ARR[J] = temp;    return J;} Template <class type>void QuickSort (Type arr[], int l, int r) {    if (L < r) {        int m = Partition (arr, L,R);        QuickSort (arr,l,m-1);        QuickSort (arr,m+1,r);    }}


Algorithm Analysis:

The running time of the fast row is related to whether the partition is symmetrical, and the worst case occurs when the two regions of the partitioning process contain 1 and n-1 elements respectively. Since the function partition function is calculated at O (n), it is assumed that each step of the partition function is the most asymmetric division, then the complexity of the computational time t (N) is:

T (n)= O (1) when n≤1

=T (n-1) +o (n) when n>1

Solution to this recursive equation can be obtained T (n) = O (n^2)

But if, at best, each partition is divided evenly, then the computational time complexity T (N) is:

T (n)= O (1) when n≤1

=2*t (N/2) +o (n) when n>1

Solution to this recursive equation can be obtained t (n) = O (Nlog (n))

It is also proved that the average complexity is O (Nlog (n)), which is fast in the comparison-based sorting algorithm, hence the name is quickly sorted.


Algorithm optimization:

For the optimization of fast sequencing, it is the optimization of the benchmark. As can be seen in the algorithm analysis, a good benchmark is very important, so its optimization lies in this.

In the above function, use a random to get a subscript position instead of the value of the first element.





*************************************** Reprint Please specify the Source: Http://blog.csdn.net/lttree ********************************************


The way to regain the 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.