This preview shows pages 1–4. Sign up to view the full content.
This preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full Document
Unformatted text preview: COT 5405  Fall 2010 Homework #1 Solutions September 9, 2010 Grading Policy: Please contact TAs by email (cot5405fa10@cise.ufl.edu) or office hours for any grading issues. Maximum score is 100 points. Each completed question with honest effort worth 10 points. Problem 3 is graded for 25 points, problem 5 is graded for 25 points. Partial credit is given if you dont answer a question completely. Please notify the TA if you find anything wrong in this solution. 1 Problem 1 ( 2.34, 2.35, 2.36 ) 2.34: Insertion sort can be expressed as a recursive procedure as follows. In order to sort A [1 ..n ] , we recursively sort A [1 ..n 1] and then insert A [ n ] into the sorted array A [1 ..n 1] . Write a recurrence for the running time of this recursive version of insertion sort. Sol: Since it takes ( n ) time in the worst case to insert A [ n ] into the sorted array A [1 ..n 1] , we get the recurrence T ( n ) = ( (1) if n = 1 T ( n 1) + ( n ) if n > 1 The solution to this recurrence is T ( n ) = ( n 2 ) . 2.35: Referring back to the searching problem (see Exercise 2.13), observe that if the sequesnce A is sorted, we can check the midpoint of the sequence againest v and eliminate half of the sequence from further consideration. Binary search is an algorithm that repeats this procedure, halving the size of the remaining portion of the sequence each time. Write pseudocode, either iterative or recur sive, for binary search. Argue that the worstcase running time of binary search is ( lgn ) . Sol: Iterative method; Input: A sorted array A [1 ..n ] , a value v and a range [ low..high ] Output: Either an index i such that A [ i ] = v, or NIL if no entry of A [ low..high ] contains the value v. ITERATIVEBINARYSEARCH ( A,v,low,high ) while low high do mid b ( low + high ) / 2 c if v = A [ mid ] then return mid if v > A [ mid ] then low mid + 1 else high mid 1 return NIL The initial call is ITERATIVEBINARYSEARCH ( A,v, 1 ,n ) . 2 Recursive method; Input: A sorted array A [1 ..n ] , a value v and a range [ low..high ] Output: Either an index i such that A [ i ] = v, or NIL if no entry of A [ low..high ] contains the value v. RECURSIVEBINARYSEARCH ( A,v,low,high ) if low > high then return NIL mid b ( low + high ) / 2 c if v = A [ mid ] then return mid if v > A [ mid ] then return RECURSIVEBINARYSEARCH ( A,v,mid + 1 ,high ) else return RECURSIVEBINARYSEARCH ( A,v,low,mid 1) The initial call is ITERATIVEBINARYSEARCH ( A,v, 1 ,n ) . Both procedures terminate the search unsucessfully when the range is empty and terminate it successfully if the value v has been found. Based on the com parison of v to the middle element in the searched range, the search contin ues with the range halved. The recurrence for these procedure is therefore T ( n ) = T ( n/ 2) + (1) , whose solution is T ( n ) = (lg n ) ....
View
Full
Document
This note was uploaded on 01/15/2012 for the course COT 5405 taught by Professor Ungor during the Fall '08 term at University of Florida.
 Fall '08
 UNGOR
 Algorithms

Click to edit the document details