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.

Partition the set S <strong>in</strong>to …n/5… groups of size 5 each (except possibly for one<br />

group). Sort each little set <strong>and</strong> identify the median element <strong>in</strong> this set. From this<br />

set of …n/5… "baby" medians, apply the selection algorithm recursively to f<strong>in</strong>d<br />

the median of the baby medians. Use this element as the pivot <strong>and</strong> proceed as <strong>in</strong><br />

the quick-select algorithm.<br />

Show that this determ<strong>in</strong>istic method runs <strong>in</strong> O(n) time by answer<strong>in</strong>g the<br />

follow<strong>in</strong>g questions (please ignore floor <strong>and</strong> ceil<strong>in</strong>g functions if that simplifies<br />

the mathematics, for the asymptotics are the same either way):<br />

a.<br />

How many baby medians are less than or equal to the chosen pivot? How<br />

many are greater than or equal to the pivot?<br />

b.<br />

For each baby median less than or equal to the pivot, how many other<br />

elements are less than or equal to the pivot? Is the same true for those<br />

greater than or equal to the pivot?<br />

c.<br />

Argue why the method for f<strong>in</strong>d<strong>in</strong>g the determ<strong>in</strong>istic pivot <strong>and</strong> us<strong>in</strong>g it to<br />

partition S takes O(n) time.<br />

d.<br />

Based on these estimates, write a recurrence equation to bound the worstcase<br />

runn<strong>in</strong>g time t(n) for this selection algorithm (note that <strong>in</strong> the worst<br />

case there are two recursive calls—one to f<strong>in</strong>d the median of the baby<br />

medians <strong>and</strong> one to recur on the larger of L <strong>and</strong> G).<br />

e.<br />

Projects<br />

P-11.1<br />

Us<strong>in</strong>g this recurrence equation, show by <strong>in</strong>duction that t(n) is O(n).<br />

Experimentally compare the performance of <strong>in</strong>-place quick-sort <strong>and</strong> a version of<br />

quick-sort that is not <strong>in</strong>-place.<br />

P-11.2<br />

738

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

Saved successfully!

Ooh no, something went wrong!