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.12 Projects 2637.6 It has been proposed th<strong>at</strong> 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 to X. If X is too small, it is swapped with its larger child <strong>and</strong> theprocess repe<strong>at</strong>ed. The proposed optimiz<strong>at</strong>ion dispenses with the test againstX. Instead, the larger child autom<strong>at</strong>ically replaces X, until X reaches thebottom level of the heap. At this point, X might be too large to remain inth<strong>at</strong> position. This is corrected by repe<strong>at</strong>edly comparing X with its parent<strong>and</strong> swapping as necessary to “bubble” it up to its proper level. The claimis th<strong>at</strong> this process will save a number of comparisons because most nodeswhen sifted down end up near the bottom of the tree anyway. Implement bothversions of siftdown, <strong>and</strong> do an empirical study to compare their runningtimes.7.7 Radix Sort is typically implemented to support only a radix th<strong>at</strong> is a powerof two. This allows for a direct conversion from the radix to 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 to a more efficientimplement<strong>at</strong>ion because bit shifting can replace the division oper<strong>at</strong>ionsshown in the implement<strong>at</strong>ion of Section 7.7. Re-implement the Radix Sortcode given in Section 7.7 to use bit shifting in place of division. Comparethe running time of the old <strong>and</strong> new Radix Sort implement<strong>at</strong>ions.7.8 Write your own collection of sorting programs to implement the algorithmsdescribed in this chapter, <strong>and</strong> compare their running times. Be sure to implementoptimized versions, trying to make each program as fast as possible.Do you get the same rel<strong>at</strong>ive timings as shown in Figure 7.20? If not, why doyou think this happened? How do your results compare with those of yourclassm<strong>at</strong>es? Wh<strong>at</strong> does this say about the difficulty of doing empirical timingstudies?

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

Saved successfully!

Ooh no, something went wrong!