24
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

Analysis of Quicksort - University Of Maryland · The expected number of comparisons for Quicksort is ˘2nlnn. NOTE: Could go back and do the Constructive Induction with S(n) anlnn,

  • Upload
    others

  • View
    0

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Analysis of Quicksort - University Of Maryland · The expected number of comparisons for Quicksort is ˘2nlnn. NOTE: Could go back and do the Constructive Induction with S(n) anlnn,

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

Page 2: Analysis of Quicksort - University Of Maryland · The expected number of comparisons for Quicksort is ˘2nlnn. NOTE: Could go back and do the Constructive Induction with S(n) anlnn,

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

Page 3: Analysis of Quicksort - University Of Maryland · The expected number of comparisons for Quicksort is ˘2nlnn. NOTE: Could go back and do the Constructive Induction with S(n) anlnn,

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

Now what?

Page 4: Analysis of Quicksort - University Of Maryland · The expected number of comparisons for Quicksort is ˘2nlnn. NOTE: Could go back and do the Constructive Induction with S(n) anlnn,

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

Now what?

Page 5: Analysis of Quicksort - University Of Maryland · The expected number of comparisons for Quicksort is ˘2nlnn. NOTE: Could go back and do the Constructive Induction with S(n) anlnn,

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

Now what?

Page 6: Analysis of Quicksort - University Of Maryland · The expected number of comparisons for Quicksort is ˘2nlnn. NOTE: Could go back and do the Constructive Induction with S(n) anlnn,

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

Now what?

Page 7: Analysis of Quicksort - University Of Maryland · The expected number of comparisons for Quicksort is ˘2nlnn. NOTE: Could go back and do the Constructive Induction with S(n) anlnn,

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

Now what?

Page 8: Analysis of Quicksort - University Of Maryland · The expected number of comparisons for Quicksort is ˘2nlnn. NOTE: Could go back and do the Constructive Induction with S(n) anlnn,

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

Now what?

Page 9: Analysis of Quicksort - University Of Maryland · The expected number of comparisons for Quicksort is ˘2nlnn. NOTE: Could go back and do the Constructive Induction with S(n) anlnn,

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

Now what?

Page 10: Analysis of Quicksort - University Of Maryland · The expected number of comparisons for Quicksort is ˘2nlnn. NOTE: Could go back and do the Constructive Induction with S(n) anlnn,

Use Constructive Induction.

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

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 11: Analysis of Quicksort - University Of Maryland · The expected number of comparisons for Quicksort is ˘2nlnn. NOTE: Could go back and do the Constructive Induction with S(n) anlnn,

S(n) =2

n

n−1∑q=1

S(q) + n − 1 where we left off

≤ 2

n

n−1∑q=1

aq lg q + n − 1 by IH

≤ 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 expected number of comparisons for Quicksort is ˘2nlnn. NOTE: Could go back and do the Constructive Induction with S(n) anlnn,

S(n) =2

n

n−1∑q=1

S(q) + n − 1 where we left off

≤ 2

n

n−1∑q=1

aq lg q + n − 1 by IH

≤ 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 expected number of comparisons for Quicksort is ˘2nlnn. NOTE: Could go back and do the Constructive Induction with S(n) anlnn,

S(n) =2

n

n−1∑q=1

S(q) + n − 1 where we left off

≤ 2

n

n−1∑q=1

aq lg q + n − 1 by IH

≤ 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 expected number of comparisons for Quicksort is ˘2nlnn. NOTE: Could go back and do the Constructive Induction with S(n) anlnn,

S(n) =2

n

n−1∑q=1

S(q) + n − 1 where we left off

≤ 2

n

n−1∑q=1

aq lg q + n − 1 by IH

≤ 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 expected number of comparisons for Quicksort is ˘2nlnn. NOTE: Could go back and do the Constructive Induction with S(n) anlnn,

S(n) =2

n

n−1∑q=1

S(q) + n − 1 where we left off

≤ 2

n

n−1∑q=1

aq lg q + n − 1 by IH

≤ 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 expected number of comparisons for Quicksort is ˘2nlnn. NOTE: Could go back and do the Constructive Induction with S(n) anlnn,

S(n) =2

n

n−1∑q=1

S(q) + n − 1 where we left off

≤ 2

n

n−1∑q=1

aq lg q + n − 1 by IH

≤ 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 17: Analysis of Quicksort - University Of Maryland · The expected number of comparisons for Quicksort is ˘2nlnn. NOTE: Could go back and do the Constructive Induction with S(n) anlnn,

S(n) =2

n

n−1∑q=1

S(q) + n − 1 where we left off

≤ 2

n

n−1∑q=1

aq lg q + n − 1 by IH

≤ 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 18: Analysis of Quicksort - University Of Maryland · The expected number of comparisons for Quicksort is ˘2nlnn. NOTE: Could go back and do the Constructive Induction with S(n) anlnn,

S(n) =2

n

n−1∑q=1

S(q) + n − 1 where we left off

≤ 2

n

n−1∑q=1

aq lg q + n − 1 by IH

≤ 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 19: Analysis of Quicksort - University Of Maryland · The expected number of comparisons for Quicksort is ˘2nlnn. NOTE: Could go back and do the Constructive Induction with S(n) anlnn,

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 expected number of comparisons for Quicksort is ˘2nlnn. NOTE: Could go back and do the Constructive Induction with S(n) anlnn,

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 21: Analysis of Quicksort - University Of Maryland · The expected number of comparisons for Quicksort is ˘2nlnn. NOTE: Could go back and do the Constructive Induction with S(n) anlnn,

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 22: Analysis of Quicksort - University Of Maryland · The expected number of comparisons for Quicksort is ˘2nlnn. NOTE: Could go back and do the Constructive Induction with S(n) anlnn,

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 23: Analysis of Quicksort - University Of Maryland · The expected number of comparisons for Quicksort is ˘2nlnn. NOTE: Could go back and do the Constructive Induction with S(n) anlnn,

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.

Theorem

The expected number of comparisons for Quicksort is ∼ 2n ln n.

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

Page 24: Analysis of Quicksort - University Of Maryland · The expected number of comparisons for Quicksort is ˘2nlnn. NOTE: Could go back and do the Constructive Induction with S(n) anlnn,

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.

Theorem

The expected number of comparisons for Quicksort is ∼ 2n ln n.

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