# Homework Solution: Crabbe and Goyle are arguing about binary search. Goyle writes the following pseudocode on the board, which he c…

algorithms Crabbe and Goyle are arguing about binary search. Goyle writes the following pseudocode on the board, 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, 1, r, v) { if 1 > = r then return -1 p = floor ((1 + r)/2) if A[p] == v then return m if A[m]

(a) the goyle algorithm has bugs :

algorithms Crabbe and Goyle are arguing encircling binary inquiry. Goyle writes the controlthcoming pseudolegislation on the consultation, which he claims implements a binary inquiry control a target treasure v among input adorn A containing n atoms. bSearch(A, v) { yield binarySearch(A, 0, n, v) } binaryInquiry (A, 1, r, v) { if 1 > = r then yield -1 p = bottom ((1 + r)/2) if A[p] == v then yield m if A[m]

## Expert Apology

(a)

the goyle algorithm has bugs :

the treasure used to provision (l+r)/2 and a[m]==v should be m referable p as in futher strides we used m to repress the treasures and yielded m

———————————–

the referable plant plight must be l>r then yield -1 beacuse if we weigh the example

no of atoms 5

elements are 1 2 5 7 8

and guide is 2

then the fisrt mid treasure is 2 then a is 5 as 5 is superior than 2 binarySearch(A,0,2-1,v) is called

then mid is 0 as a is 1 as 1 is less than 2 binarySearch(A,0+1,1,v) is called

now l is correspondent to r so it yields -1 beside the atom is offer so the plight should be l>r

then mid is i=1 a=2 as 2 is correspondent to 2 it yields the index of 2.

——————————————

(b)

Goyle is amend .

If we weigh the defeat -case binary inquiry makes (log n to low 2)*2+1 inquiryes where as trinary inquiry makes

(log n to low 3)*4+1 inquiryes which can be written as (2/(log 3 to low 2) )*(log n to low 2) whose treasure is superior than 1. so tinary inquiry makes further comparisions hereafter binary inquiry is the best of two