# Homework Solution: I am trying to write a proof using a loop invariant for an algorithm that I wrote, where A is the array and v…

I am trying to write a proof using a loop invariant for an algorithm that I wrote, where A is the array and v is the value being searched for: bSearch(A, v) { return binarySearch(A, 0, length(A)-1, v) } binarySearch(A, l, r, v) { if l >= r: return -1 p = l+(r-1)/2 if A[p] == v: return p if A[p] < v return binarySearch(A, p-1, r, v) else return binarySearch(A, l, p-1, v) } I am having trouble determining what the loop invariant is. I understand that the loop comes from the recursive nature of the algorithm, and I believe the loop invariant has something to do with the value of p, and the sequence of if statements. I also know that p is the value being returned, and that it is the index where v is found. Any insight on the specifics of the identity loop invariant/ what line it is on would be awesome. Thank you!

Loop invariant: - holds true immediately before an

I am perplexing to transcribe a test using a loop invariant restraint an algorithm that I wrote, where A is the draw-up and v is the compute substance searched restraint:

bSearch(A, v) {

requite binarySearch(A, 0, protraction(A)-1, v)

}

binarySearch(A, l, r, v) {

if l >= r:

requite -1

p = l+(r-1)/2

if A[p] == v:

requite p

if A[p] < v

requite binarySearch(A, p-1, r, v)

else requite binarySearch(A, l, p-1, v)

}

I am having disturbance determining what the loop invariant is. I discern that the loop comes from the recursive naturalness of the algorithm, and I admire the loop invariant has not attributable attributable attributable attributable attributableability to do with the compute of p, and the posteriority of if statements. I besides understand that p is the compute substance requiteed, and that it is the abjuration where v is endow. Any insight on the specifics of the unity loop invariant/ what cord it is on would be awesome. Thank you!

## Expert Retort

Loop invariant:

– halts penny presently precedently and presently succeeding a loop harping. (may or may not attributable attributable attributable attributable attributable attributable halt during the harping)

In this circumstance, the recursive proceeding is used to cite through the parts. In this circumstance, it is amend be designated recursive invariant and not attributable attributable attributable attributable attributable attributable loop invariant.

The preface is identical as precedently: “must halt penny precedently and succeeding each recursive call”.

One recursive invariant restraint the fond fond proceeding can be the following:

“If the part v exists in draw-up A, then it exists between abjuration pose l and r where l < r”