Linear Search Recursive Runtime

78 Views Asked by At

Assume this is the pseudocode of a Linear Search:
LS(Array, i, obj) if i == Array return false else return Array[i] == obj or LS(Array, i+1, obj)
For $i = len(A) - n$ Wouldn't the $T(n)$ be:
$$ T(n) = \begin{cases} 1 & n=0 \\ 1 + T(n+1) & n > 0 \end{cases} $$ And how would you find its closed form?

1

There are 1 best solutions below

0
On

One mistake, $T$ is based on size of the remaining input, so the recurrence should be $$ T(n) = \begin{cases} 1 & n=0 \\ 1 + T(n-1) & n > 0 \end{cases} $$