I\'m reading \"Introduction to Algorithm\" by CLRS. In chapter 2, the authors mention \"loop invariants\". What is a loop invariant?
In Linear Search (as per exercise given in book), we need to find value V in given array.
Its simple as scanning the array from 0 <= k < length and comparing each element. If V found, or if scanning reaches length of array, just terminate the loop.
As per my understanding in above problem-
Loop Invariants(Initialization): V is not found in k - 1 iteration. Very first iteration, this would be -1 hence we can say V not found at position -1
Maintainance: In next iteration,V not found in k-1 holds true
Terminatation: If V found in k position or k reaches the length of the array, terminate the loop.