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.

• If k = e.getKey(), then we have found the entry we were look<strong>in</strong>g for,<br />

<strong>and</strong> the search term<strong>in</strong>ates successfully return<strong>in</strong>g e.<br />

• If k < e.getKey(), then we recur on the first half of the array list, that is,<br />

on the range of <strong>in</strong>dices from low to mid − 1.<br />

• If k > e.getKey(), we recur on the range of <strong>in</strong>dices from mid + 1 to<br />

high.<br />

This search method is called b<strong>in</strong>ary search, <strong>and</strong> is given <strong>in</strong> pseudo-code <strong>in</strong> Code<br />

Fragment 9.9. Operation f<strong>in</strong>d(k) on an n-entry dictionary implemented with an<br />

ordered array list S consists of call<strong>in</strong>g B<strong>in</strong>arySearch(S,k,0,n − 1).<br />

Code Fragment 9.9:<br />

array list.<br />

B<strong>in</strong>ary search <strong>in</strong> an ordered<br />

We illustrate the b<strong>in</strong>ary search algorithm <strong>in</strong> Figure 9.8.<br />

Figure 9.8: Example of a b<strong>in</strong>ary search to perform<br />

operation f<strong>in</strong>d(22), <strong>in</strong> a dictio nary with <strong>in</strong>teger<br />

keys, implemented with an ordered array list. For<br />

simplicity, we show the keys stored <strong>in</strong> the dictionary<br />

but not the whole entries.<br />

554

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

Saved successfully!

Ooh no, something went wrong!