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.

Sec. 7.8 An Empirical Comparison of Sorting <strong>Algorithm</strong>s 251a 32-bit key. Of course, this requires a cnt array of size 64K. Performance willbe good only if the number of records is close to 64K or gre<strong>at</strong>er. In other words,the number of records must be large compared to the key size for Radix Sort to beefficient. In many sorting applic<strong>at</strong>ions, Radix Sort can be tuned in this way to givegood performance.Radix Sort depends on the ability to make a fixed number of multiway choicesbased on a digit value, as well as r<strong>and</strong>om access to the bins. Thus, Radix Sortmight be difficult to implement for certain key types. For example, if the keysare real numbers or arbitrary length strings, then some care will be necessary inimplement<strong>at</strong>ion. In particular, Radix Sort will need to be careful about decidingwhen the “last digit” has been found to distinguish among real numbers, or the lastcharacter in variable length strings. Implementing the concept of Radix Sort withthe trie d<strong>at</strong>a structure (Section 13.1) is most appropri<strong>at</strong>e for these situ<strong>at</strong>ions.At this point, the perceptive reader might begin to question our earlier assumptionth<strong>at</strong> key comparison takes constant time. If the keys are “normal integer”values stored in, say, an integer variable, wh<strong>at</strong> is the size of this variable comparedto n? In fact, it is almost certain th<strong>at</strong> 32 (the number of bits in a st<strong>and</strong>ard int variable)is gre<strong>at</strong>er than log n for any practical comput<strong>at</strong>ion. In this sense, comparisonof two long integers requires Ω(log n) work.<strong>Computer</strong>s normally do arithmetic in units of a particular size, such as a 32-bitword. Regardless of the size of the variables, comparisons use this n<strong>at</strong>ive wordsize <strong>and</strong> require a constant amount of time since the comparison is implemented inhardware. In practice, comparisons of two 32-bit values take constant time, eventhough 32 is much gre<strong>at</strong>er than log n. To some extent the truth of the propositionth<strong>at</strong> there are constant time oper<strong>at</strong>ions (such as integer comparison) is in the eyeof the beholder. At the g<strong>at</strong>e level of computer architecture, individual bits arecompared. However, constant time comparison for integers is true in practice onmost computers (they require a fixed number of machine instructions), <strong>and</strong> we relyon such assumptions as the basis for our analyses. In contrast, Radix Sort must doseveral arithmetic calcul<strong>at</strong>ions on key values (each requiring constant time), wherethe number of such calcul<strong>at</strong>ions is proportional to the key length. Thus, Radix Sorttruly does Ω(n log n) work to process n distinct key values.7.8 An Empirical Comparison of Sorting <strong>Algorithm</strong>sWhich sorting algorithm is fastest? Asymptotic complexity analysis lets us distinguishbetween Θ(n 2 ) <strong>and</strong> Θ(n log n) algorithms, but it does not help distinguishbetween algorithms with the same asymptotic complexity. Nor does asymptoticanalysis say anything about which algorithm is best for sorting small lists. Foranswers to these questions, we can turn to empirical testing.

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

Saved successfully!

Ooh no, something went wrong!