Binary search

Binary search is higher than the linear search. However, it cannot be tested on the unsorted data structure. The binary search is based on the method divide-and-conquer. The binary search begins by testing the data in the middle component of the array. This determines an object is whether in the first half or second half. If the object is in the first half, we do not need to check the second half and if it is in the second half no need to check in the first half. Similarly, we repeat this procedure until we find an object in the list or not found from the list. Here we need three variables to identify first, last and middle elements.

To implement a binary search technique, the item must be in sorted order.
Binary Search is performed as follows:
The key is compared with an element in the middle position of an array
If the key matches with an element, return it and stop.
If the key is less than mid-position element, then the element to be found must be in the first half of the array, otherwise, it must be in the second half of the array.
Repeat the method for the lower (or upper half) of the array until the component is found.

Binary search, also known as half-interval search,[1] logarithmic search,[2] or binary chop,[3] is a search algorithm that finds the position of a target value within a sorted array.[4][5] Binary search compares the target value to the middle element of the array. If they are not equal, the half in which the target cannot lie is eliminated and the search continues on the remaining half, again taking the middle element to compare to the target value, and repeating this until the target value is found. If the search ends with the remaining half being empty, the target is not in the array.


Binary Search: Search a sorted array by repeatedly dividing the search interval in half. Begin with an interval covering the whole array. 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 until the value is found or the interval is empty.The idea of binary search is to use the information that the array is sorted and reduce the time complexity to O(Log n).
Posted on by