0% found this document useful (0 votes)
73 views3 pages

Average Case Analysis of Quick Sort

The document analyzes the average case runtime of quicksort. It proves that quicksort runs in O(n log n) time on average. The proof works by modeling the runtime as a recurrence relation, then applying techniques to solve the relation. This yields that the average runtime T(n) is proportional to n log n, meaning quicksort has linearithmic average case performance.

Uploaded by

Hitech Ga
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
73 views3 pages

Average Case Analysis of Quick Sort

The document analyzes the average case runtime of quicksort. It proves that quicksort runs in O(n log n) time on average. The proof works by modeling the runtime as a recurrence relation, then applying techniques to solve the relation. This yields that the average runtime T(n) is proportional to n log n, meaning quicksort has linearithmic average case performance.

Uploaded by

Hitech Ga
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 3

5.

 Average Case Analysis of Quick Sort https://secweb.cs.odu.edu/~zeil/cs361/web/webs...

5. Average Case Analysis of Quick Sort

5. Average Case Analysis of Quick Sort


The average case run time of Quick sort is 𝑂(𝑛 log 𝑛). Proving this is really a
bit much for this course, but we present the proof here for the curious:

Assume a strategy for choosing pivots such that, after partitioning A into
A1 and A2, all lengths of A1 from 0 to | 𝐴| − 1 are equally likely.

The running time of quickSort equals the time of its two recursive calls
plus time to do the partition.

pivotIndex is 𝑂(𝑙𝑎𝑠𝑡 − 𝑓 𝑖𝑟𝑠𝑡).

Suppose that we partition 𝑛 elements into sub-arrays of length 𝑖 and


(𝑛 − 𝑖).

Time 𝑇 to sort the 𝑛 elements is then:

𝑇 (𝑛) = 𝑇 (𝑖) + 𝑇 (𝑛 − 𝑖) + 𝑐 ⋅ 𝑛

This kind of formula is called a recurrence relation. They are very


common in describing the performance of recursive routines.

Because 𝑖 is equally likely to be any value from 0 to 𝑛 − 1, the average


(expected) value of 𝑇 (𝑖) is

1𝑛 − 1 𝑇 𝑗
𝐸(𝑇 (𝑖)) = 𝑛 � ( )
𝑗 =0

Since 𝑛 − 𝑖 can take on the same values as 𝑖, and all such values are equally
likely,

𝐸(𝑇 (𝑛 − 𝑖)) = 𝐸(𝑇 (𝑖))

On average, then

2 ⎛𝑛 − 1 𝑇 𝑗 ⎞ + 𝑐 ⋅ 𝑛
𝑇 (𝑛) = 𝑛 ⎜⎜ � ( )⎟⎟
⎝𝑗 =0 ⎠

Multiply through by 𝑛:

1 of 3 02/12/19, 11:36 am
5. Average Case Analysis of Quick Sort https://secweb.cs.odu.edu/~zeil/cs361/web/webs...

⎛𝑛 − 1 ⎞ 2
𝑛 ⋅ 𝑇 (𝑛) = 2 ⋅ ⎜ � 𝑇 ( 𝑗)⎟ + 𝑐 ⋅ 𝑛
⎜⎝ ⎟⎠
𝑗 =0

𝑛 is just a variable, so this must be true no matter what value we plug in for
it. Try replacing 𝑛 by 𝑛 − 1.

⎛𝑛 − 2 ⎞ 2
(𝑛 − 1) ⋅ 𝑇 (𝑛 − 1) = 2 ⋅ ⎜⎜ � 𝑇 ( 𝑗)⎟⎟ + 𝑐 ⋅ (𝑛 − 1)
⎝𝑗 =0 ⎠

So now both of these are true:

𝑛 −1
= 2 ⋅ ⎛ ∑ 𝑗 = 0 𝑇 ( 𝑗) ⎞ + 𝑐 ⋅ 𝑛
2
𝑛 ⋅ 𝑇 (𝑛)
⎝⎜ ⎟

𝑛 −2 2
( 𝑛 − 1) ⋅ 𝑇 ( 𝑛 − 1) = 2 ⋅ ⎛ ∑ 𝑗 = 0 𝑇 ( 𝑗) ⎞ + 𝑐 ⋅ ( 𝑛 − 1)
⎝⎜ ⎟

Subtract the 2nd equation from the first:

𝑛𝑇 (𝑛) − (𝑛 − 1)𝑇 (𝑛 − 1) = 2𝑇 (𝑛 − 1) + 2𝑐𝑛 − 𝑐

Collect the 𝑇 (𝑛 − 1) terms together and drop the −𝑐 term:

𝑛𝑇 (𝑛) = (𝑛 + 1)𝑇 (𝑛 − 1) + 2𝑐𝑛

Next we apply a standard technique for solving recurrence relations. Divide


by 𝑛(𝑛 + 1) and "telescope":

𝑇 (𝑛 ) 𝑇 (𝑛 − 1 )
𝑛+1 = 𝑛 + 𝑛2𝑐
+1

𝑇 (𝑛 − 1 ) 𝑇 (𝑛 − 2 )
𝑛 = 𝑛−1 + 2𝑐
𝑛

𝑇 (𝑛 − 2 ) 𝑇 (𝑛 − 3 )
𝑛−1 = 𝑛−2 + 𝑛2𝑐
−1

Note that most of the terms on the left will have appeared on the right in the
previous equation, so if we were to add up all these equations, these terms
would appear on both sides and could be dropped:

𝑇 (𝑛) 𝑇 ( 1) 𝑛 +1
= + 2𝑐 � 1
𝑛+1 2 𝑗 =3
𝑗

𝑛 +1 1
As 𝑛 gets very large, � approaches ln (𝑛) + 𝛾, where 𝛾 is Euler's
𝑗 =3 𝑗
constant, 0.577...

2 of 3 02/12/19, 11:36 am
5. Average Case Analysis of Quick Sort https://secweb.cs.odu.edu/~zeil/cs361/web/webs...

Hence

𝑇 (𝑛) 𝑇 ( 1)
= + 2𝑐 ⋅ ln (𝑛) + 2𝑐𝛾 = ln (𝑛) + 𝑐2 = 𝑂( ln (𝑛))
𝑛+1 2

and so

𝑇 (𝑛) = 𝑂(𝑛 ⋅ log (𝑛))

4. Optimizing the Algorithm 


 | 

In the Forum:

(no threads at this time)


Discuss This Page

3 of 3 02/12/19, 11:36 am

You might also like