{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

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

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

View Full Document Right Arrow Icon
COT 5405 - Fall 2010 Homework #1 Solutions September 9, 2010 Grading Policy: Please contact TAs by email ([email protected]) 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 don’t answer a question completely. Please notify the TA if you find anything wrong in this solution. 1
Background image of page 1

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

View Full Document Right Arrow Icon
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
Background image of page 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 ) . 2.3-6: Observe that the while loop of lines 5-7 of the INSERTION-SORT procedure in Section 2.1 uses a linear search to scan (backward) through the sorted subarray A [1 ..j - 1] . Can we use a binary search (see Exercise 2.3-5) instead to improve the overall worst-case running time of insertion sort to Θ( n lg n ) ?
Background image of page 3

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

View Full Document Right Arrow Icon
Image of page 4
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}