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

    algorithms
    4. (15 pts) 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) f return binarySearch(A, 0, n, v) binarySearch (A, 1, r, v) f if 1r then return -1 p floor i r)/2) if Alp]v then return m if Am] < v thern return binarySearch(A, m+1, r, v) else return binarySearch (A, 1, m-1, v) (a) Help Crabbe determine whether this code performs a correct binary search. If it does, prove to Goyle that the algorithm is correct. If it is not, state the bug(s) give line(s) of code that are correct, and then prove to Goyle that your fixed algorithm is correct. (b) Goyle tells Crabbe that binary search is efficient because, at worst, it divides the remaining problem size in half at each step. In response Crabbe claims that nary search, which would divide the remaining array A into thirds at each step, would be even more efficient. Explain who is correct and why.
    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]

    Expert Answer

     
    (a) the goyle algorithm has bugs :

    algorithms

    4. (15 pts) 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) f yield binarySearch(A, 0, n, v) binaryInquiry (A, 1, r, v) f if 1r then yield -1 p bottom i r)/2) if Alp]v then yield m if Am] < v thern yield binarySearch(A, m+1, r, v) else yield binaryInquiry (A, 1, m-1, v) (a) Help Crabbe determine whether this legislation performs a amend binary inquiry. If it does, establish to Goyle that the algorithm is amend. If it is referable, say the bug(s) afford row(s) of legislation that are amend, and then establish to Goyle that your unwandering algorithm is amend. (b) Goyle tells Crabbe that binary inquiry is fruitful consequently, at defeat, it divorces the fostering tenor extent in half at each stride. In defense Crabbe claims that nary inquiry, which would divorce the fostering adorn A into thirds at each stride, would be equable further fruitful. Explain who is amend and why.

    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[2] is 5 as 5 is superior than 2 binarySearch(A,0,2-1,v) is called

    then mid is 0 as a[0] 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[1]=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