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.

Figure 9.7: Realization of a dictionary D by means<br />

of an ordered search table. We show only the keys for<br />

this dictionary, so as to highlight their order<strong>in</strong>g.<br />

The space requirement of an ordered search table is O(n), which is similar to the<br />

list-based dictionary implementation (Section 9.3.1), assum<strong>in</strong>g we grow <strong>and</strong> shr<strong>in</strong>k<br />

the array support<strong>in</strong>g the array list S to keep the size of this array proportional to the<br />

number of entries <strong>in</strong> S. Unlike an unordered list, however, perform<strong>in</strong>g updates <strong>in</strong> a<br />

search table takes a considerable amount of time. In particular, perform<strong>in</strong>g the<br />

<strong>in</strong>sert(k,v) operation <strong>in</strong> a search table requires O(n) time, s<strong>in</strong>ce we need to shift<br />

up all the entries <strong>in</strong> the array list with key greater than k to make room for the new<br />

entry (k, v). A similar observation applies to the operation remove (k), s<strong>in</strong>ce it<br />

takes O(n) time to shift all the entries <strong>in</strong> the array list with key greater than k to<br />

close the "hole" left by the removed entry (or entries). The search table<br />

implementation is therefore <strong>in</strong>ferior to the log file <strong>in</strong> terms of the worst-case<br />

runn<strong>in</strong>g times of the dictionary update operations. Nevertheless, we can perform the<br />

f<strong>in</strong>d method much faster <strong>in</strong> a search table.<br />

B<strong>in</strong>ary Search<br />

A significant advantage of us<strong>in</strong>g an ordered array list S to implement a dictionary<br />

D with n entries is that access<strong>in</strong>g an element of S by its <strong>in</strong>dex takes O(1) time. We<br />

recall, from Section 6.1, that the <strong>in</strong>dex of an element <strong>in</strong> an array list is the number<br />

of elements preced<strong>in</strong>g it. Thus, the first element <strong>in</strong> S has <strong>in</strong>dex 0, <strong>and</strong> the last<br />

element has <strong>in</strong>dex n − 1.<br />

The elements stored <strong>in</strong> S are the entries of dictionary D, <strong>and</strong> s<strong>in</strong>ce S is ordered, the<br />

entry at <strong>in</strong>dex i has a key no smaller than the keys of the entries at <strong>in</strong>dices 0,…, i<br />

− 1, <strong>and</strong> no larger than the keys of the entries at <strong>in</strong>dices i + 1,…, n − 1. This<br />

observation allows us to quickly "home <strong>in</strong>" on a search key k us<strong>in</strong>g a variant of<br />

the children's game "high-low." We call an entry of D a c<strong>and</strong>idate if, at the<br />

current stage of the search, we cannot rule out that this entry has key equal to k.<br />

The algorithm ma<strong>in</strong>ta<strong>in</strong>s two parameters, low <strong>and</strong> high, such that all the<br />

c<strong>and</strong>idate entries have <strong>in</strong>dex at least low <strong>and</strong> at most high <strong>in</strong> S. Initially, low =<br />

0 <strong>and</strong> high = n − 1. We then compare k to the key of the median c<strong>and</strong>idate e, that<br />

is, the entry e with <strong>in</strong>dex<br />

mid = (low + high)/2.<br />

We consider three cases:<br />

553

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

Saved successfully!

Ooh no, something went wrong!