Q27. Write a code to search element in array using binary search algorithm

Binary Search :- Binary search is a fast search algorithm with run-time complexity of search O(log n). For this algorithm to work properly, elements must be in a sorted form.

Binary Search Tree Algorithm

START
do until the pointers low and high meet each other.
mid = (low + high)/2
if (x == arr[mid])
return mid
else if (x > A[mid]) // x is on the right side
low = mid + 1
else // x is on the left side
high = mid – 1
STOP

Code Binary Search Tree

Output

Element is found at index 3

Recommended Programs
Program to find factorial of a number
Program to count number of digits in a number