g(n) =
4n20 +5n2 +4
111
52n.
g(n) = log(n372 ).
g(n) = n2 5n .
g(n) = n(n ) .
(a) (2 points) Both of these procedures return TRUE if and only if the same condition holds on
the arrays A and B holds. Describe this condition (in words).
Solution: Both methods reurn ture if every element in A is less than every element in B.
(b) (5 points) Analyze the worst-case running time for both algorithms using the -notation.
Solution: Both methods worst case runtime is (n2 )
(c) (3 points) Does the situation change if we consider the best-case running time for both
algorithms?
Solution: Yes, magic-1s best case runtime is (1) while magic-2s is (n)
RecSec(A, key)
if key == 1
return
RecSec(A, key 1)
int max=key
for key to 0
if A[i] > A[key]
max=i
swap A[key] and A[max]
T (n) = n + T (n 1) if n = 1 T(n)=1
n + n 1 + n 2 + ...1 = n(n+1)
2