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

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Sec. 7.12 Projects 2617.18 Which of the following oper<strong>at</strong>ions are best implemented by first sorting thelist of numbers? For each oper<strong>at</strong>ion, briefly describe an algorithm to implementit, <strong>and</strong> st<strong>at</strong>e the algorithm’s asymptotic 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 th<strong>at</strong> appears the most times).7.19 Consider a recursive Mergesort implement<strong>at</strong>ion th<strong>at</strong> calls Insertion Sort onsublists smaller than some threshold. If there are n calls to Mergesort, howmany calls will there be to 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 tom − 1) works by counting the number of records with each key value in thefirst pass, <strong>and</strong> then uses this inform<strong>at</strong>ion to place the records in order in asecond pass. Write an implement<strong>at</strong>ion of counting sort (see the implement<strong>at</strong>ionof radix sort for some ideas). Wh<strong>at</strong> can we say about the rel<strong>at</strong>ive valuesof m <strong>and</strong> n for this to be effective? If m < n, wh<strong>at</strong> is the running time ofthis algorithm?7.22 Use an argument similar to th<strong>at</strong> given in Section 7.9 to prove th<strong>at</strong> log n is aworst-case lower bound for the problem of searching for a given value in asorted array containing n elements.7.23 A simpler way to do the Quicksort partition step is to set index splitto the position of the first value gre<strong>at</strong>er than the pivot. Then from positionsplit+1 have another index curr move to the right until it finds avalue less than a pivot. Swap the values <strong>at</strong> split <strong>and</strong> next, <strong>and</strong> incrementsplit. Continue in this way, swapping the smaller values to the leftside. When curr reaches the end of the subarray, split will be <strong>at</strong> the splitpoint between the two partitions. Unfortun<strong>at</strong>ely, this approach requires moreswaps than does the version presented in Section 7.5, resulting in a slowerimplement<strong>at</strong>ion. Give an example <strong>and</strong> explain why.7.12 Projects7.1 One possible improvement for Bubble Sort would be to add a flag variable<strong>and</strong> a test th<strong>at</strong> determines if an exchange was made during the current iter<strong>at</strong>ion.If no exchange was made, then the list is sorted <strong>and</strong> so the algorithmcan stop early. This makes the best case performance become O(n) (becauseif the list is already sorted, then no iter<strong>at</strong>ions will take place on the first pass,<strong>and</strong> the sort will stop right there).

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

Saved successfully!

Ooh no, something went wrong!