Binary Search In Java
Binary search is used to search an key element from array of elements. Binary search is faster than linear search. It can be used when the elements are in a sorted sequence and all the key elements are unique. It takes O(logn) time where as the linear search takes O(n) time in worst case.
Algorithm:
Compare the middle element with key
If the middle element matches with the key, then return index of middle element
Else if middle elements is greater than key, then key can only be lie on half sub array before the mid so we recursively find the key in half sub array
Else key can be found in half sub array after the mid
