23.11.2014 Views

Data Structures and Algorithms in Java[1].pdf - Fulvio Frisone

Data Structures and Algorithms in Java[1].pdf - Fulvio Frisone

Data Structures and Algorithms in Java[1].pdf - Fulvio Frisone

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

is the same as the expected number of times we must flip a fair co<strong>in</strong> before it comes<br />

up "heads." That is, E(g(n)) = 2. Thus, if we let T(n) be shorth<strong>and</strong> for E(t(n)), then<br />

we can write the case for n > 1 as<br />

T(n)≤T(3n/4) + 2bn.<br />

To convert this relation <strong>in</strong>to a closed form, let us iteratively apply this <strong>in</strong>equality<br />

assum<strong>in</strong>g n is large. So, for example, after two applications,<br />

T(n) ≤T((3/4) 2 n) + 2b(3/4)n + 2bn.<br />

At this po<strong>in</strong>t, we should see that the general case is<br />

In other words, the expected runn<strong>in</strong>g time is at most 2bn times a geometric sum<br />

whose base is a positive number less than 1. Thus, by Proposition 4.5, T(n) is O(n).<br />

Proposition 11.11: The expected runn<strong>in</strong>g time of r<strong>and</strong>omized quick-select<br />

on a sequence S of size n is O(n), assum<strong>in</strong>g two elements of S can be compared <strong>in</strong><br />

O(1) time.<br />

11.8 Exercises<br />

For source code <strong>and</strong> help with exercises, please visit<br />

java.datastructures.net.<br />

Re<strong>in</strong>forcement<br />

R-11.1<br />

Suppose S is a list of n bits, that is, n 0's <strong>and</strong> 1's. How long will it take to sort S<br />

with the merge-sort algorithm? What about quick-sort?<br />

R-11.2<br />

Suppose S is a list of n bits, that is, n 0's <strong>and</strong> 1's. How long will it take to sort S<br />

stably with the bucket-sort algorithm?<br />

R-11.3<br />

Give a complete justification of Proposition 11.1.<br />

R-11.4<br />

729

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!