Chapter 7 Quick Sort

Source: Internet
Author: User

Quick Sort---the best choice in real-world sequencing applications

The expected time complexity is θ (NLGN) and The worst case complexity is θ (n2) ---- Because of the small implied constant factor and the original order, it is widely used

7.1 Quick Sort Description

Adopting the idea of divided treatment

Decomposition: A[p the array. R] divided into two sub-arrays a[p: Q-1] and A[q+1,r], the basis for division is to make the a[p. Q-1] values are all less than or equal to A[Q],A[Q+1..R] each value is greater than or equal to A[q]

Workaround: Fast Sort by recursive call, A[p for sub-array. Q-1] and A[Q+1..R] to sort

Merging: Because the sub-arrays are in-situ sort, they are ordered without merging

Core idea: Find a "pit" in the array, will be less than equal to the value of the pit to throw the left side of the pit, greater than equal to the value of the pit to throw the right side of the pit, in each recursion to take the same measures (naturally each recursion to choose the pit)

The partitioning of the array (1. R is also within the array, so it is array.count-1 2 when the code is being tested. Where the pit selected is the last element of the array):

        Static intPartition (list<int> Array,intPintr) {intx =Array[r], temp; inti = P1;//similar to an indication, the value on the left side of the indicator is less than x             for(intj=p;j<r;j++)            {                if(array[j]<=x)//encounter smaller than X, put the next one to I go{i++; Temp=Array[i]; Array[i]=Array[j]; ARRAY[J]=temp; }} Array[r]= Array[i +1]; Array[i+1] =x; returni +1; }

Quick sort:

        Static void QuickSort (list<int> array,int p,int  r)        {            int  q;             if(p<r)  // recursive condition                 {= Partition (array, P, R);                 1 );                 1 , R);            }        }

7.2 Performance Analysis

Worst case Scenario ( basic Impossibility ): Each time a sub-problem is divided ( including recursion ) , a sub-problem is generated that contains n-1 and 0 elements, and the time complexity is θ (n2)

The best and the balanced division of the case ( that is, any one of the constant proportions ): The resulting recursive tree depth is θ (LGN), the time cost of each layer is O (n) , so that the algorithm run time is θ ( NLGN)

7.3 Quick-Sort randomized versions (i.e. randomly selected when "pit" is selected)

        Static intRandompartition (list<int> Array,intPintr) {Random rnd=NewRandom (); intPosition = rnd. Next (p, r +1);//randomly produces a p.. The number between r, which represents the subscript for selecting "Pit"            inttemp = Array[r];//swap the selected array[position] with Array[r]ARRAY[R] =Array[position]; Array[position]=temp; returnPartition (Array, p, R); }

--------Practice--------

7.4-1 proof in the recursive T (n) =max0≤q≤n-1 (t (q) +t (n-q-1)) +θ (n), t (n) =ω (n2)

Idea, substituting Law cn2≤max0≤q≤n-1 (T (q) +t (n-q-1)) +θ (n)

by Max0≤q≤n-1 (T (q) +t (n-q-1)) +θ (n) ≥max (q2+ (n-q-1) 2) +θ (n) ≥ (n-q-1) 2+θ (n)

∴t (n) ≥ (n-q-1) 2+θ (n) ≥cn2

--------------------

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