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.

Sec. 9.1 Searching Unsorted <strong>and</strong> Sorted Arrays 323It is not always practical <strong>to</strong> reduce an algorithm’s growth rate. There is a practicalitywindow for every problem, in that we have a practical limit <strong>to</strong> how big aninput we wish <strong>to</strong> solve for. If our problem size never grows <strong>to</strong>o big, it might notmatter if we can reduce the cost by an extra log fac<strong>to</strong>r, because the constant fac<strong>to</strong>rsin the two algorithms might overwhelm the savings.For our two algorithms, let us look further <strong>and</strong> check the actual number ofcomparisons used. For binary search, we need about log n − 1 <strong>to</strong>tal comparisons.Quadratic binary search requires about 2.4 lg lg n comparisons. If we incorporatethis observation in<strong>to</strong> our table, we get a different picture about the relative differences.n lg n − 1 2.4 lg lg n Difference16 3 4.8 worse256 7 7.2 ≈ same64K 15 9.6 1.62 32 31 12 2.6But we still are not done. This is only a count of comparisons! Binary searchis inherently much simpler than QBS, because binary search only needs <strong>to</strong> calculatethe midpoint position of the array before each comparison, while quadraticbinary search must calculate an interpolation point which is more expensive. Sothe constant fac<strong>to</strong>rs for QBS are even higher.Not only are the constant fac<strong>to</strong>rs worse on average, but QBS is far more dependentthan binary search on good data distribution <strong>to</strong> perform well. For example,imagine that you are searching a telephone direc<strong>to</strong>ry for the name “Young.” Normallyyou would look near the back of the book. If you found a name beginningwith ‘Z,’ you might look just a little ways <strong>to</strong>ward the front. If the next name youfind also begins with ’Z,‘ you would look a little further <strong>to</strong>ward the front. If thisparticular telephone direc<strong>to</strong>ry were unusual in that half of the entries begin with ‘Z,’then you would need <strong>to</strong> move <strong>to</strong>ward the front many times, each time eliminatingrelatively few records from the search. In the extreme, the performance of interpolationsearch might not be much better than sequential search if the distribution ofkey values is badly calculated.While it turns out that QBS is not a practical algorithm, this is not a typicalsituation. Fortunately, algorithm growth rates are usually well behaved, so that asymp<strong>to</strong>ticalgorithm analysis nearly always gives us a practical indication for whichof two algorithms is better.

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

Saved successfully!

Ooh no, something went wrong!