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.

methods of the array list ADT on such a representation. Is it better to store the<br />

entries <strong>in</strong> L by <strong>in</strong>creas<strong>in</strong>g <strong>in</strong>dices or not?<br />

C-6.17<br />

There is a simple, but <strong>in</strong>efficient, algorithm, called bubble-sort, for sort<strong>in</strong>g a<br />

sequence S of n comparable elements. This algorithm scans the sequence n−1<br />

times, where, <strong>in</strong> each scan, the algorithm compares the current element with the<br />

next one <strong>and</strong> swaps them if they are out of order. Give a pseudo-code<br />

description of bubble-sort that is as efficient as possible assum<strong>in</strong>g S is<br />

implemented with a doubly l<strong>in</strong>ked list. What is the runn<strong>in</strong>g time of this<br />

algorithm?<br />

C-6.18<br />

Answer Exercise C-6.17 assum<strong>in</strong>g S is implemented with an array list.<br />

C-6.19<br />

A useful operation <strong>in</strong> databases is the natural jo<strong>in</strong>. If we view a database as a<br />

list of ordered pairs of objects, then the natural jo<strong>in</strong> of databases A <strong>and</strong> B is the<br />

list of all ordered triples (x,y,z) such that the pair (x,y) is <strong>in</strong> A <strong>and</strong> the pair (y,z) is<br />

<strong>in</strong> B. Describe <strong>and</strong> analyze an efficient algorithm for comput<strong>in</strong>g the natural jo<strong>in</strong><br />

of a list A of n pairs <strong>and</strong> a list B of m pairs.<br />

C-6.20<br />

When Bob wants to send Alice a message M on the Internet, he breaks M <strong>in</strong>to n<br />

data packets, numbers the packets consecutively, <strong>and</strong> <strong>in</strong>jects them <strong>in</strong>to the<br />

network. When the packets arrive at Alice's computer, they may be out of order,<br />

so Alice must assemble the sequence of n packets <strong>in</strong> order before she can be<br />

sure she has the entire message. Describe an efficient scheme for Alice to do<br />

this. What is the runn<strong>in</strong>g time of this algorithm?<br />

C-6.21<br />

Given a list L of n positive <strong>in</strong>tegers, each represented with k = logn + 1 bits,<br />

describe an O(n)-time method for f<strong>in</strong>d<strong>in</strong>g a k-bit <strong>in</strong>teger not <strong>in</strong> L.<br />

C-6.22<br />

Argue why any solution to the previous problem must run <strong>in</strong> Ω(n) time.<br />

C-6.23<br />

Given a list L of n arbitrary <strong>in</strong>tegers, design an O(n)-time method for f<strong>in</strong>d<strong>in</strong>g an<br />

<strong>in</strong>teger that cannot be formed as the sum of two <strong>in</strong>tegers <strong>in</strong> L.<br />

370

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

Saved successfully!

Ooh no, something went wrong!