Data Structure (2130702)

BE | Semester-3   Summer-2019 | 06/04/2019

Q3) (b)

Write the algorithm for binary search.

# Input: Sorted Array A, integer key
# Output: first index of key in A, or -1 if not found

Algorith: Binary_Search (A, left, right)

while left <= right
  middle = index halfway between left, right
  if D[middle] matches key
   return middle
  else if key less than A[middle]
   right = middle -1
  else
   left = middle + 1
return -1