Binary Search Program, Algorithm & their Time Complexity

Binary Search Program, Algorithm & their Time Complexity. In this tutorial, You’ll learn about binary search algorithm, how it’s useful in searching and the time complexity of a binary search.

Binary Search

A Binary Search is used to search an element in a sorted array.  Binary search is also known as half-interval search or logarithmic search. It works by comparing a value (value needs to search) to the middle element an array. If a value matches with the middle element then the position is returned otherwise the algorithm repeats if the value is less than or greater than the middle element.

A Binary search algorithm is efficient than the linear search algorithm. The time complexity of binary search is O(log(n)). 

Important Points

i) A Binary search  algorithm is applicable only for sorted values. An array should be sorted  either in ascending or descending order.

ii) Binary search average and worst-case time complexity is O(log(n)).

Linked List Interview Questions

Subscribe Our Tutorials

Get Latest Updates on Facebook

Binary search implementation using Recursion

Binary Search Algorithm

Let’s check the algorithm of binary search.

i) Initialize two indexes low and high. Initialize the value of low to zero and high is n – 1 where n is the length of an array.

low = 0
high = N – 1

Run a loop while low is less than equal to high.

Binary Search Program in PHP

Binary Search Program in C

We have learned about binary search, let’s implement them in C.

Reference Books for Data Structure

For reference, you can read these good books for a data structure.

Data Structure Books on Amazon India

Data Structure Books on Flipkart


About WebRewrite

I am technology lover who loves to keep updated with latest technology. My interest field is Web Development.
Tagged , , , . Bookmark the permalink.