CSE 2320 Section 060/061 Spring 1999

**Homework 1**

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

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*v*does 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.,*c*_{i}= 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
*c*_{1},*c*_{2}and*n*_{0}from the definition of . - 3.
- Prove that
*n*^{2}is an asymptotic lower bound for*n*^{4}by finding the constants*c*and*n*_{0}from the definition of . - 4.
- Prove that
*n*^{2}is*not*an asymptotic upper bound for*n*^{4}by showing that no constants*c*and*n*_{0}exist satisfying the definition of*O*. - 5.
- Referring to the above search problem, note that if the array
*A*were 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.,*c*_{i}= 1), except, of course, those lines containing recursive calls. - (c)
- Argue that the worst-case running time of BINARY-SEARCH is
,
where
.