hw1-sol - COT 5405 - Fall 2010 Homework #1 Solutions...

Info iconThis preview shows pages 1–4. Sign up to view the full content.

View Full Document Right Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the 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.3-4, 2.3-5, 2.3-6 ) 2.3-4: 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.3-5: Referring back to the searching problem (see Exercise 2.1-3), 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 worst-case 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. ITERATIVE-BINARY-SEARCH ( 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 ITERATIVE-BINARY-SEARCH ( 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. RECURSIVE-BINARY-SEARCH ( 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 RECURSIVE-BINARY-SEARCH ( A,v,mid + 1 ,high ) else return RECURSIVE-BINARY-SEARCH ( A,v,low,mid- 1) The initial call is ITERATIVE-BINARY-SEARCH ( 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.

Page1 / 11

hw1-sol - COT 5405 - Fall 2010 Homework #1 Solutions...

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

View Full Document Right Arrow Icon
Ask a homework question - tutors are online