Data Structure (2130702)

BE | Semester-3   Winter-2016 | 01/02/2017

Q4) (a)

Write an algorithm for Binary search method.

Algorithm
# Input: Sorted Array A, integer key
# Output: first index of key in A, or -1 if not found
Algorithm: 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