Searching
Introduction to Python
Starter
• Think of a number between 1 and 100
• Can you think of a way to guess the number the
fastest?
• Try this with the person sat next to you.
Learning Objective Success Criteria
• Compare alternative • Use a linear search to find a
number
algorithms for a given
problem – using searches • Understand how a binary search
works
• Understand Random searches
Introduction to Python
Searching
Searching methods with an
ordered list
• Linear Search
• Random Search
• Binary Search
Introduction to Python
Searching
Linear search: Pseudocode
• Each item in the list is examined one at a time from
the beginning
read the first item in list
while not end of list
read next item in list
if item found then
display item
Introduction to Python
Searching
Random search: Pseudocode
• Each item in the list is selected randomly and
checked until the correct item is found
n := random value between 1 – (list length)
read the nth item in list
while item not found
n := random value between 1 – (list length)
read the nth item in list
print found item
Introduction to Python
Searching
Efficiency of code
• Which of these two search methods will result in the
least amount of processing?
• Is there a better way?
• How would you find an
item in a sorted list?
• How would you find
“Monkey” in the
dictionary?
Introduction to Python
Searching
Binary search
• How would you get a computer to guess a
number you were thinking of between 1 and 100?
Introduction to Python
Searching
Binary search technique
• Find the midpoint of the range 1-100
• If 50 is too low bisect the new range 51-100, and
guess 75, etc until the number is correctly guessed
• The maximum number of guesses needed for a
number of 2n is n
• E.g. 100 is between 26 and 27 so maximum number
of guesses needed is 7
Introduction to Python
Searching
Plan your code
• Complete the Binary Search Task – MS Teams
• Use pseudocode to help you plan your binary search
Introduction to Python
Searching
Plenary
• How many types of Searches are there?
• Which one is the most efficient and why?
• In Binary searches, what is the maximum number of
tries it will take to guess a number that is between 1
- 1000