Explaining Proof of Quick-Sort's O(nlog n) Expected Running Time

  • Context:
  • Thread starter Thread starter evinda
  • Start date Start date
  • Tags Tags
    Explanation Proof
Join the discussion
Registration is free. Ask a follow-up in this thread, or start your own.
1 reply · 2K views
evinda
Gold Member
MHB
Messages
3,741
Reaction score
0
Hello! (Wave)

View attachment 4422

View attachment 4423I am trying to understand the proof that the expected running time of quick-sort is $O(n \log n)$.

Could you maybe explain it to me?
 

Attachments

  • worst.PNG
    worst.PNG
    25.6 KB · Views: 142
  • proof1.PNG
    proof1.PNG
    31.5 KB · Views: 131
Physics news on Phys.org
Suppose that we use the following algorithm:

Code:
Quicksort(S):
1.  if S contains at most one element then return S
    else
2.       choose an element a randomly from S;
3.       let S1, S2, and S3 be the sequences of elements in S less than, equal to, and greater than a, respectively;
4.       return Quicksort(S1) followed by S2 followed by Quicksort(S3)
I have tried the following:

Let [tex]T(n)[/tex] be the expected time required by the Quicksort algorithm in order to sort a sequence of [tex]n[/tex] elements. We have that [tex]T(0)=T(1)=b[/tex] for a constant [tex]b[/tex].

We suppose that the elements [tex]a[/tex] is the [tex]i[/tex]-th smallest elements of the [tex]n[/tex] elements of the sequence [tex]S[/tex].
Then the 2 recursive calls of Quicksort have expected time [tex]T(i-1)[/tex] and [tex]T(n-i)[/tex], respectively.
Since the lines 2-3 require time [tex]O(n)[/tex] and the line 1 requires time [tex]O(1)[/tex], we have the following recurrence relation:

[tex]T(n) \leq cn+ \frac{1}{n} \sum_{i=1}^n \left[ T(i-1)+T(n-i)\right], n \geq 2[/tex]

[tex]\\ T(n) \leq cn+ \frac{1}{n} \sum_{i=1}^n \left[ T(i-1)+T(n-i)\right] \\ \leq cn+ \frac{1}{n} \sum_{i=1}^n \left[ 2T(i-1) \right] =cn+ \frac{2}{n} \sum_{i=0}^{n-1} T(i) \\ \Rightarrow T(n) \leq cn+ \frac{2}{n} \sum_{i=0}^{n-1} T(i)(1)[/tex]

We will show that for [tex]n \geq 2[/tex], [tex]T(n) \leq k n \log n[/tex], for [tex]k=2(c+b)[/tex] positive.

For [tex]n=2[/tex]: From the relation (1) we have [tex]T(2) \leq c2+ 2b=2(c+b) \checkmark[/tex]

We suppose that [tex]T(n) \leq k n \log n[/tex] γfor some [tex]n \geq 2[/tex].

We will show that [tex]T(n+1) \leq k (n+1) \log (n+1)[/tex] .

From the relation (1) we have [tex]T(n+1) \leq c(n+1)+ \frac{2}{n+1} \sum_{i=0}^{n} T(i) \leq c(n+1)+ \frac{2}{n+1} (n+1) T(n)= c(n+1)+2T(n) \leq c(n+1)+2 k n \log n \leq (n+1) (c+2k \log n)[/tex]How could we continue? Or should we use strong induction?