Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Lecture Slides on Average - Fundamental Algorithms | CS 231, Study notes of Algorithms and Programming

Material Type: Notes; Class: Fundamental Algorithms; Subject: Computer Science; University: Wellesley College; Term: Spring 2010;

Typology: Study notes

Pre 2010

Uploaded on 08/19/2009

koofers-user-05q-1
koofers-user-05q-1 🇺🇸

10 documents

1 / 6

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
On Average?
How unbalanced can the partitions become before the
costs go up?
For example, T(n) = T(9n/10) + T(n/10) + n
T(n) = T(n/10) + T(9n/10) + n
.
.
..
.
.
n= n
= (1/10 + 9/10)n
n/10 9n/10
n/100 9n/100 9n/100 81n/100 = (1/100 + 9/100+1/100+81/100)n
81n/1000 729n/1000 n
.
.
..
.
.
1 n
log n
10
log n
9/10
pf3
pf4
pf5

Partial preview of the text

Download Lecture Slides on Average - Fundamental Algorithms | CS 231 and more Study notes Algorithms and Programming in PDF only on Docsity!

On Average?

• How unbalanced can the partitions become before the

costs go up?

• For example, T(n) = T(9n/10) + T(n/10) + n

T(n) = T(n/10) + T(9n/10) + n

..^

n (^) = n n/10 9n/10 = (1/10 + 9/10)n n/100 9n/100 9n/100 (^) 81n/100 = (1/100 + 9/100+1/100+81/100)n 81n/1000 729n/1000 ≤ n

..^

1 ≤ n

log 10 n

log (^) 9/10n

Expected Running Time

Quicksort( A,p,r )

if p < r

then

q <- Partition( A,p,r )

Quicksort( A,p,q-1 )

Quicksort( A,q+1,r )

Q U I Z O R T

Q I O R T Z U

Q I O R T U Z

I O Q R T U Z

I O Q R T U Z

The running time of

Quicksort is dominated by

time spent in Partition.

Partition selects one pivot

element that is never included

in future calls to Quicksort.

Focusing on Partition

Partition( A,p,r)

x ← A[r]

i ← p -

for j ← p to r -

do if A[j] ≤ x

then i ← i+ 1

exchange A[i] ↔ Α [j]

exchange A[i+1] ↔ Α [r]

return i+

Lemma 7.1. The running time of Quicksort is O ( n + X ),

where X is the number of

comparisons performed in

all calls to Partition.

Average Value of X

E[ X ] = E [Σ 1 ≤ i ≤ n -1 Σi+1≤ j ≤ n X ij ]

= Σ 1 ≤ i ≤ n -1 Σi+1≤ j ≤ n E[ Xij ]

= Σ 1 ≤ i ≤ n -1 Σi+1≤ j ≤ n Pr[ zi is compared to zj ]

*Here we use the fact that the average of the sum is the sum of averages.

When are two values not compared?

Q U I Z O R T

Q I O R T Z U

Q I O R T U Z

I O Q R T U Z

I O Q R T U Z

Pivot T is compared

with every element,...

... but after the partition,

no two elements separated

by T are ever compared.

*In general, z i and z j are compared if and only if the first element to be chosen

as a pivot from [z i .. z j ] is either z i or z j.

E[ X ] = Σ 1 ≤i≤ n-1 Σ i+ 1 ≤ j ≤ n 2/( j - i + 1)

Pr[ z i is compared to z j ] = Pr[ z i or z j is 1 st^ pivot

chosen from [ z i.. z j ]]

= Pr[ z i is 1 st^ pivot

chosen from [ z i.. z j ]]

Pr[ zj is 1 st^ pivot

chosen from [ z i.. z j ]]

= 2/( j - i + 1)

Harmonic Numbers: Hk = Σ 1 ≤ k ≤ n 1/ k