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.

Bucket-Sort <strong>and</strong> Radix-Sort<br />

F<strong>in</strong>ally, if our application <strong>in</strong>volves sort<strong>in</strong>g entries with small <strong>in</strong>teger keys or d-<br />

tuples of small <strong>in</strong>teger keys, then bucket-sort or radix-sort is an excellent choice,<br />

for it runs <strong>in</strong> O(d(n + N)) time, where [0,N − 1] is the range of <strong>in</strong>teger keys (<strong>and</strong> d =<br />

1 for bucket sort). Thus, if d(n + N) is significantly "below" the nlogn function, then<br />

this sort<strong>in</strong>g method should run faster than even quick-sort or heap-sort.<br />

Thus, our study of all these different sort<strong>in</strong>g algorithms provides us with a versatile<br />

collection of sort<strong>in</strong>g methods <strong>in</strong> our algorithm eng<strong>in</strong>eer<strong>in</strong>g "toolbox."<br />

11.6 The Set ADT <strong>and</strong> Union/F<strong>in</strong>d <strong>Structures</strong><br />

In this section, we <strong>in</strong>troduce the set ADT. A set is a collection of dist<strong>in</strong>ct objects.<br />

That is, there are no duplicate elements <strong>in</strong> a set, <strong>and</strong> there is no explicit notion of keys<br />

or even an order. Even so, we <strong>in</strong>clude our discussion of sets here <strong>in</strong> a chapter on<br />

sort<strong>in</strong>g, because sort<strong>in</strong>g can play an important role <strong>in</strong> efficient implementations of the<br />

operations of the set ADT.<br />

Sets <strong>and</strong> Some of Their Uses<br />

First, we recall the mathematical def<strong>in</strong>itions of the union, <strong>in</strong>tersection, <strong>and</strong><br />

subtraction of two sets A <strong>and</strong> B:<br />

A B = {x:xis <strong>in</strong> A or x is <strong>in</strong> B},<br />

A ∩ B = {x:x is <strong>in</strong> A <strong>and</strong> x is <strong>in</strong> B},<br />

A − B = {x:x is <strong>in</strong> A <strong>and</strong> x /is <strong>in</strong> B}.<br />

Example 11.7: Most Internet search eng<strong>in</strong>es store, for each word x <strong>in</strong> their<br />

dictionary database, a set, W(x), of Web pages that conta<strong>in</strong> x, where each Web page<br />

is identified by a unique Internet address. When presented with a query for a word<br />

x, such a search eng<strong>in</strong>e need only return the Web pages <strong>in</strong> the set W(x), sorted<br />

accord<strong>in</strong>g to some proprietary priority rank<strong>in</strong>g of page "importance." But when<br />

presented with a two-word query for words x <strong>and</strong> y, such a search eng<strong>in</strong>e must first<br />

compute the <strong>in</strong>tersection W(x) ∩ W(y), <strong>and</strong> then return the Web pages <strong>in</strong> the<br />

result<strong>in</strong>g set sorted by priority. Several search eng<strong>in</strong>es use the set <strong>in</strong>tersection<br />

algorithm described <strong>in</strong> this section for this computation.<br />

Fundamental Methods of the Set ADT<br />

The fundamental methods of the set ADT, act<strong>in</strong>g on a set A, are as follows:<br />

union(B): Replace A with the union of A <strong>and</strong> B, that is, execute A←A B.<br />

715

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

Saved successfully!

Ooh no, something went wrong!