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.

O(n + N) time for sort<strong>in</strong>g all the sequences (not as a union), where n denotes the<br />

total size of all the sequences.<br />

C-11.19<br />

Given a sequence S of n elements, on which a total order relation is def<strong>in</strong>ed,<br />

describe an efficient method for determ<strong>in</strong><strong>in</strong>g whether there are two equal<br />

elements <strong>in</strong> S. What is the runn<strong>in</strong>g time of your method?<br />

C-11.20<br />

Let S be a sequence of n elements on which a total order relation is def<strong>in</strong>ed.<br />

Recall that an <strong>in</strong>version <strong>in</strong> S is a pair of elements x <strong>and</strong> y such that x appears<br />

before y <strong>in</strong> S but x > y. Describe an algorithm runn<strong>in</strong>g <strong>in</strong> O(nlogn) time for<br />

determ<strong>in</strong><strong>in</strong>g the number of <strong>in</strong>versions <strong>in</strong> S.<br />

C-11.21<br />

Let S be a sequence of n <strong>in</strong>tegers. Describe a method for pr<strong>in</strong>t<strong>in</strong>g out all the<br />

pairs of <strong>in</strong>versions <strong>in</strong> S <strong>in</strong> O(n + k) time, where k is the number of such<br />

<strong>in</strong>versions.<br />

C-11.22<br />

Let S be a r<strong>and</strong>om permutation of n dist<strong>in</strong>ct <strong>in</strong>tegers. Argue that the expected<br />

runn<strong>in</strong>g time of <strong>in</strong>sertion-sort on S is (n2) . (H<strong>in</strong>t: Note that half of the elements<br />

ranked <strong>in</strong> the top half of a sorted version of S are expected to be <strong>in</strong> the first half<br />

of S.)<br />

C-11.23<br />

Let A <strong>and</strong> B be two sequences of n <strong>in</strong>tegers each. Given an <strong>in</strong>teger m, describe<br />

an O(n log n) -time algorithm for determ<strong>in</strong><strong>in</strong>g if there is an <strong>in</strong>teger a <strong>in</strong> A <strong>and</strong> an<br />

<strong>in</strong>teger b <strong>in</strong> B such that m = a + b.<br />

C-11.24<br />

Given a set of n <strong>in</strong>tegers, describe <strong>and</strong> analyze a fast method for f<strong>in</strong>d<strong>in</strong>g the<br />

…logn… <strong>in</strong>tegers closest to the median.<br />

C-11.25<br />

Bob has a set A of n nuts <strong>and</strong> a set B of n bolts, such that each nut <strong>in</strong> A has a<br />

unique match<strong>in</strong>g bolt <strong>in</strong> B. Unfortunately, the nuts <strong>in</strong> A all look the same, <strong>and</strong><br />

the bolts <strong>in</strong> B all look the same as well. The only k<strong>in</strong>d of a comparison that Bob<br />

can make is to take a nut-bolt pair (a, b), such that a is <strong>in</strong> A <strong>and</strong> b is <strong>in</strong> B, <strong>and</strong><br />

test it to see if the threads of a are larger, smaller, or a perfect match with the<br />

736

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

Saved successfully!

Ooh no, something went wrong!