0% found this document useful (0 votes)
90 views

Sequential & Binary Search

The document discusses sequential and binary search algorithms. Sequential search linearly searches through a list to find an item. It checks each item in order until the item is found or all items are checked. Binary search searches a sorted list by comparing the middle item and eliminating half of the remaining list based on if the item is lower or higher than the middle. It repeatedly halves the search space to find the item faster than sequential search.

Uploaded by

Dan Joshua
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
90 views

Sequential & Binary Search

The document discusses sequential and binary search algorithms. Sequential search linearly searches through a list to find an item. It checks each item in order until the item is found or all items are checked. Binary search searches a sorted list by comparing the middle item and eliminating half of the remaining list based on if the item is lower or higher than the middle. It repeatedly halves the search space to find the item faster than sequential search.

Uploaded by

Dan Joshua
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 13

Searching

Sequential & Binary Search


SEARCHING

 Searching is the algorithmic process of finding a particular


item in a collection of items.

 A search typically answers either True or False as to say


whether the item is present or not.
TYPES

• SEQUENTIAL /LINEAR 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.

 Each data item is stored in a position relative to the others.

 In Python lists, these relative positions are the index values


of the individual items. Since these index values are ordered,
it is possible for us to visit them in sequence.

 This process gives rise to our first searching technique,


the sequential search
Sequential Search

Starting at the first item in the list, we simply move from


item to item, following the underlying sequential ordering
until we either find what we are looking for or run out of
items. If we run out of items, we have discovered that the
item we were searching for was not present.
EXAMPLE
Python Implementation

• 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.

• The Boolean variable found is initialized to False and is


assigned the value True if we discover the item in the list.
Solved Program
Consider the following list of numbers: 4, 2, 7, 5, 12, 54, 21, 64, 12,
32 Using linear search algorithm, search for the number 54 and show
the position for the same number.
Solved Program
When the city planners developed your neighborhood, they accidentally numbered
the houses wrong. As such, the addresses of the houses on your street are in a
random order. How does the postman find your house using a linear search
method?
BINARY SEARCH
• In the sequential search, when we compare against the
first item, there are at most n−1 more items to look
through if the first item is not what we are looking for.

• Instead of searching the list in sequence; binary


search will start by examining the middle item. If that
item is the one we are searching for, we are done.
If it is not the correct item, we can use the ordered
nature of the list to eliminate half of the remaining
items.
BINARY SEARCH
Algorithm can quickly find the value 54

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

You might also like