CSE 2320 Section 060/061 Spring 1999

Homework 1

Due: February 4, 1999, in class (February 5, 1999, 5:00pm for -10%)

1.
Design and analyze an iterative algorithm called LINEAR-SEARCH(A,v,n) that returns the index i of the first occurrence of v in the integer array A[1..n], or returns -1 if vdoes not occur in A. Specifically,

(a)
Give pseudocode for your iterative LINEAR-SEARCH(A,v,n) algorithm.

(b)
Perform a line-by-line analysis of your algorithm and derive a precise expression T(n) for the running time. You may assume that each line of pseudocode takes unit time (i.e., ci = 1).

(c)
Describe the situation resulting in the best-case running time of your algorithm and derive an expression for T(n) in this case. Also, give an asymptotically-tight bound for T(n).

(d)
Describe the situation resulting in the worst-case running time of your algorithm and derive an expression for T(n) in this case. Also, give an asymptotically-tight bound for T(n).

2.
Prove that your asymptotically-tight bound in part 1d is correct by finding the constants c1, c2 and n0 from the definition of .

3.
Prove that n2 is an asymptotic lower bound for n4 by finding the constants c and n0 from the definition of .

4.
Prove that n2 is not an asymptotic upper bound for n4 by showing that no constants c and n0 exist satisfying the definition of O.

5.
Referring to the above search problem, note that if the array Awere sorted in increasing order, we can check the midpoint of the sequence against 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.

(a)
Write pseudocode for a recursive version of the BINARY-SEARCH(A,v,p,r) algorithm, which searches for v in the sequence . To search the entire sequence A, the initial call to the algorithm would be BINARY-SEARCH(A,v,1,n). The algorithm should still return -1 if v is not found. NOTE: You may not use global variables in your algorithm.

(b)
Perform a line-by-line analysis of your algorithm and derive a recurrence T(n) for the worst-case running time, where n = r - p + 1. You may assume that each line of pseudocode takes unit time (i.e., ci = 1), except, of course, those lines containing recursive calls.

(c)
Argue that the worst-case running time of BINARY-SEARCH is , where .