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

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Argue why the previous claim proves r<strong>and</strong>omized quick-sort runs <strong>in</strong><br />

O(nlogn) time with probability at least 1 − 1/n.<br />

C-11.11<br />

Given an array A of n entries with keys equal to 0 or 1, describe an <strong>in</strong>-place<br />

method for order<strong>in</strong>g A so that all the 0's are before every 1.<br />

C-11.12<br />

Suppose we are given an n-element sequence S such that each element <strong>in</strong> S<br />

represents a different vote for president, where each vote is given as an <strong>in</strong>teger<br />

represent<strong>in</strong>g a particular c<strong>and</strong>idate. Design an O(nlogn)time algorithm to see<br />

who w<strong>in</strong>s the election S represents, assum<strong>in</strong>g the c<strong>and</strong>idate with the most votes<br />

w<strong>in</strong>s (even if there are O(n) c<strong>and</strong>idates).<br />

C-11.13<br />

Consider the vot<strong>in</strong>g problem from Exercise C-11.12, but now suppose that we<br />

know the number k < n of c<strong>and</strong>idates runn<strong>in</strong>g. Describe an O(nlogk)time<br />

algorithm for determ<strong>in</strong><strong>in</strong>g who w<strong>in</strong>s the election.<br />

C-11.14<br />

Consider the vot<strong>in</strong>g problem from Exercise C-11.12, but now suppose a<br />

c<strong>and</strong>idate w<strong>in</strong>s only if he or she gets a majority of the votes cast. Design <strong>and</strong><br />

analyze a fast algorithm for determ<strong>in</strong><strong>in</strong>g the w<strong>in</strong>ner if there is one.<br />

C-11.15<br />

Show that any comparison-based sort<strong>in</strong>g algorithm can be made to be stable<br />

without affect<strong>in</strong>g its asymptotic runn<strong>in</strong>g time.<br />

C-11.16<br />

Suppose we are given two sequences A <strong>and</strong> B of n elements, possibly conta<strong>in</strong><strong>in</strong>g<br />

duplicates, on which a total order relation is def<strong>in</strong>ed. Describe an efficient<br />

algorithm for determ<strong>in</strong><strong>in</strong>g if A <strong>and</strong> B conta<strong>in</strong> the same set of elements. What is<br />

the runn<strong>in</strong>g time of this method?<br />

C-11.17<br />

Given an array A of n <strong>in</strong>tegers <strong>in</strong> the range [0,n 2 − 1], describe a simple method<br />

for sort<strong>in</strong>g A <strong>in</strong> O(n) time.<br />

C-11.18<br />

Let S 1 ,S 2 ,...,Sk be k different sequences whose elements have <strong>in</strong>teger keys <strong>in</strong> the<br />

range [0,N − 1], for some parameter N ≥ 2. Describe an algorithm runn<strong>in</strong>g <strong>in</strong><br />

735

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

Saved successfully!

Ooh no, something went wrong!