11.07.2015 Views

Data Structures and Algorithm Analysis - Computer Science at ...

Data Structures and Algorithm Analysis - Computer Science at ...

Data Structures and Algorithm Analysis - Computer Science at ...

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.

72 Chap. 3 <strong>Algorithm</strong> <strong>Analysis</strong>Position 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15Key 11 13 21 26 29 36 40 41 45 51 54 56 65 72 77 83Figure 3.4 An illustr<strong>at</strong>ion of binary search on a sorted array of 16 positions.Consider a search for the position with value K = 45. Binary search first checksthe value <strong>at</strong> position 7. Because 41 < K, the desired value cannot appear in anyposition below 7 in the array. Next, binary search checks the value <strong>at</strong> position 11.Because 56 > K, the desired value (if it exists) must be between positions 7<strong>and</strong> 11. Position 9 is checked next. Again, its value is too gre<strong>at</strong>. The final searchis <strong>at</strong> position 8, which contains the desired value. Thus, function binary returnsposition 8. Altern<strong>at</strong>ively, if K were 44, then the same series of record accesseswould be made. After checking position 8, binary would return a value of n,indic<strong>at</strong>ing th<strong>at</strong> the search is unsuccessful.The final example of algorithm analysis for this section will compare two algorithmsfor performing search in an array. Earlier, we determined th<strong>at</strong> the runningtime for sequential search on an array where the search value K is equally likelyto appear in any loc<strong>at</strong>ion is Θ(n) in both the average <strong>and</strong> worst cases. We wouldlike to compare this running time to th<strong>at</strong> required to perform a binary search onan array whose values are stored in order from lowest to highest.Binary search begins by examining the value in the middle position of the array;call this position mid <strong>and</strong> the corresponding value k mid . If k mid = K, thenprocessing can stop immedi<strong>at</strong>ely. This is unlikely to be the case, however. Fortun<strong>at</strong>ely,knowing the middle value provides useful inform<strong>at</strong>ion th<strong>at</strong> can help guidethe search process. In particular, if k mid > K, then you know th<strong>at</strong> the value Kcannot appear in the array <strong>at</strong> any position gre<strong>at</strong>er than mid. Thus, you can elimin<strong>at</strong>efuture search in the upper half of the array. Conversely, if k mid < K, thenyou know th<strong>at</strong> you can ignore all positions in the array less than mid. Either way,half of the positions are elimin<strong>at</strong>ed from further consider<strong>at</strong>ion. Binary search nextlooks <strong>at</strong> the middle position in th<strong>at</strong> part of the array where value K may exist. Thevalue <strong>at</strong> this position again allows us to elimin<strong>at</strong>e half of the remaining positionsfrom consider<strong>at</strong>ion. This process repe<strong>at</strong>s until either the desired value is found, orthere are no positions remaining in the array th<strong>at</strong> might contain the value K. Figure3.4 illustr<strong>at</strong>es the binary search method. Figure 3.5 shows an implement<strong>at</strong>ionfor binary search.To find the cost of this algorithm in the worst case, we can model the runningtime as a recurrence <strong>and</strong> then find the closed-form solution. Each recursive callto binary cuts the size of the array approxim<strong>at</strong>ely in half, so we can model theworst-case cost as follows, assuming for simplicity th<strong>at</strong> n is a power of two.T(n) = T(n/2) + 1 for n > 1; T(1) = 1.

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

Saved successfully!

Ooh no, something went wrong!