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...

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Sec. 7.8 An Empirical Comparison of Sorting <strong>Algorithm</strong>s 265are real numbers or arbitrary length strings, then some care will be necessary inimplementation. In particular, Radix Sort will need <strong>to</strong> be careful about decidingwhen the “last digit” has been found <strong>to</strong> distinguish among real numbers, or the lastcharacter in variable length strings. Implementing the concept of Radix Sort withthe trie data structure (Section 13.1) is most appropriate for these situations.At this point, the perceptive reader might begin <strong>to</strong> question our earlier assumptionthat key comparison takes constant time. If the keys are “normal integer”values s<strong>to</strong>red in, say, an integer variable, what is the size of this variable compared<strong>to</strong> n? In fact, it is almost certain that 32 (the number of bits in a st<strong>and</strong>ard int variable)is greater than log n for any practical computation. In this sense, comparisonof two long integers requires Ω(log n) work.Computers normally do arithmetic in units of a particular size, such as a 32-bitword. Regardless of the size of the variables, comparisons use this native wordsize <strong>and</strong> require a constant amount of time. In practice, comparisons of two 32-bitvalues take constant time, even though 32 is much greater than log n. To someextent the truth of the proposition that there are constant time operations (such asinteger comparison) is in the eye of the beholder. At the gate level of computerarchitecture, individual bits are compared. However, constant time comparison forintegers is true in practice on most computers, <strong>and</strong> we rely on such assumptionsas the basis for our analyses. In contrast, Radix Sort must do several arithmeticcalculations on key values (each requiring constant time), where the number ofsuch calculations is proportional <strong>to</strong> the key length. Thus, Radix Sort truly doesΩ(n log n) work <strong>to</strong> process n distinct key values.7.8 An Empirical Comparison of Sorting <strong>Algorithm</strong>sWhich sorting algorithm is fastest? Asymp<strong>to</strong>tic complexity analysis lets us distinguishbetween Θ(n 2 ) <strong>and</strong> Θ(n log n) algorithms, but it does not help distinguishbetween algorithms with the same asymp<strong>to</strong>tic complexity. Nor does asymp<strong>to</strong>ticanalysis say anything about which algorithm is best for sorting small lists. Foranswers <strong>to</strong> these questions, we can turn <strong>to</strong> empirical testing.Figure 7.13 shows timing results for actual implementations of the sorting algorithmspresented in this chapter. The algorithms compared include Insertion Sort,Bubble Sort, Selection Sort, Shellsort, Quicksort, Mergesort, Heapsort <strong>and</strong> RadixSort. Shellsort shows both the basic version from Section 7.3 <strong>and</strong> another withincrements based on division by three. Mergesort shows both the basic implementationfrom Section 7.4 <strong>and</strong> the optimized version with calls <strong>to</strong> Insertion Sort forlists of length below nine. For Quicksort, two versions are compared: the basicimplementation from Section 7.5 <strong>and</strong> an optimized version that does not partition

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

Saved successfully!

Ooh no, something went wrong!