This preview has intentionally blurred sections. Sign up to view the full version.
View Full 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: STOOGESORT ( 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 STOOGESORT ( A, i, j k ) 7 STOOGESORT ( A, i + k, j ) 8 STOOGESORT ( A, i, j k ) (a) Argue that STOOGESORT ( A, 1 , length [ A ]) correctly sorts the input array A [1 ..n ] , where n = length [ A ] (b) Give a recurrence for the worstcase running time of STOOGESORT and a tight asymptotic (Θnotation ) bound on the worstcase running time. (c) Compare the worstcase running time of STOOGESORT with that of insertion sort, merge sort, heapsort, and quicksort. Do the professors deserve tenure? 2. kway 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 kway merging. Analyze the time complexity of your algorithm. Prove a tight lower bound on the number of comparisons for kway merging. 3. Maxsum: Use the divideandconquer 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 worstcase lineartime 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 multifaceted dice: There are N dice, and the ith 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.
 Winter '08
 staff
 Algorithms, Sort

Click to edit the document details