Analyzing the Average Case of Quicksort Algorithm, Slides of Computer Science

An in-depth analysis of the quicksort algorithm, focusing on the average case scenario. It covers the worst, best, and average cases of the algorithm, and uses intuition and mathematical calculations to understand the behavior of quicksort. The document also includes a recurrence relation that is solved using the substitution method to determine the average running time of quicksort.

Typology: Slides

2012/2013

Uploaded on 03/23/2013

dhruv
dhruv 🇮🇳

4.3

(12)

194 documents

1 / 27

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Algorithms
Quicksort
Docsity.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b

Partial preview of the text

Download Analyzing the Average Case of Quicksort Algorithm and more Slides Computer Science in PDF only on Docsity!

Algorithms

Quicksort

Review: Analyzing Quicksort

 What will be the worst case for the algorithm?

 Partition is always unbalanced

 What will be the best case for the algorithm?

 Partition is balanced

 Which is more likely?

 The latter, by far, except...

 Will any particular input elicit the worst case?

 Yes: Already-sorted input

Review: Analyzing Quicksort

 In the best case:

T(n) = 2T(n/2) + Θ(n)

 What does this work out to?

T(n) = Θ(n lg n)

Review: Analyzing Quicksort

(Average Case)

 Intuitively, a real-life run of quicksort will

produce a mix of “bad” and “good” splits

 Randomly distributed among the recursion tree

 Pretend for intuition that they alternate between best-case (n/2 : n/2) and worst-case (n-1 : 1)

What happens if we bad-split root node, then good-split the resulting size (n-1) node?

Review: Analyzing Quicksort

(Average Case)

 Intuitively, the O(n) cost of a bad split

(or 2 or 3 bad splits) can be absorbed

into the O(n) cost of each good split

 Thus running time of alternating bad and good

splits is still O(n lg n), with slightly higher

constants

 How can we be more rigorous?

Analyzing Quicksort: Average Case

 For simplicity, assume:

 All inputs distinct (no repeats)

 Slightly different partition() procedure

 partition around a random element, which is not included in subarrays

 all splits (0:n-1, 1:n-2, 2:n-3, … , n-1:0) equally likely

 What is the probability of a particular split

happening?

 Answer: 1/n

Analyzing Quicksort: Average Case

 So…

( ) [ ( ) ( )] ( )

∑^ ( )^ ( )

=

=

1

0

1

0

n

k

n

k

T k n n

T k T n k n n

T n

Write it on the board

Analyzing Quicksort: Average Case

 We can solve this recurrence using the dreaded

substitution method

 Guess the answer

 Assume that the inductive hypothesis holds

 Substitute it in for some value < n

 Prove that it follows for n

Analyzing Quicksort: Average Case

 We can solve this recurrence using the dreaded

substitution method

 Guess the answer

 T(n) = O(n lg n)

 Assume that the inductive hypothesis holds

 Substitute it in for some value < n

 Prove that it follows for n

Analyzing Quicksort: Average Case

 We can solve this recurrence using the dreaded

substitution method

 Guess the answer

 T(n) = O(n lg n)

 Assume that the inductive hypothesis holds

What’s the inductive hypothesis?

 Substitute it in for some value < n

 Prove that it follows for n

Analyzing Quicksort: Average Case

 We can solve this recurrence using the dreaded

substitution method

 Guess the answer

 T( n ) = O( n lg n )

 Assume that the inductive hypothesis holds

 T( n ) ≤ an lg n + b for some constants a and b

 Substitute it in for some value < n

What value?

 Prove that it follows for n

Analyzing Quicksort: Average Case

 We can solve this recurrence using the dreaded

substitution method

 Guess the answer

 T( n ) = O( n lg n )

 Assume that the inductive hypothesis holds

 T( n ) ≤ an lg n + b for some constants a and b

 Substitute it in for some value < n

 The value k in the recurrence

 Prove that it follows for n

Note: leaving the same recurrence as the book

What are we doing here?

Analyzing Quicksort: Average Case

∑^ (^ )^ ( )

=

=

=

=

=

1

1

1

1

1

1

1

0

1

0

lg

lg

lg

lg

n

k

n

k

n

k

n

k

n

k

ak k b n n

n n

b ak k b n

b ak k b n n

ak k b n n

T k n n

T n The recurrence to be solved

What are we doing here?

What are we doing here?

Plug in inductive hypothesis

Expand out the k=0 case

2b/n is just a constant, so fold it into Θ (n)

What are we doing here?

What are we doing here?

Evaluate the summation: b+b+…+b = b (n-1)

The recurrence to be solved

Since n-1<n, 2b(n-1)/n < 2b

Analyzing Quicksort: Average Case

k k b ( ) n

n

a

n n n

b k k n

a

b n n

ak k n

ak k b n n

T n

n

k

n

k

n

k

n

k

n

k

=

=

=

=

=

lg 2

lg

lg

lg

1

1

1

1

1

1

1

1

1

1

Distribute the summation What are we doing here?

This summation gets its own set of slides later