Discrete Mathematical Analysis for fast sorting and Discrete Mathematical Analysis for sorting

Source: Internet
Author: User

Discrete Mathematical Analysis for fast sorting and Discrete Mathematical Analysis for sorting
 

The following is a pseudo-code. To improve efficiency, change the score to randomization. For the original algorithm code, seeAlgorithm-5. Fast sorting

QUICKSORT(A,p,r)1  if p<r2     then q = PARTITION(A,p,r)3         QUICKSORT(A,p,q-1)4         QUICKSORT(A,q+1,r)RANDOMIZED-PARTITION(A,p,r)1  i=RANDOM(p,r)2  exchange A[r]<->A[i]3  return PARTITION(A,p,r)PARTITION(A,p,r)1  x=A[r]2  i=p-13  for j=p to r-14     do if A[j]<=x5         then i=i+16            exchange A[i]<->A[j]7  exchange A[i+1]<->A[r]8  return i+1

 

1. Worst Case Analysis

If every layer in the Quick Sort performs recursive division in the worst case, the running time is n2 ). Intuitively, this is the worst-case running time. The following is a proof.

The replacement method can prove that the running time of the quick sorting is O (n2 ). If T (n) is the worst case time when the process QUICKSORT takes effect on the input with a scale of n, there are:

(Recursive 1)

The parameter q is changed from 0 to n-1, because the PARTITION process generates two subproblems. The total size is n-1. We guess T (n) <= cn2 is true, and c is a constant. This formula is substituted into recursive 1:

Expression q2 + (n-q-1) 2 gets the maximum value on an endpoint of the parameter value range 0 <= q <= n-1, because the formula aboutThe second derivative is positive.(So the original expression is a concave function, and it is the minimum value when q = (n-1)/2 ). In this way, it is bounded (q = 0 or N-1)

T (n:

Because we can select a large enough constant c, so that item c (2n-1) can control limit (n ). Therefore, the run time of the fast sorting (worst case) is forward (n2 ).

2. The expected running time (that is, the average running time)

When QUICKSORT runs on an array containing n elements, the total number of times PARTITION compared in row 4th is X. The running time of QUICKSORT is O (n + X ). Proof:

A total of n times are called Based on the PARTITION. Each call requires a fixed amount of work, and then executes several for loops. In each iteration of the for Loop, 4th rows are executed.

Our goal is to calculate X, that is, the total number of comparisons executed in all calls to PARTITION. We do not want to analyze how many comparisons are made in each PARTITION call, but want to export a field about the total number of comparisons. To achieve this goal, we must understand when the algorithm will compare the two elements in the array and when it will not. For ease of analysis, we rename each element of array A to z1, z2,..., zn, where zi is the smallest element of array. In addition, we also define Zij = {zi, zi + 1,..., zj} as a set of elements between zi and zj (including these two elements.

So when will the algorithms compare zi and zj? To answer this question, we first observe each pair of elements.Comparison at most once. Why? Because each element is only compared with the principal element, and after a PARTITION call ends, the principal element used in this call will no longer be compared with any other element.

We use random indicator variables for our analysis. We define

We need to consider whether there is any comparison in the Algorithm Execution process, rather than a comparison in a loop iteration or a call to PARTITION. Because each pair of elements is compared at most once, we can easily describe the total number of comparisons performed by the algorithm:

Take the expected values on both sides of the preceding formula, and use the linear characteristics of the expected values and the Theorem 1 to obtain the following results:

In the above formula, Pr {zi and zj are compared.} further calculation is required.

In general, once a principal component x meeting the zi <x <zj is selected, we know that zi and zj will no longer be compared. On the other hand, if zi is selected as the principal element before all other elements in Zij, then zi will be compared with all elements except itself in Zij. Similarly, if zi is selected as the principal element before other elements in Zij, zj will be compared with each element except itself in Zij. From this we know that,Zi will be compared with zj. if and only when Zij selects the primary element, the first element is zi or zj.

Now we can calculate the probability of this event. Before an element in Zij is selected as the principal component, the set Zij is all in the same division. Therefore, any element in Zij may be first selected as the principal element. Because the set Zij contains a total of j-I + 1 elements, the probability of selecting any element as the principal component is 1/(j-I + 1 ). Therefore, we have:

The second row in the above formula is valid because the two events involved are mutually exclusive. Combine the equations with the following:

When finding this sum, you can change the variable (k = j-I) and use the bounds of the harmonic series given in equation 1:

Therefore, we can draw a conclusion that Using RANDOMIZED-PARTITION, the desired running time of the quick sorting algorithm is O (nlgn ).

 

Theorem 1:For event A in Sample Space S and S, if XA = I {A}, E [XA] = Pr {}

Equation 1:

Related Article

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.