Hi I cannot understand why the best case for line 3 is
n-1 and why it isnt just always
I tried to write this in python to understand when it would be
n-1 and the answer is always
n. I am assuming that the loop counter in pseudo-code is evaluated one more time than the body is executed as is said in the Cormen book.
My thinking is that if the length of
n is 1, the loop evaluation is still tested so regardless the answer would still be
n, even if the loop body itself did not run.
Max(A) 1 MaxInt = A 2 index = 2 3 while index ≤ n // Why is the best n-1 here? 4 if MaxInt < A[index] 5 MaxInt = A[index] 6 index = index+1 7 return MaxInt
I know it has to be something very simple but I cant seem to work it out as testing with 1 or 1000 the answer I get back for a count is always
n-1 (0, 999) and if I assume that the loops asserts a final time that is + 1, so
0 + 1 = 1, which is
999 + 1 = 1000 which is