I want to show correctness of "Algorithm to find maximum element in array" using induction and contradiction.
ans=-infinity
for (i=0; i<n; i++)
ans= max(ans, A[i])
where A[0:n-1] is array and max is the function to return maximum of its two arguments.
What I am doing:
Base case: i=0, ans= max(-infinity,A[0])=A[0],
as only one element has been processsed, it is maximum.
Induction Hypothesis: i=k<n-1
, assume the algorithm correctly find maximum upto k iterations.
Inductive Step: i=k+1
, let ans_{i}
denote maximum element obtained by algorithm upto i steps and let ans'_{i}
denote another maximum element from array A[0:i-1]
.
Then from induction hypothsis, ans_{k} = ans'_{k}
Now, for the sake of contradiction, assume ans_{k+1} < ans'_{k+1}
Now, how should I proceed to show this contradiction ?
Any suggestion? Should I change this approach ?