Loading....
Coupon Accepted Successfully!

 

Binary Search

The algorithm is as follows


Input :
 An array A [1..n] of n elements sorted in non-decreasing order and an element x.


Output :
 j if x = A [j], 1 ≤ j ≤ n, and 0 otherwise.

  1. binarysearch (1, n)

Procedure binarysearch (low, high)

  1. if low > high then return 0
  2. else
  3. mid  [(low + high)/2]
  4. if x = A [mid] then return mid
  5. else if x < A [mid] then return binary search (low, mid–1)
  6. else return binarysearch (mid + 1, high)
  7. end if




Test Your Skills Now!
Take a Quiz now
Reviewer Name