ps2sol

ps2sol - Introduction to Algorithms September 27, 2006...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Introduction to Algorithms September 27, 2006 Massachusetts Institute of Technology 6.046J/18.410J Professors Erik Demaine and Madhu Sudan Problem Set 2 Solutions Problem Set 2 Solutions Problem 2-1. Sorting can be boring! A comparison sort is boring if, for infinitely many n , when sorting some sequence of n items, at least one item (or any copy of it) gets compared ( n ) times. In other words, a comparison sort is boring if there exists a positive constant c > such that, for infinitely many values of n , there is a sequence of n items on which the sorting algorithm compares one item at least c n times. Assume throughout this question that all n items must be distinct. (a) Is Mergesort boring? Prove your answer. Solution: Yes. If the initial sequence is already sorted, the first item in the second half of the sequence is compared n/ 2 times, once with each element in the first half of the sequence. (b) Is Quicksort boring? Prove your answer. Solution: Yes. In the first pass that partitions the n items, each item is compared once with the pivot, which is therefore compared ( n ) times. (c) Is Heapsort boring? Prove your answer. Solution: Yes. Consider an array holding a max-heap of n items, where all items in the left subtree (of the root) are smaller than all items in the right subtree. Let us sort the array with Heapsort - note that the initial call to BUILD-MAX-HEAP moves no items, since the array already contains a max-heap. Clearly, after the initial root is extracted, each element in the initial right subtree must be extracted from the heap before any element in the left subtree can be extracted. Now observe that, until all elements of the (initial) right subtree are extracted, the item that is initially the root of the left subtree is not moved, since it is never a leaf and never wins a comparison with the root (i.e. the largest element) of the right subtree. Since there is one such comparison for every item that is extracted from the heap (after the initial root), and the number of elements in the (initial) right subtree is clearly ( n ) , the item that is initially the root of the left subtree is compared ( n ) times. (d) Bonus Part: This part is optional. However, solving this part does not exempt you from solving the other parts of this question. Modify any one of the three sorting algorithms above that you find boring, in such a way that it is no longer boring (but keep the time to sort n items O ( n lg n ) in expectation if randomized). 2 Problem Set 2 Solutions Solution: Consider the following INTERESTINGMERGE algorithm that merges two sorted sequences of or more items: If either of the two sequences contains no items, output the other sequence; otherwise let the two sequences be a 1 , . . . , a p and b 1 , . . . , b q (with p, q 1 ), and: 1. Compare a 1 and b 1 , and assume, without loss of generality, that a 1 b 1 ....
View Full Document

Page1 / 6

ps2sol - Introduction to Algorithms September 27, 2006...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online