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 2757.16 (a) Devise an algorithm <strong>to</strong> sort three numbers. It should make as few comparisonsas possible. How many comparisons <strong>and</strong> swaps are requiredin the best, worst, <strong>and</strong> average cases?(b) Devise an algorithm <strong>to</strong> sort five numbers. It should make as few comparisonsas possible. How many comparisons <strong>and</strong> swaps are requiredin the best, worst, <strong>and</strong> average cases?(c) Devise an algorithm <strong>to</strong> sort eight numbers. It should make as few comparisonsas possible. How many comparisons <strong>and</strong> swaps are requiredin the best, worst, <strong>and</strong> average cases?7.17 Devise an efficient algorithm <strong>to</strong> sort a set of numbers with values in the range0 <strong>to</strong> 30,000. There are no duplicates. Keep memory requirements <strong>to</strong> a minimum.7.18 Which of the following operations are best implemented by first sorting thelist of numbers? For each operation, briefly describe an algorithm <strong>to</strong> implementit, <strong>and</strong> state the algorithm’s asymp<strong>to</strong>tic complexity.(a) Find the minimum value.(b) Find the maximum value.(c) Compute the arithmetic mean.(d) Find the median (i.e., the middle value).(e) Find the mode (i.e., the value that appears the most times).7.19 Consider a recursive Mergesort implementation that calls Insertion Sort onsublists smaller than some threshold. If there are n calls <strong>to</strong> Mergesort, howmany calls will there be <strong>to</strong> Insertion Sort? Why?7.20 Implement Mergesort for the case where the input is a linked list.7.21 Counting sort (assuming the input key values are integers in the range 0 <strong>to</strong>m − 1) works by counting the number of records with each key value in thefirst pass, <strong>and</strong> then uses this information <strong>to</strong> place the records in order in asecond pass. Write an implementation of counting sort (see the implementationof radix sort for some ideas). What can we say about the relative valuesof m <strong>and</strong> n for this <strong>to</strong> be effective? If m < n, what is the running time ofthis algorithm?7.22 Use an argument similar <strong>to</strong> that given in Section 7.9 <strong>to</strong> prove that log n is aworst-case lower bound for the problem of searching for a given value in asorted array containing n elements.7.12 Projects7.1 One possible improvement for Bubble Sort would be <strong>to</strong> add a flag variable<strong>and</strong> a test that determines if an exchange was made during the current iteration.If no exchange was made, then the list is sorted <strong>and</strong> so the algorithm

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

Saved successfully!

Ooh no, something went wrong!