You can count exactly the operations in this function. First, one must observe that this function finds the smallest element in mylist
between first
and last
.
We can see that return mylist[first]
happens exactly once for each element of the input array, so happens exactly N times overall.
The second return
(ie: return min(...)
) chooses the smallest of the smallest of each half of the array. Each element in the array (except for the smallest element) is on the losing side of this min
exactly once, so this statement occurs exactly N-1 times overall.
There's 2 calls to mystery
for each execution of the second return statement, and none for the first return statement, and each call to mystery
executes one or the other, so the total number of calls to mystery
(including the initial call) is 1 + 2(N-1) = 2N-1.