Analysis of Quicksort - University Of Maryland · The average number of comparisons for quicksort...

21
The average number of comparisons for quicksort is S (n)= 0 if n =0, 1 n q=1 1 n [S (q - 1) + S (n - q )] + n - 1 otherwise

Transcript of Analysis of Quicksort - University Of Maryland · The average number of comparisons for quicksort...

Page 1: Analysis of Quicksort - University Of Maryland · The average number of comparisons for quicksort is S(n) = ˆ 0 ifP n = 0;1 n q=1 1 n [ S( q1) + n)] + 1 otherwise Theorem S(n) anlg

The average number of comparisons for quicksort is

S(n) =

{0 if n = 0, 1∑n

q=11n

[S(q − 1) + S(n − q)] + n − 1 otherwise

Theorem

S(n) ≤ an lg n for some constant a and n ≥ 1.

Proof by Constructive Induction.

Base case: n = 1: S(1) = 0 and a · 1 · lg 1 = 0.

Induction Hypothesis:Assume it holds for all positive integers less than n.So, S(k) ≤ ak lg k for 1 ≤ k ≤ n − 1.

Induction step:

Page 2: Analysis of Quicksort - University Of Maryland · The average number of comparisons for quicksort is S(n) = ˆ 0 ifP n = 0;1 n q=1 1 n [ S( q1) + n)] + 1 otherwise Theorem S(n) anlg

The average number of comparisons for quicksort is

S(n) =

{0 if n = 0, 1∑n

q=11n

[S(q − 1) + S(n − q)] + n − 1 otherwise

Theorem

S(n) ≤ an lg n for some constant a and n ≥ 1.

Proof by Constructive Induction.

Base case: n = 1: S(1) = 0 and a · 1 · lg 1 = 0.

Induction Hypothesis:Assume it holds for all positive integers less than n.So, S(k) ≤ ak lg k for 1 ≤ k ≤ n − 1.

Induction step:

Page 3: Analysis of Quicksort - University Of Maryland · The average number of comparisons for quicksort is S(n) = ˆ 0 ifP n = 0;1 n q=1 1 n [ S( q1) + n)] + 1 otherwise Theorem S(n) anlg

S(n) =n∑

q=1

1

n[S(q − 1) + S(n − q)] + n − 1

=1

n

n∑q=1

[S(q − 1) + S(n − q)] + n − 1

=1

n

n∑q=1

S(q − 1) +1

n

n∑q=1

S(n − q) + n − 1

=1

n

n−1∑q=0

S(q) +1

n

n−1∑q=0

S(q) + n − 1

=2

n

n−1∑q=0

S(q) + n − 1 =2

n

n−1∑q=1

S(q) + n − 1 since S(0) = 0

≤ 2

n

n−1∑q=1

aq lg q + n − 1 by IH

Page 4: Analysis of Quicksort - University Of Maryland · The average number of comparisons for quicksort is S(n) = ˆ 0 ifP n = 0;1 n q=1 1 n [ S( q1) + n)] + 1 otherwise Theorem S(n) anlg

S(n) =n∑

q=1

1

n[S(q − 1) + S(n − q)] + n − 1

=1

n

n∑q=1

[S(q − 1) + S(n − q)] + n − 1

=1

n

n∑q=1

S(q − 1) +1

n

n∑q=1

S(n − q) + n − 1

=1

n

n−1∑q=0

S(q) +1

n

n−1∑q=0

S(q) + n − 1

=2

n

n−1∑q=0

S(q) + n − 1 =2

n

n−1∑q=1

S(q) + n − 1 since S(0) = 0

≤ 2

n

n−1∑q=1

aq lg q + n − 1 by IH

Page 5: Analysis of Quicksort - University Of Maryland · The average number of comparisons for quicksort is S(n) = ˆ 0 ifP n = 0;1 n q=1 1 n [ S( q1) + n)] + 1 otherwise Theorem S(n) anlg

S(n) =n∑

q=1

1

n[S(q − 1) + S(n − q)] + n − 1

=1

n

n∑q=1

[S(q − 1) + S(n − q)] + n − 1

=1

n

n∑q=1

S(q − 1) +1

n

n∑q=1

S(n − q) + n − 1

=1

n

n−1∑q=0

S(q) +1

n

n−1∑q=0

S(q) + n − 1

=2

n

n−1∑q=0

S(q) + n − 1 =2

n

n−1∑q=1

S(q) + n − 1 since S(0) = 0

≤ 2

n

n−1∑q=1

aq lg q + n − 1 by IH

Page 6: Analysis of Quicksort - University Of Maryland · The average number of comparisons for quicksort is S(n) = ˆ 0 ifP n = 0;1 n q=1 1 n [ S( q1) + n)] + 1 otherwise Theorem S(n) anlg

S(n) =n∑

q=1

1

n[S(q − 1) + S(n − q)] + n − 1

=1

n

n∑q=1

[S(q − 1) + S(n − q)] + n − 1

=1

n

n∑q=1

S(q − 1) +1

n

n∑q=1

S(n − q) + n − 1

=1

n

n−1∑q=0

S(q) +1

n

n−1∑q=0

S(q) + n − 1

=2

n

n−1∑q=0

S(q) + n − 1 =2

n

n−1∑q=1

S(q) + n − 1 since S(0) = 0

≤ 2

n

n−1∑q=1

aq lg q + n − 1 by IH

Page 7: Analysis of Quicksort - University Of Maryland · The average number of comparisons for quicksort is S(n) = ˆ 0 ifP n = 0;1 n q=1 1 n [ S( q1) + n)] + 1 otherwise Theorem S(n) anlg

S(n) =n∑

q=1

1

n[S(q − 1) + S(n − q)] + n − 1

=1

n

n∑q=1

[S(q − 1) + S(n − q)] + n − 1

=1

n

n∑q=1

S(q − 1) +1

n

n∑q=1

S(n − q) + n − 1

=1

n

n−1∑q=0

S(q) +1

n

n−1∑q=0

S(q) + n − 1

=2

n

n−1∑q=0

S(q) + n − 1 =2

n

n−1∑q=1

S(q) + n − 1 since S(0) = 0

≤ 2

n

n−1∑q=1

aq lg q + n − 1 by IH

Page 8: Analysis of Quicksort - University Of Maryland · The average number of comparisons for quicksort is S(n) = ˆ 0 ifP n = 0;1 n q=1 1 n [ S( q1) + n)] + 1 otherwise Theorem S(n) anlg

S(n) =n∑

q=1

1

n[S(q − 1) + S(n − q)] + n − 1

=1

n

n∑q=1

[S(q − 1) + S(n − q)] + n − 1

=1

n

n∑q=1

S(q − 1) +1

n

n∑q=1

S(n − q) + n − 1

=1

n

n−1∑q=0

S(q) +1

n

n−1∑q=0

S(q) + n − 1

=2

n

n−1∑q=0

S(q) + n − 1 =2

n

n−1∑q=1

S(q) + n − 1 since S(0) = 0

≤ 2

n

n−1∑q=1

aq lg q + n − 1 by IH

Page 9: Analysis of Quicksort - University Of Maryland · The average number of comparisons for quicksort is S(n) = ˆ 0 ifP n = 0;1 n q=1 1 n [ S( q1) + n)] + 1 otherwise Theorem S(n) anlg

=2a

n

n−1∑q=1

q lg q + n − 1

≤ 2a

n

∫ n

1

x lg xdx + n − 1 by integral bound

=2a

n

[x2 lg x

2− x2 lg e

4

]|n1 +n − 1

=2a

n

[(n2 lg n

2− n2 lg e

4

)−(

12 lg 1

2− 12 lg e

4

)]+ n − 1

= an lg n − an lg e

2+

a lg e

2n+ n − 1

= an lg n +

[1− a lg e

2

]n +

a lg e

2n− 1

≤ an lg n for the induction to hold

Page 10: Analysis of Quicksort - University Of Maryland · The average number of comparisons for quicksort is S(n) = ˆ 0 ifP n = 0;1 n q=1 1 n [ S( q1) + n)] + 1 otherwise Theorem S(n) anlg

=2a

n

n−1∑q=1

q lg q + n − 1

≤ 2a

n

∫ n

1

x lg xdx + n − 1 by integral bound

=2a

n

[x2 lg x

2− x2 lg e

4

]|n1 +n − 1

=2a

n

[(n2 lg n

2− n2 lg e

4

)−(

12 lg 1

2− 12 lg e

4

)]+ n − 1

= an lg n − an lg e

2+

a lg e

2n+ n − 1

= an lg n +

[1− a lg e

2

]n +

a lg e

2n− 1

≤ an lg n for the induction to hold

Page 11: Analysis of Quicksort - University Of Maryland · The average number of comparisons for quicksort is S(n) = ˆ 0 ifP n = 0;1 n q=1 1 n [ S( q1) + n)] + 1 otherwise Theorem S(n) anlg

=2a

n

n−1∑q=1

q lg q + n − 1

≤ 2a

n

∫ n

1

x lg xdx + n − 1 by integral bound

=2a

n

[x2 lg x

2− x2 lg e

4

]|n1 +n − 1

=2a

n

[(n2 lg n

2− n2 lg e

4

)−(

12 lg 1

2− 12 lg e

4

)]+ n − 1

= an lg n − an lg e

2+

a lg e

2n+ n − 1

= an lg n +

[1− a lg e

2

]n +

a lg e

2n− 1

≤ an lg n for the induction to hold

Page 12: Analysis of Quicksort - University Of Maryland · The average number of comparisons for quicksort is S(n) = ˆ 0 ifP n = 0;1 n q=1 1 n [ S( q1) + n)] + 1 otherwise Theorem S(n) anlg

=2a

n

n−1∑q=1

q lg q + n − 1

≤ 2a

n

∫ n

1

x lg xdx + n − 1 by integral bound

=2a

n

[x2 lg x

2− x2 lg e

4

]|n1 +n − 1

=2a

n

[(n2 lg n

2− n2 lg e

4

)−(

12 lg 1

2− 12 lg e

4

)]+ n − 1

= an lg n − an lg e

2+

a lg e

2n+ n − 1

= an lg n +

[1− a lg e

2

]n +

a lg e

2n− 1

≤ an lg n for the induction to hold

Page 13: Analysis of Quicksort - University Of Maryland · The average number of comparisons for quicksort is S(n) = ˆ 0 ifP n = 0;1 n q=1 1 n [ S( q1) + n)] + 1 otherwise Theorem S(n) anlg

=2a

n

n−1∑q=1

q lg q + n − 1

≤ 2a

n

∫ n

1

x lg xdx + n − 1 by integral bound

=2a

n

[x2 lg x

2− x2 lg e

4

]|n1 +n − 1

=2a

n

[(n2 lg n

2− n2 lg e

4

)−(

12 lg 1

2− 12 lg e

4

)]+ n − 1

= an lg n − an lg e

2+

a lg e

2n+ n − 1

= an lg n +

[1− a lg e

2

]n +

a lg e

2n− 1

≤ an lg n for the induction to hold

Page 14: Analysis of Quicksort - University Of Maryland · The average number of comparisons for quicksort is S(n) = ˆ 0 ifP n = 0;1 n q=1 1 n [ S( q1) + n)] + 1 otherwise Theorem S(n) anlg

=2a

n

n−1∑q=1

q lg q + n − 1

≤ 2a

n

∫ n

1

x lg xdx + n − 1 by integral bound

=2a

n

[x2 lg x

2− x2 lg e

4

]|n1 +n − 1

=2a

n

[(n2 lg n

2− n2 lg e

4

)−(

12 lg 1

2− 12 lg e

4

)]+ n − 1

= an lg n − an lg e

2+

a lg e

2n+ n − 1

= an lg n +

[1− a lg e

2

]n +

a lg e

2n− 1

≤ an lg n for the induction to hold

Page 15: Analysis of Quicksort - University Of Maryland · The average number of comparisons for quicksort is S(n) = ˆ 0 ifP n = 0;1 n q=1 1 n [ S( q1) + n)] + 1 otherwise Theorem S(n) anlg

=2a

n

n−1∑q=1

q lg q + n − 1

≤ 2a

n

∫ n

1

x lg xdx + n − 1 by integral bound

=2a

n

[x2 lg x

2− x2 lg e

4

]|n1 +n − 1

=2a

n

[(n2 lg n

2− n2 lg e

4

)−(

12 lg 1

2− 12 lg e

4

)]+ n − 1

= an lg n − an lg e

2+

a lg e

2n+ n − 1

= an lg n +

[1− a lg e

2

]n +

a lg e

2n− 1

≤ an lg n for the induction to hold

Page 16: Analysis of Quicksort - University Of Maryland · The average number of comparisons for quicksort is S(n) = ˆ 0 ifP n = 0;1 n q=1 1 n [ S( q1) + n)] + 1 otherwise Theorem S(n) anlg

Need

an lg n +

[1− a lg e

2

]n +

a lg e

2n− 1 ≤ an lg n

Need

1− a lg e

2≤ 0 =⇒ a ≥ 2

lg e

Set a = 2lg e≈ 1.39

Need

a lg e

2n− 1 ≤ 0 ⇐⇒ 2 lg e

(lg e)2n− 1 ≤ 0 ⇐⇒ 1

n− 1 ≤ 0

which always holds since n ≥ 1.

So,S(n) ≈ 1.39n lg n

Page 17: Analysis of Quicksort - University Of Maryland · The average number of comparisons for quicksort is S(n) = ˆ 0 ifP n = 0;1 n q=1 1 n [ S( q1) + n)] + 1 otherwise Theorem S(n) anlg

Need

an lg n +

[1− a lg e

2

]n +

a lg e

2n− 1 ≤ an lg n

Need

1− a lg e

2≤ 0 =⇒ a ≥ 2

lg e

Set a = 2lg e≈ 1.39

Need

a lg e

2n− 1 ≤ 0 ⇐⇒ 2 lg e

(lg e)2n− 1 ≤ 0 ⇐⇒ 1

n− 1 ≤ 0

which always holds since n ≥ 1.

So,S(n) ≈ 1.39n lg n

Page 18: Analysis of Quicksort - University Of Maryland · The average number of comparisons for quicksort is S(n) = ˆ 0 ifP n = 0;1 n q=1 1 n [ S( q1) + n)] + 1 otherwise Theorem S(n) anlg

Need

an lg n +

[1− a lg e

2

]n +

a lg e

2n− 1 ≤ an lg n

Need

1− a lg e

2≤ 0 =⇒ a ≥ 2

lg e

Set a = 2lg e≈ 1.39

Need

a lg e

2n− 1 ≤ 0 ⇐⇒ 2 lg e

(lg e)2n− 1 ≤ 0 ⇐⇒ 1

n− 1 ≤ 0

which always holds since n ≥ 1.

So,S(n) ≈ 1.39n lg n

Page 19: Analysis of Quicksort - University Of Maryland · The average number of comparisons for quicksort is S(n) = ˆ 0 ifP n = 0;1 n q=1 1 n [ S( q1) + n)] + 1 otherwise Theorem S(n) anlg

Need

an lg n +

[1− a lg e

2

]n +

a lg e

2n− 1 ≤ an lg n

Need

1− a lg e

2≤ 0 =⇒ a ≥ 2

lg e

Set a = 2lg e≈ 1.39

Need

a lg e

2n− 1 ≤ 0 ⇐⇒ 2 lg e

(lg e)2n− 1 ≤ 0 ⇐⇒ 1

n− 1 ≤ 0

which always holds since n ≥ 1.

So,S(n) ≈ 1.39n lg n

Page 20: Analysis of Quicksort - University Of Maryland · The average number of comparisons for quicksort is S(n) = ˆ 0 ifP n = 0;1 n q=1 1 n [ S( q1) + n)] + 1 otherwise Theorem S(n) anlg

Now that we are finished, we realize that

S(n) ≤ an lg n =2n lg n

lg e= 2n ln n

which is a more natural formula.

Could go back and do the Constructive Induction with S(n) ≤ an ln n,which would simplify the algebra.

Page 21: Analysis of Quicksort - University Of Maryland · The average number of comparisons for quicksort is S(n) = ˆ 0 ifP n = 0;1 n q=1 1 n [ S( q1) + n)] + 1 otherwise Theorem S(n) anlg

Now that we are finished, we realize that

S(n) ≤ an lg n =2n lg n

lg e= 2n ln n

which is a more natural formula.

Could go back and do the Constructive Induction with S(n) ≤ an ln n,which would simplify the algebra.