12.07.2015 Views

A Practical Introduction to Data Structures and Algorithm Analysis

A Practical Introduction to Data Structures and Algorithm Analysis

A Practical Introduction to Data Structures and Algorithm Analysis

SHOW MORE
SHOW LESS
  • No tags were found...

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

78 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 illustration 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 at position 7. Because 41 < K, the desired value cannot appear in anyposition below 7 in the array. Next, binary search checks the value at 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 <strong>to</strong>o great. The final searchis at position 8, which contains the desired value. Thus, function binary returnsposition 8. Alternatively, if K were 44, then the same series of record accesseswould be made. After checking position 8, binary would return a value of n,indicating that the search is unsuccessful.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 call<strong>to</strong> binary cuts the size of the array approximately in half, so we can model theworst-case cost as follows, assuming for simplicity that n is a power of two.T(n) = T(n/2) + 1 for n > 1; T(1) = 1.If we exp<strong>and</strong> the recurrence, we find that we can do so only log n times beforewe reach the base case, <strong>and</strong> each expansion adds one <strong>to</strong> the cost. Thus, the closedformsolution for the recurrence is T(n) = log n.Function binary is designed <strong>to</strong> find the (single) occurrence of K <strong>and</strong> returnits position. A special value is returned if K does not appear in the array. Thisalgorithm can be modified <strong>to</strong> implement variations such as returning the positionof the first occurrence of K in the array if multiple occurrences are allowed, <strong>and</strong>returning the position of the greatest value less than K when K is not in the array.Comparing sequential search <strong>to</strong> binary search, we see that as n grows, the Θ(n)running time for sequential search in the average <strong>and</strong> worst cases quickly becomesmuch greater than the Θ(log n) running time for binary search. Taken in isolation,binary search appears <strong>to</strong> be much more efficient than sequential search. This isdespite the fact that the constant fac<strong>to</strong>r for binary search is greater than that forsequential search, because the calculation for the next search position in binarysearch is more expensive than just incrementing the current position, as sequentialsearch does.Note however that the running time for sequential search will be roughly thesame regardless of whether or not the array values are s<strong>to</strong>red in order. In contrast,

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

Saved successfully!

Ooh no, something went wrong!