# Homework Solution: ements a binary search for a target value v within input array A containing n elements. bSearch(A, v)…

I have a problem which reads that a person writes the following pseudocode, which he claims implements a binary search for a target value v within input array A containing n elements. bSearch(A, v) { return binarySearch(A, 0, n, v) } binarySearch(A, l, r, v) { if l >= r then return -1 p = floor( (l + r)/2 ) if A[p] == v then return m if A[m] < v then return binarySearch(A, m+1, r, v) else return binarySearch(A, l, m-1, v) } Determine whether this code performs a correct binary search. If it does, prove the algorithm is correct. If it is not, state the bug(s), give line(s) of code that are correct, and then prove your fixed algorithm is correct. All I am asking for is help determing which lines are incorrect, and how to resolve them. I know the algorithm is incorrect. As far as I can tell, the only mistake is the variable m not being defined.The solution I believe is to either replace all instances of p with m or vice versa. (They should be the same variable), and I think also the first if statement should be removed, and the entire function's body should be within an if statement which reads: if "(r>= 1", except for the return 0, which should be outside it. I think it is better to put that in the end because then either l>r or the value isn't in the binary array. Right now, the if statement will return -1 even if the array has a size of 1, but we still need to check that value to see if it is v. As you can see, I already have the problem thought out, it is just that it is part one of a multi stage problem, so I want to make sure my answers are correct before moving forward. I would fix the function by writing it as: bSearch(A, v) { return binarySearch(A, 0, n, v) } binarySearch(A, l, r, v) { if r >= l) p = floor( (l + r)/2 ) if A[p] == v then return p if A[p] < v then return binarySearch(A, p+1, r, v) else return binarySearch(A, l, p-1, v) } return -1

The correct algorithm is as follows for the binary search:

I accept a substance which reads that a individual writes the subjoined pseudocode, which he claims implements a binary inquiry restraint a target treasafe v amid inprostrate arrange A containing n elements. bSearch(A, v)

{ reappear binarySearch(A, 0, n, v)

}

binarySearch(A, l, r, v)

{ if l >= r then reappear -1

p = pedestal( (l + r)/2 )

if A[p] == v then reappear m

if A[m] < v then

reappear binarySearch(A, m+1, r, v)

else reappear binarySearch(A, l, m-1, v) }

Determine whether this rule performs a set-right binary inquiry. If it does, examine the algorithm is set-right. If it is referable, narrate the bug(s), imsever succession(s) of rule that are set-right, and then examine your agricultural algorithm is set-right.

Complete I am interrogation restraint is succor determing which successions are inaccurate, and how to expound them. I comprehpurpose the algorithm is inaccurate.

As remote as I can publish, the merely succeed is the mutable m referable nature defined.The disintegration I venerate is to either substitute complete instances of p with m or immorality versa. (They should be the corresponding mutable), and I reckon so the chief if narratement should be removed, and the full capacity’s mass should be amid an if narratement which reads: if “(r>= 1”, bar restraint the reappear 0, which should be withextinguished it. I reckon it is meliorate to prostrate that in the purpose consequently then either l>r or the treasafe isn’t in the binary arrange. Right now, the if narratement get reappear -1 level if the arrange has a greatness of 1, excepting we calm?} lack to bridle that treasafe to perceive if it is v.

As you can perceive, I already accept the substance idea extinguished, it is proper that it is sever individual of a multi quantity substance, so I lack to shape safe my solutions are set-right precedently moving restraintward.

I would plant the capacity by fitness it as:

bSearch(A, v)

{ reappear binarySearch(A, 0, n, v)

}

binarySearch(A, l, r, v)

{ if r >= l)

p = pedestal( (l + r)/2 )

if A[p] == v then reappear p

if A[p] < v then

reappear binarySearch(A, p+1, r, v)

else reappear binarySearch(A, l, p-1, v) }

reappear -1

## Expert Solution

The set-right algorithm is as follows restraint the binary inquiry:

Binary Inquiry Algorithm:

binarySearch(a,l,r,v)

{

if(r >= 1)

{

int m = l+(r-1)/2;

if(a[m] == v)

reappear m;

if(a[m] > v)

reappear binarySearch(a,1,m-1,v);

reappear binarySearch(a,m+1,r,v);

}

reappear -1;

}

This is the set-right algorithm restraint the binary inquirying and the overhead impartn algorithm in the interrogation is inaccurately written. The reappear narratements are injustice and the subject narratements are so inset-right they are comparing among items unnecessarily providing injustice extinguishedput.