sortp

# sortp - Design and Analysis of Algorithms Divide and...

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

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

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Design and Analysis of Algorithms Divide and Conquer and Sorting July 3, 2007 The problems are collected from several books including CLR and “Introduction to Algorithms” by Udi Mander. 1. Stooge Sort: Professors Howard, Fine, and Howard have proposed the following ‘elegant’ sorting algorithm: STOOGE-SORT ( A, i, j ) 1 if A [ i ] > A [ j ] 2 then exchange A [ i ] ↔ A [ j ] 3 if i + 1 ≥ j 4 then return 5 k ← b ( j- i + 1) / 3 c 6 STOOGE-SORT ( A, i, j- k ) 7 STOOGE-SORT ( A, i + k, j ) 8 STOOGE-SORT ( A, i, j- k ) (a) Argue that STOOGE-SORT ( A, 1 , length [ A ]) correctly sorts the input array A [1 ..n ] , where n = length [ A ] (b) Give a recurrence for the worst-case running time of STOOGE-SORT and a tight asymptotic (Θ-notation ) bound on the worst-case running time. (c) Compare the worst-case running time of STOOGE-SORT with that of insertion sort, merge sort, heapsort, and quicksort. Do the professors deserve tenure? 2. k-way merging: Give an O ( n lg k ) algorithm to merge k sorted lists into one sorted list, where n is the total number of elements in all the input lists. Hint: Use a heap for k-way merging. Analyze the time complexity of your algorithm. Prove a tight lower bound on the number of comparisons for k-way merging. 3. Max-sum: Use the divide-and-conquer approach to write an efficient recursive algorithm that finds the maximum sum in any contiguous sublist of a given list of n real (positive or negative) values. Analyze your algorithm, and show the results in order notation. Can you do better? 4. Mode: The mode of a set of numbers is the number that occurs most frequently in the set. The set (4 , 6 , 2 , 4 , 3 , 1) has a mode of 4. 1 (a) Give an efficient and correct algorithm to compute the mode of a set of n numbers. Analyze the time complexity of your algorithm. (b) Suppose that we know that there is an (unknown) element that occurs more than n/ 2 times in the set. Give a worst-case linear-time algorithm to find the mode. For partial credit, your algorithm may run in an expected linear time. Analyze the time complexity of your algorithm. 5. Searching: Write an algorithm that searches a sorted list of n items by dividing it into three sublists of almost n/ 3 items. This algorithm finds the sublist that might contain the given item, and divides it into three smaller sublists of almost equal size. The algorithm repeats this process until it finds the item or concludes that the item is not in the list. Analyze your algorithm, and give the results using order notation. 6. Probabilities for multi-faceted dice: There are N dice, and the i-th die has its faces numbered from 1 to k i where each k i is a positive integer less than K . Assuming that on each die, each number is equally likely, we want to find the probability that when we roll all N dice, the sum of the numbers is exactly T . To do so, we need to count exactly how many ways we can choose integers d 1 , d 2 , ..., d n with d i ≤ k i , such that ∑ d i = T . (The probability is this count divided by the product of the....
View Full Document

## This note was uploaded on 03/16/2010 for the course CSE 101 taught by Professor Staff during the Winter '08 term at UCSD.

### Page1 / 6

sortp - Design and Analysis of Algorithms Divide and...

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

View Full Document
Ask a homework question - tutors are online