This preview has intentionally blurred sections. Sign up to view the full version.
View Full 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 21. 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 maxheap 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 BUILDMAXHEAP moves no items, since the array already contains a maxheap. 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
This note was uploaded on 01/20/2012 for the course CS 6.006 taught by Professor Erikdemaine during the Fall '08 term at MIT.
 Fall '08
 ErikDemaine
 Algorithms, Sort

Click to edit the document details