HW5 - (b) Give an algorithm for sorting a list of n keys...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
Fall Semester 2008 CS300 Algorithms Homework #5 (due 11/7) 1. Assume that a list of distinct keys is sorted in the decreasing order. You want to sort them in increasing order by Heapsort. (a) How many comparisons of keys are done in the heap construction phase if there are 10 keys? (b) How many are done if there are n keys? Show how you drive your answer. (c) Is this list in the decreasing order a best case for the heap construction? Why or why not? 2. Throughout most of sorting methods, we have assumed that the keys in the list to be sorted were distinct. Often, there are duplicate keys. Such duplication could make sorting easier, but algorithms that were designed for distinct (or mostly distinct) keys may not take advantage of the duplication. Let us consider the extreme case where there are only two possible key values, 0 and 1. (a) What is the order of the number of key comparisons done by InsertionSort in the worst case? (Describe a worst-case input.)
Background image of page 1
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: (b) Give an algorithm for sorting a list of n keys whose member is either 0 or 1 in O(n). (You are allowed to read the list only ONCE.) 3. We are given an n × n array A . A = a 11 a 12 ··· a 1 n a 21 a 22 ··· a 2 n . . . . . . a n 1 a n 2 ··· a nn The elements in each row are sorted, i.e., a i,j ≤ a i,j +1 , for j = 1 , 2 , ··· ,n-1 for each row i , where 1 ≤ i ≤ n . The elements in each column are also sorted, i.e., a i,j ≤ a i +1 ,j , for i = 1 , 2 , ··· ,n-1 for each column j , where 1 ≤ j ≤ n . Propose a method to find the element a ij in A that is equal to a query value x . In other words, your method should report ( i,j ) if x = a ij for some 1 ≤ i,j ≤ n , and (0 , 0), if x is not equal to any element in A . The number of comparisons should be less than or equal to 2 n . 1...
View Full Document

This note was uploaded on 02/04/2010 for the course COMPUTER S cs300 taught by Professor Unkown during the Spring '08 term at Korea Advanced Institute of Science and Technology.

Ask a homework question - tutors are online