Loop invariants

“An algorithm is a well-defined computational procedure that takes some value, or set of values, as input and produces some values, or set of values, as output.”

-from Introduction to algorithms (CLRS)


// the Loop Invariant must be true here
for ( TEST CONDITION ) {
// top of the loop


// bottom of the loop
// the Loop Invariant must be true here
}
//insertion sortfor (i = 1 to n-1)
{
key = arr[i];
j = i-1;
//insert A[i] into the sorted sequence A[0 .. i-1] while (j >= 0 and arr[j] > key)
{
arr[j+1] = arr[j];
j = j-1;
}
arr[j+1] = key;
}
I designed this using Canva. Hope you liked it. :-)

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store