Program Statement :
Write a C program to find a particular element from an array using binary search method.

Theory :
Searching is an operation which finds the location of a given element in a list. The search is said to be successful or unsuccessful depending on whether the element that is to be searched is found or not.
Binary search method is very fast and efficient. This method requires that the list of elements be in sorted order. In this method, to search an element we compare it with the element present at the center of the list. If it matches the search is successful. Otherwise the list divided into two halves :-
1. One from 0th element to the center element (whose elements are smaller than the center element).
2. Another one is from center element to the last element (whose elements are greater than the center element).
Now if the searching element is smaller than the center element then searching will be done in the first half, otherwise in the second half.
This procedure is repeated till the element is found or division of half parts gives one element.
Suppose an array consists of 10 elements and 52 is the element that is to be searched.
It works as follows:-

Discussions :
● COMPLEXCITY OF BINARY SEARCH :
In the best possible case, the ITEM may occur at middle position. In this case, the search operation terminates in success with just one comparison.
After each comparison either the search terminates successfully or the size of the array remaining to be searched is about one half of the original size. So after j comparisons the array remaining to be examined is of size at most N/2j . In the worst case the search terminates when there are only one element remaining to be searched. Hence,
N/2j = 1
Or, N = 2j
Or, N = log2N
Thus in worst case, this method requires O(log2N) comparisons to search an element in the array. The maximum number of comparisons in binary search is limited to log n.
● ADVANTAGE :
The advantage of binary search method is that, in each iteration it reduces the number of elements to be searched from n to n/2. On the other hand, linear search method checks sequentially for every element, which makes it inefficient.
● DISADVANTAGE :
The disadvantage of binary search is that it works only on sorted lists. So when searching is to be performed on unsorted list then linear search is the only option.
● Alternatively if user input is an unsorted list then it can be made sorted by using any sorting method and after that we can imply binary search method on the list. But it is not feasible as complexity of the program will become much higher.