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.

Consider<strong>in</strong>g the runn<strong>in</strong>g time of b<strong>in</strong>ary search, we observe that a constant num<br />

ber of primitive operations are executed at each recursive call of method B<strong>in</strong>ary<br />

Search. Hence, the runn<strong>in</strong>g time is proportional to the number of recursive calls<br />

performed. A crucial fact is that with each recursive call the number of c<strong>and</strong>idate<br />

entries still to be searched <strong>in</strong> the array list S is given by the value<br />

high − low + 1.<br />

Moreover, the number of rema<strong>in</strong><strong>in</strong>g c<strong>and</strong>idates is reduced by at least one half with<br />

each recursive call. Specifically, from the def<strong>in</strong>ition of mid, the number of rema<strong>in</strong><br />

<strong>in</strong>g c<strong>and</strong>idates is either<br />

or<br />

Initially, the number of c<strong>and</strong>idate entries is n; after the first call to<br />

B<strong>in</strong>arySearch, it is at most n/2; after the second call, it is at most n/4; <strong>and</strong> so<br />

on. In general, after the ith call to B<strong>in</strong>arySearch, the number of c<strong>and</strong>idate<br />

entries rema<strong>in</strong><strong>in</strong>g is at most n/2 i . In the worst case (unsuccessful search), the<br />

recursive calls stop when there are no more c<strong>and</strong>idate entries. Hence, the<br />

maximum number of recursive calls performed, is the smallest <strong>in</strong>teger m such that<br />

n/2 m < 1.<br />

555

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

Saved successfully!

Ooh no, something went wrong!