L5 Slides - Algorithms - KS4
L5 Slides - Algorithms - KS4
Binary search
KS4 - Algorithms
Starter activity
For the first attempt you guess 8 and the person replies “lower”.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
Questions:
What number will you guess next?
What would be the maximum number of guesses needed for 15
numbers?
2
Think, write, pair, share.
Objectives
3
Activity 1
Binary search
Binary search is a much more efficient way of searching through a list
of items compared to a linear search.
However, you can only use a binary search algorithm if the data is
ordered i.e. smallest to largest.
If the data that you have is unordered, you must either use a linear
search algorithm or sort the data first.
5
Activity 2
Binary search
Each number is hidden under a cup. The cups are arranged in order
with the lowest value on the left. The number to find is 68.
5 23 28 47 52 68 73 77 90
● Take an ordered list of data and an item that is being searched for
(the search item)
6
Activity 2
Binary search
Each number is hidden under a cup. The cups are arranged in order
with the lowest value on the left. The number to find is 68.
5 23 28 47 52 68 73 77 90
Binary search
Each number is hidden under a cup. The cups are arranged in order
with the lowest value on the left. The number to find is 68.
5 23 28 47 52 68 73 77 90
midpoint
● Find the item in the middle of the range (the midpoint item).
8
Activity 2
Binary search
Each number is hidden under a cup. The cups are arranged in order
with the lowest value on the left. The number to find is 68.
5 23 28 47 52 68 73 77 90
midpoint
● Compare the midpoint item to the item you are searching for.
9
Activity 2
Binary search
Each number is hidden under a cup. The cups are arranged in order
with the lowest value on the left. The number to find is 68.
52 68 73 77 90
midpoint
● If the midpoint item is less than than the search item, change the
range to focus on the items after the midpoint.
The items are arranged in order, so the search item cannot be found before the
midpoint. 1
0
Activity 2
Binary search
Each number is hidden under a cup. The cups are arranged in order
with the lowest value on the left. The number to find is 68.
68 73 77 90
midpoint
● Find the item in the middle of the range (the midpoint item).
If there is an even number of items, select the middle-left item.
1
1
Activity 2
Binary search
Each number is hidden under a cup. The cups are arranged in order
with the lowest value on the left. The number to find is 68.
68 73 77 90
midpoint
● Compare the midpoint item to the item you are searching for.
1
2
Activity 2
Binary search
Each number is hidden under a cup. The cups are arranged in order
with the lowest value on the left. The number to find is 68.
73
midpoint
● If the midpoint item is greater than than the search item, change the
range to focus on the items before the midpoint.
The items are arranged in order, so the search item cannot be found after the midpoint.
1
3
Activity 2
Binary search
Each number is hidden under a cup. The cups are arranged in order
with the lowest value on the left. The number to find is 68.
68
midpoint
● Find the item in the middle of the range (the midpoint item).
If there is only one item left, select this item.
1
4
Activity 2
Binary search
Each number is hidden under a cup. The cups are arranged in order
with the lowest value on the left. The number to find is 68.
68
midpoint
● Compare the midpoint item to the item you are searching for.
1
5
Activity 2
Binary search
Each number is hidden under a cup. The cups are arranged in order
with the lowest value on the left. The number to find is 68.
68
midpoint
● If the item at the midpoint is equal to the search item, then stop
searching.
1
6
Activity 2
Rules: you can only turn over one card at a time. You must turn it back
over after each comparison.
In the previous examples, you saw that with each comparison, the
algorithm eliminates half of the data.
That means if you had 1000 items to search through, it would take at
most 10 comparisons for a binary search to find an item.
If you doubled that number to 2000 items, it would only increase the
most number of comparisons by one!
1
9
Activity 4
2
0
Plenary
Questions:
2
1
Summary
Next lesson