L4 Slides - Algorithms - KS4
L4 Slides - Algorithms - KS4
Linear search
KS4 - Algorithms
Starter activity
Question:
Would the instructions be the same in both cases?
2
Objectives
3
Activity 1
4
Activity 1
When faced with a search problem, you will either have to deal with
ordered or unordered sequences of data.
5
Activity 2
Linear search
Linear search is an algorithm that involves checking each item in a list
or sequence of data one at a time (hence “linear”) to see if it’s the right
item.
If you have a list that is not in order, a linear search is the only
reasonable way to search through it.
In the next slides, you will see an example of how to perform a linear
search.
6
Activity 2
Linear search
Each number is hidden under a cup. The cups are arranged in a
random order. From the cups, the number to find is 126.
● Take a list of data and an item that is being searched for (the search
item).
7
Activity 2
Linear search
Each number is hidden under a cup. The cups are arranged in a
random order. From the cups, the number to find is 126.
current
8
Activity 2
Linear search
Each number is hidden under a cup. The cups are arranged in a
random order. From the cups, the number to find is 126.
current
9
Activity 2
Linear search
Each number is hidden under a cup. The cups are arranged in a
random order. From the cups, the number to find is 126.
current
● If it is not the item you are searching for, go to the next item in the
list.
1
0
Activity 2
Linear search
Each number is hidden under a cup. The cups are arranged in a
random order. From the cups, the number to find is 126.
current
1
1
Activity 2
Linear search
Each number is hidden under a cup. The cups are arranged in a
random order. From the cups, the number to find is 126.
current
● If it is not the item you are searching for, go to the next item in the
list.
1
2
Activity 2
Linear search
Each number is hidden under a cup. The cups are arranged in a
random order. From the cups, the number to find is 126.
current
1
3
Activity 2
Linear search
Each number is hidden under a cup. The cups are arranged in a
random order. From the cups, the number to find is 126.
current
● If it is not the item you are searching for, go to the next item in the
list.
1
4
Activity 2
Linear search
Each number is hidden under a cup. The cups are arranged in a
random order. From the cups, the number to find is 126.
current
1
5
Activity 2
Linear search
Each number is hidden under a cup. The cups are arranged in a
random order. From the cups, the number to find is 126.
current
● If it is not the item you are searching for, go to the next item in the
list.
1
6
Activity 2
Linear search
Each number is hidden under a cup. The cups are arranged in a
random order. From the cups, the number to find is 126.
current
1
7
Activity 2
Linear search
Each number is hidden under a cup. The cups are arranged in a
random order. From the cups, the number to find is 126.
current
● If it is not the item you are searching for, go to the next item in the
list.
1
8
Activity 2
Linear search
Each number is hidden under a cup. The cups are arranged in a
random order. From the cups, the number to find is 126.
current
1
9
Activity 2
Linear search
Each number is hidden under a cup. The cups are arranged in a
random order. From the cups, the number to find is 126.
current
● If the item at the current position is equal to the search item, then
stop searching.
2
0
Activity 2
1. Take a list of data and an item that is being searched for (the search
item)
2. Repeat steps a-c starting from the first item in the list, until you find
the search item or until the end of the list is reached:
a. Compare the item at the current position to the search item
b. If the item at the current position is equal to the search item,
then stop searching.
c. Otherwise, go to the next item in the list.
2
1
Activity 3
Rules: you can only turn over one card at a time. You must turn it back
over after each comparison.
Performance of an algorithm
In the last activity, you may have noticed that the amount of cards you
had to compare to your search card wasn’t always the same.
2
3
Activity 4
Best-case scenario
The best-case scenario occurs when the item you are looking for
results in the smallest possible number of steps.
In the case of linear search, this happens when the item you are looking
for is the very first one in the list.
Best-case
2
4
Activity 4
Worst-case scenario
The worst-case scenario takes place when the item you are looking
for results in the greatest possible number of steps.
With linear search, this happens when the item you are looking for is
the very last one in the list or it isn’t in the list at all.
Worst-
case
2
5
Activity 4
2
6
Plenary
1. What card would you need to search for the best-case scenario to
occur?
2. What card would give you the worst-case scenario?
3. How many comparisons would it take to work out that a card wasn’t
in the set of cards?
2
7
Homework
2
8
Summary
Next lesson
2
9