Binary search using recursion

Binary Search :

For Binary search, array must input in sorted order.Here, it sould be in ascending order.
ALGORITHM:
 In this algorithm, function bsearch() takes the parameters array,its size, lower and upper bound of array and returns the location of the element ,if present, otherwise, returns -1.
BSEARCH(int a[10],n,ub,lb)
    1. [INITIALIZE] mid=(lb+ub/2)
    2. if(x[mid]==n)
      return mid;
      else if(x[mid]>x[lb])
      return bsearch(x,n,ub,mid);else if(x[mid]<x[ub])
      return bsearch(x,n,mid,lb);
      else
      return -1;
      }
C SOURCE CODE:

Leave a Reply

Your email address will not be published. Required fields are marked *