in c Suppose the binary search algorithm is used to find th
(in c++) - Suppose the binary search algorithm is used to find the value 24 in the array shown below. 10 15 17 24 35 36 41 45 48 57 65 68 70 74 80 83 90 What will be the value of the variable last when the binary search algorithm completes?
Solution
Binary search works on sorted arrays. A binary search begins by comparing the middle element of the array with the target value. If the target value matches the middle element, its position in the array is returned. If the target value is less or more than the middle element, the search continues the lower or upper half of the array respectively with a new middle element, eliminating the other half from consideration
Given an array A of n elements with values or records A0 ... An1 and target value T, the following subroutine uses binary search to find the index of T in A.[6]
    Set L to 0 and R to n  1.
     If L > R, the search terminates as unsuccessful. Set m (the position of the middle element) to the floor of (L + R) / 2.
     If Am < T, set L to m + 1 and go to step 2.
     If Am > T, set R to m  1 and go to step 2.
     If Am = T, the search is done; return m.
Then when the algorithm finish will return the position 3
 array[3]=24
 Always return the position of the element to find

