13.07.2015 Views

Computer Science 673 Fall 2012 Homework 4: Sorting & Medians ...

Computer Science 673 Fall 2012 Homework 4: Sorting & Medians ...

Computer Science 673 Fall 2012 Homework 4: Sorting & Medians ...

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.

Department of <strong>Computer</strong> <strong>Science</strong>University of San Francisco<strong>Computer</strong> <strong>Science</strong> <strong>673</strong><strong>Fall</strong> 2013<strong>Homework</strong> 4: <strong>Sorting</strong> & <strong>Medians</strong>Due Wednesday, September 25thAll problem / exercise numbers are from the 2nd edition of Introduction to Algorithms(the 1st edition is different!) Note the difference between problems and exercises!1. (6 points) Consider a sorted list L of n integers, each of which must be distinct (that is,the list contains no duplicates). L may contain negative elements. Give an algorithm(describe in English, and then give pseudo-code) that finds all indicies i such thatL[i] = i. Your algorithm should run in O(lg n + k) time, where k is the number ofelements i such that L[i] = i.2. Problem 8-2 <strong>Sorting</strong> in place in linear timeSuppose that we have an array of n data records to sort and that the key of each recordhas the value 0 or 1. An algorithm for sorting such a set of records might posses somesubset of the following thre desirable characteristics:1. The algorithm runs in O(n) time2. The algorithm is stable3. The algorithm sorts in place, using no more than a constant amount of storagespace in addition to the original array(a) (3 points) Give an algorithm that satisfies criteria 1 and 2 above(b) (3 points) Give an algorithm that satisfies criteria 1 and 3 above(c) (3 points) Give an algorithm that satisfies criteria 2 and 3 above(d) (3 points) Can you use any of your sorting algorithms from parts (a) - (c) aboveas the sorting method in line 2 of Radix-Sort, so that Radix-Sort sorts n recordswith b-bit keys in time O(bn)? Explain how or why not(e) Suppose that the n records have keys in the range 1 . . . k. Show how to modifycounting sort so that it sorts the records in place in O(n + k) time. You may useO(k) storage space outside the original array. Is your algorithm stable? (Hint:How would you do it for k = 3?)3. (6 points) Let X[1 . . . n] and Y [1 . . . n] be two arrays, each containing n numbers,already in sorted order. Give O(lg n)-time algorithm to find the median of all 2nelements in arrays X and Y .1


4. Special Cases of Select:(a) (4 points) Give an algorithm to find the second smallest element in a list of nelements in at most n + ⌈lg n⌉ − 2 comparisons. Note: Your algorithm can useno more than n + ⌈lg n⌉ − 2 total comparisons in the worst case. Hint: Find thesmallest element, too.(b) (4 points) Give an algorithm to find the 3rd smallest element in a list, using thesmallest number of worst-case comparisons. Exactly how many comparisons doesyour algorithm take in the worst case? Hint: Use your solution to part a — youwill need to find the smallest and second-smallest element as well!2

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

Saved successfully!

Ooh no, something went wrong!