Sequential & Binary Search
Sequential & Binary Search
• BINARY SEARCH
SEQUENTIAL SEARCH
When data items are stored in a collection such as a list, we
say that they have a linear or sequential relationship.
• The function needs the list and the item we are looking for
and returns a Boolean value as to indicate whether it is
present or not.
If the item we are searching for is greater than the middle item, we know that
the entire lower half of the list as well as the middle item can be eliminated
from further consideration.
The item, if it is in the list, must be in the upper half. We can then repeat the
process with the upper half. Start at the middle item and compare it against
what we are looking for. Again, we either find it or split the list in half,
therefore eliminating another large part of our possible search space
Binary Search Example
Consider the following array of sorted integers: 10, 15, 25, 30, 33, 34, 46, 55, 78,
84, 96, 99 Using binary search algorithm, search for 23. Show the sequence of
array elements that are compared, and for each comparison, indicate the values of
low and high