Input: A sequence of n numbers A = (a1, a2, …, an) and a value v. Output: An ind
ID: 3782616 • Letter: I
Question
Input: A sequence of n numbers A = (a1, a2, …, an) and a value v.
Output: An index i such that v = A[i] or the special value NIL if v does not appear in A
a) (10 points) Write the pseudo code for a linear search. The linear search scans through a sequence looking for v. (Assume that indexing starts at 1 since the book uses this in its discussions of lists and sequences.)
b) (10 points for loop invariant; 5 points for each property) Using a loop invariant, prove that your algorithm is correct. Make sure that your loop invariant fulfills the three necessary properties. (Be sure to discuss all three properties.)
Explanation / Answer
USING LOOP VARIANT TO PROVE ALGORITHM IS CORRECT :
In the case of linear search, the loop variant will be the backing store used for saving the index(output) .
Hence we can prove that our algorithm is correct.