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.

320 Chap. 9 Searchingelement in L, that is, we check elements L[j], L[2j], <strong>and</strong> so on. So long as K isgreater than the values we are checking, we continue on. But when we reach avalue in L greater than K, we do a linear search on the piece of length j − 1 thatwe know brackets K if it is in the list.If mj ≤ n < (m+1)j, then the <strong>to</strong>tal cost of this algorithm is at most m+j −13-way comparisons. Therefore, the cost <strong>to</strong> run the algorithm on n items with a jumpof size j is⌊ ⌋ nT(n, j) = m + j − 1 = + j − 1.jWhat is the best value that we can pick for j? We want <strong>to</strong> minimize the cost:{⌊ ⌋ }nmin + j − 11≤j≤n jTake the derivative <strong>and</strong> solve for f ′ (j) = 0 <strong>to</strong> find the minimum, which isj = √ n. In this case, the worst case cost will be roughly 2 √ n.This example teaches us some lessons about algorithm design. We want <strong>to</strong>balance the work done while selecting a sublist with the work done while searchinga sublist. In general, it is a good strategy <strong>to</strong> make subproblems of equal effort. Thisis an example of a divide <strong>and</strong> conquer algorithm.What if we extend this idea <strong>to</strong> three levels? We would first make jumps ofsome size j <strong>to</strong> find a sublist of size j − 1 whose end values bracket value K. Wwwould then work through this sublist by making jumps of some smaller size, sayj 1 . Finally, once we find a bracketed sublist of size j 1 − 1, we would do sequentialsearch <strong>to</strong> complete the process.This probably sounds convoluted <strong>to</strong> do two levels of jumping <strong>to</strong> be followed bya sequential search. While it might make sense <strong>to</strong> do a two-level algorithm (that is,jump search jumps <strong>to</strong> find a sublist <strong>and</strong> then does sequential search on the sublist),it almost never seems <strong>to</strong> make sense <strong>to</strong> do a three-level algorithm. Instead, whenwe go beyond two levels, we nearly always generalize by using recursion. Thisleads us <strong>to</strong> the most commonly used search algorithm for sorted arrays, the binarysearch described in Section 3.5.If we know nothing about the distribution of key values, then binary searchis the best algorithm available for searching a sorted array (see Exercise 9.22).However, sometimes we do know something about the expected key distribution.Consider the typical behavior of a person looking up a word in a large dictionary.Most people certainly do not use sequential search! Typically, people use a modifiedform of binary search, at least until they get close <strong>to</strong> the word that they are

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

Saved successfully!

Ooh no, something went wrong!