algorithms – Coming up with loop invariants involving k?


void min(arr(), n){
for(i = 0;i < n; ++i)
{
   // Change < to > if you want to find the smallest element
   if(arr(i) > arr(i+1))
       arr(i) = arr(i+1);
}

To avoid wording about a “just check my work” question. I will try to explain what I do not understand about this topic of loop invaraints. I have been having the hardest time coming up with the beginning loop variant with respect to the variables and the what they mean in relation to the arrays. So, my question about this code loop invariant is that this program finds the smallest element in the array. My loop invaraiant is At the start of the iteration when i = k, the item at index k is the smallest item in the range from zero to k. Can you tell me both if I have the correct loop invariant and also am I understanding the concept correctly. I believe that I need to relate such variables such as i, and j in loops with the varaible k in relation to what the program is doing. Thank you.