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.

This may come as a small surprise, but we can <strong>in</strong>deed solve the selection problem<br />

<strong>in</strong> O(n) time for any value of k. Moreover, the technique we use to achieve this<br />

result <strong>in</strong>volves an <strong>in</strong>terest<strong>in</strong>g algorithmic design pattern. This design pattern is<br />

known as prune-<strong>and</strong>-search or decrease-<strong>and</strong>-conquer. In apply<strong>in</strong>g this design<br />

pattern, we solve a given problem that is def<strong>in</strong>ed on a collection of n objects by<br />

prun<strong>in</strong>g away a fraction of the n objects <strong>and</strong> recursively solv<strong>in</strong>g the smaller<br />

problem. When we have f<strong>in</strong>ally reduced the problem to one def<strong>in</strong>ed on a constantsized<br />

collection of objects, then we solve the problem us<strong>in</strong>g some brute-force<br />

method. Return<strong>in</strong>g back from all the recursive calls completes the construction. In<br />

some cases, we can avoid us<strong>in</strong>g recursion, <strong>in</strong> which case we simply iterate the<br />

prune-<strong>and</strong>-search reduction step until we can apply a brute-force method <strong>and</strong> stop.<br />

Incidentally, the b<strong>in</strong>ary search method described <strong>in</strong> Section 9.3.3 is an example of<br />

the prune-<strong>and</strong>search design pattern.<br />

11.7.2 R<strong>and</strong>omized Quick-Select<br />

Code Fragment 11.11: R<strong>and</strong>omized quick-select<br />

algorithm.<br />

In apply<strong>in</strong>g the prune-<strong>and</strong>-search pattern to the selection problem, we can design a<br />

simple <strong>and</strong> practical method, called r<strong>and</strong>omized quick-select, for f<strong>in</strong>d<strong>in</strong>g the kth<br />

smallest element <strong>in</strong> an unordered sequence of n elements on which a total order<br />

relation is def<strong>in</strong>ed. R<strong>and</strong>omized quick-select runs <strong>in</strong> O(n) expected time, taken over<br />

all possible r<strong>and</strong>om choices made by the algorithm, <strong>and</strong> this expectation does not<br />

depend whatsoever on any r<strong>and</strong>omness assumptions about the <strong>in</strong>put distribution.<br />

We note though that r<strong>and</strong>omized quick-select runs <strong>in</strong> O(n 2 ) time <strong>in</strong> the worst case,<br />

the justification of which is left as an exercise (R-11.25). We also provide an<br />

727

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

Saved successfully!

Ooh no, something went wrong!