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.12 Projects 277classmates? What does this say about the difficulty of doing empirical timingstudies?7.5 Perform a study of Shellsort, using different increments. Compare the versionshown in Section 7.3, where each increment is half the previous one,with others. In particular, try implementing “division by 3” where the incrementson a list of length n will be n/3, n/9, etc. Do other increment schemeswork as well?7.6 The implementation for Mergesort given in Section 7.4 takes an array as input<strong>and</strong> sorts that array. At the beginning of Section 7.4 there is a simplepseudocode implementation for sorting a linked list using Mergesort. Implementboth a linked list-based version of Mergesort <strong>and</strong> the array-basedversion of Mergesort, <strong>and</strong> compare their running times.7.7 Radix Sort is typically implemented <strong>to</strong> support only a radix that is a powerof two. This allows for a direct conversion from the radix <strong>to</strong> some numberof bits in an integer key value. For example, if the radix is 16, then a 32-bitkey will be processed in 8 steps of 4 bits each. This can lead <strong>to</strong> a more efficientimplementation because bit shifting can replace the division operationsshown in the implementation of Section 7.7. Re-implement the Radix Sortcode given in Section 7.7 <strong>to</strong> use bit shifting in place of division. Comparethe running time of the old <strong>and</strong> new Radix Sort implementations.7.8 It has been proposed that Heapsort can be optimized by altering the heap’ssiftdown function. Call the value being sifted down X. Siftdown does twocomparisons per level: First the children of X are compared, then the winneris compared <strong>to</strong> X. If X is <strong>to</strong>o small, it is swapped with its larger child <strong>and</strong> theprocess repeated. The proposed optimization dispenses with the test againstX. Instead, the larger child au<strong>to</strong>matically replaces X, until X reaches thebot<strong>to</strong>m level of the heap. At this point, X might be <strong>to</strong>o large <strong>to</strong> remain inthat position. This is corrected by repeatedly comparing X with its parent<strong>and</strong> swapping as necessary <strong>to</strong> “bubble” it up <strong>to</strong> its proper level. The claimis that this process will save a number of comparisons because most nodeswhen sifted down end up near the bot<strong>to</strong>m of the tree anyway. Implement bothversions of siftdown, <strong>and</strong> do an empirical study <strong>to</strong> compare their runningtimes.

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

Saved successfully!

Ooh no, something went wrong!