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.

In other words (recall<strong>in</strong>g that we omit a logarithm's base when it is 2), m > logn.<br />

Thus, we have<br />

m = logn + 1,<br />

which implies that b<strong>in</strong>ary search runs <strong>in</strong> O(logn) time.<br />

There is a simple variation of b<strong>in</strong>ary search that performs f<strong>in</strong>dAll(k) <strong>in</strong> time<br />

O(logn + s), where s is the number of entries <strong>in</strong> the iterator returned. The details<br />

are left as an exercise (C-9.4).<br />

Thus, we can use an ordered search table to perform fast dictionary searches, but<br />

us<strong>in</strong>g such a table for lots of dictionary updates would take a considerable amount<br />

of time. For this reason, the primary applications for search tables are <strong>in</strong> situations<br />

where we expect few updates to the dictionary but many searches. Such a<br />

situation could arise, for example, <strong>in</strong> an ordered list of English words we use to<br />

order entries <strong>in</strong> an encyclopedia or help file.<br />

Compar<strong>in</strong>g Dictionary Implementations<br />

Table 9.3 compares the runn<strong>in</strong>g times of the methods of a dictionary realized by<br />

either an unordered list, a hash table, or an ordered search table. Note that an<br />

unordered list allows for fast <strong>in</strong>sertions but slow searches <strong>and</strong> removals, whereas<br />

a search table allows for fast searches but slow <strong>in</strong>sertions <strong>and</strong> removals.<br />

Incidentally, although we don't explicitly discuss it, we note that a sorted list<br />

implemented with a doubly l<strong>in</strong>ked list would be slow <strong>in</strong> perform<strong>in</strong>g almost all the<br />

dictionary operations. (See Exercise R-9.3.)<br />

Table 9.3: Comparison of the runn<strong>in</strong>g times of the<br />

methods of a dictionary realized by means of an<br />

unordered list, a hash table, or an ordered search<br />

table. We let n denote the number of entries <strong>in</strong> the<br />

dictionary, N denote the capacity of the bucket array<br />

<strong>in</strong> the hash table implementations, <strong>and</strong> s denote the<br />

size of collection returned by operation f<strong>in</strong>dAll. The<br />

space requirement of all the implementations is O(n),<br />

assum<strong>in</strong>g that the arrays support<strong>in</strong>g the hash table<br />

<strong>and</strong> search table implementations are ma<strong>in</strong>ta<strong>in</strong>ed such<br />

that their capacity is proportional to the number of<br />

entries <strong>in</strong> the dictionary.<br />

556

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

Saved successfully!

Ooh no, something went wrong!