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.11 Exercises 2737.7 Recall that a sorting algorithm is said <strong>to</strong> be stable if the original ordering forduplicate keys is preserved. We can make any algorithm stable if we alterthe input keys so that (potentially) duplicate key values are made unique ina way that the first occurrence of the original duplicate value is less than thesecond occurrence, which in turn is less than the third, <strong>and</strong> so on. In the worstcase, it is possible that all n input records have the same key value. Give analgorithm <strong>to</strong> modify the key values such that every modified key value isunique, the resulting key values give the same sort order as the original keys,the result is stable (in that the duplicate original key values remain in theiroriginal order), <strong>and</strong> the process of altering the keys is done in linear timeusing only a constant amount of additional space.7.8 The discussion of Quicksort in Section 7.5 described using a stack instead ofrecursion <strong>to</strong> reduce the number of function calls made.(a) How deep can the stack get in the worst case?(b) Quicksort makes two recursive calls. The algorithm could be changed<strong>to</strong> make these two calls in a specific order. In what order should thetwo calls be made, <strong>and</strong> how does this affect how deep the stack canbecome?7.9 Give a permutation for the values 0 through 7 that will cause Quicksort (asimplemented in Section 7.5) <strong>to</strong> have its worst case behavior.7.10 Assume L is an array, length(L) returns the number of records in thearray, <strong>and</strong> qsort(L, i, j) sorts the records of L from i <strong>to</strong> j (leavingthe records sorted in L) using the Quicksort algorithm. What is the averagecasetime complexity for each of the following code fragments?(a) for (i=0; i

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

Saved successfully!

Ooh no, something went wrong!