eguruchela

What is binary search and write its logic


Answer:

Binary Search is a one of the efficient searching algorithm used in a sorted array by repeatedly dividing the search interval in half.

The binary search is to use the information that has sorted array and reduce the time complexity to O(Log n).

Logic

Begin with the middle element of the whole array as a search key.

If the value of the search key is equal to the item then return an index of the search key.

Or if the value of the search key is less than the item in the middle of the interval, narrow the interval to the lower half.

Otherwise, narrow it to the upper half.

Repeatedly check from the second point until the value is found or the interval is empty.

Binary Search Algorithm can be implemented in two ways (Iterative Method or Recursive Method)

👈       👉