Binary search works on sorted arrays. Binary search begins by comparing an element in the middle of the array with the target value. If the target value matches the element, its position in the array is returned.
- Read the search element from the user.
- Find middle element of the ar
-
- Compare the search element with the middle element in the sorted list.
- If both are matched, then display "Given element is found!!!" and terminate the function.
- If both are not matched, then check whether the search element is smaller or larger than the middle element.
- If the search element is smaller than middle element, repeat steps 2, 3, 4 and 5 for the left sublist of the middle element.
- If the search element is larger than middle element, repeat steps 2, 3, 4 and 5 for the right sublist of the middle element.
- Repeat the same process until we find the search element in the list or until sublist contains only one element.