Binary Search Program
Binary search works as follows.
-
A given key value is searched for among the values stored in the array.
-
Two index variables lowIdx and highIdx
are maintained to limit the scope where the value searched for may exist.
-
The search proceeds until the element searched for is found
or the scope limited by lowIdx and highIdx is exhausted.
-
The index currentIdx for the middle position is computed as
(lowIdx + highIdx) / 2 .
-
If key = data[currentIdx], search is successful.
-
If key < data[currentIdx], highIdx is lowered to
currentIdx.
-
If key > data[currentIdx], lowIdx is raised to
currentIdx.
-
If the scope limited by lowIdx and highIdx is exhausted,
search is unsuccessful.
Source Code of the Binary Search Program
Source Code of the Binary Search Program
(Graphical Version)
Back
Home