Binary search is a procedure for searching an element in an
array. The array has to be sorted in ascending order.
The basic idea is to compare the middle element of the array with
the element to be found. The comparison can yield three results:
the middle element is the same as the element to be
found
In this case the search is successful and the returns the index
(field position) of the element to be found.
the middle element is smaller than the element to be
found
The search is then continued recursively to the right of
the element.
the middle element is greater than the element to be found
The search is then continued recursively to the left of the element.
With sorted data the binary search has a complexity of O(log n).
The animation demonstrates the course of a failed binary search
and shows the source code. To better understand the procedure the
current program line is highlighted.
Screen Shot(s):
Start with the middle element
Comparison with second chosen element
Comparison with third chosen element - too small, continue "right"