Binary Search in C

Here you will get program for binary search in C.

Binary search algorithm can be applied on a sorted array to search an element. Search begins with comparing middle element of array to target element. If both are equal then position of element is returned. If target element is less than middle element of array then upper half of array is discarded and again search continued by dividing the lower half. If target element is greater than middle element then lower half is discarded and search is continued in upper half.

Binary Search in C

Worst Case Time Complexity: O(log n)

Best Case Time Complexity: O(1)

Also Read: Linear Search in C

Program for Binary Search in C

Below program shows the implementation of binary search algorithm in C.

Output

Enter size of array:6

Enter array element(ascending order)
20 27 40 50 58 99

Enter the element to search:27

Element found at position 2

Leave a Reply

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