Consider the algorithm findMax that finds the maximum entry in an integer array.
Algorithm findMax($A$)
Input: An integer array $A$
Output: The maximum entry of $A$
1. m <- A[0]
2. for i <- 1 to A.length - 1 do
3. if A[i] > m then
4. m <- A[i]
5. return m
Show that there are constants $c_1,d_1,c_2,d_2 \in \mathbb{N}$ such that $c_1n + d_1 \leq T_{findmax(n)} \leq c_2n + d_2$ for all $n$, where $n$ is the length of the input array.
Argue that for every algorithm A for finding the maximum entry of an integer array of length $n$ it holds that $T_A$$(n)$ $\geq n$.
NOTE: The (worst-case) running time of an algorithm A is the function $T_A: \mathbb{N} \rightarrow \mathbb{N}$ where $T_A$$(n)$ is the maximum number of computation steps performed by $A$ on an input of size $n$.
- My intuition was that $c_1$ and $c_2$ correspond to the number of times the next element of the array is greater than the current maximum, but I don't see how to fit it in the equation.
1 - executed $1$ time
2 - executed $n$ times
3 - executed $(n-1)$ times
4 - executed $c$ times - - - best case: $0$ times / worst case: $(n-1)$ times
5 - executed $1$ time
In the total sum of steps:
- Step 1 + Step 5 = 2 times
- Steps 2,3,4 $\leq 3n$ times
Therefore, $c_1 = 1$, $d_1 = 2$, $c_2 = 3n$, $d_2 = 2$.
- For the second question, would it be enough to show that the best-case scenario (array sorted in non-increasing order) takes at least $n$ steps?