Binary Search

Binary Search :

ALGORITHM:

    1. Input array of size n, A[n].
    2. Input the element to search for, say, num
    3. set lb=0
      set ub=(n-1)
      set mid=(lb+ub)/2
    4. while (lb<=ub)
      if (A[mid] < num)
      SET lb= mid + 1
      else if (A[mid] == num)
      PRINT “Element found” and its location and BREAK loop
      else if(A[mid] > num)
      SET ub = mid – 1;
      else if(ub==lb+1)
      PRINT “element not found” and BREAK the loop.
      SET mid = (lb + ub)/2;
    5. EXIT

C SOURCE CODE:

Leave a Reply

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