Question 1. (5 points) Write pseudocode to find the smallest integer ai in a sequence [an].
procedure find-min (a: array [1..n] of integer):
min := a[1]
location := 1
for i = 2 to n do:
if (a[i] < min) then
min := a[i]
location := i
endif
endfor
return(location,min)
end.
Question 2. (5 points) Find the complexity of the following algorithm given a sequence of positive integers [an] containing elements ai, i = 1..n:
STATEMENT MAX # OF OPS
p := 0 1
i := 1 1
while ( i <= n) do: n+1
{ p := p * a[i]
i := i + 1 2n
}
return(p) 1
Copyright © 1999 by Mark S. Schmalz.