AQA 8525 GCSE Computer Science · 3.1 Fundamentals of Algorithms
Stretch tasks Efficiency and Searching Algorithms
Quiz Challenge
A quiz game against four computer players, then short and long written answers.
- Quiz Blitz12 questions against the clock
- Quick Fire4 short answers
- Big Think2 long answers
About 20 minutes
This or That
Pixel the cat asks 8 questions. Each has two answers: pick the right one.
- Two answers to every question
- A fish treat for every right answer
- Streaks win Pixel shades, then a party hat
About 4 minutes
Memory Match
Flip the cards two at a time to match each term card to its meaning card.
- 12 cards: each term has a meaning
- One peek at every card
- Combo points for pairs found in a row
6 pairs, about 4 minutes
Sort It
Sort each statement into the right box, like the tick-box tables in the exam.
- 10 cards to sort into 2 boxes
- Drag a card, tap a box or press its number
- Combo points for cards sorted in a row
10 cards, about 4 minutes
The games need JavaScript. Every question is below, with the answers at the end.
Read the lesson: Efficiency and Searching Algorithms
Efficiency and Searching Algorithms: the questions
Quiz Challenge, round 1: Quiz Blitz
Binary search can only be used when the list is...
- Short
- Unsorted
- Numbers only
- Sorted
What is the worst case for a linear search?
- The target is first
- The list is sorted
- The list has one item
- The target is last or missing
A linear search checks a list of 1,000 items. How many comparisons does it make in the worst case?
About how many comparisons does a binary search need for 1,000 items in the worst case?
Each time a list doubles in size, binary search needs just one extra comparison.
- True
- False
A binary search looks for 23 in [4, 8, 15, 16, 23, 42]. Which item is the first middle item?
- 16
- 4
- 23
- 15
Why can a faster algorithm beat faster hardware?
- It only works on sorted lists
- It does less work, not the same work faster
- It uses more memory
- It needs a newer CPU
When is linear search the better choice?
Tick 2.
- The data is in alphabetical order
- The data is unsorted
- The list is short
- The list is huge and sorted
- The same sorted list is searched millions of times
What does efficiency mean at GCSE?
- How fast the CPU is
- How many variables it uses
- How many steps an algorithm needs
- How short the code is
Sorting a list just to do one binary search is usually slower than one linear search.
- True
- False
Put the steps of a binary search in order.
Number these 1 to 4.
- Compare it with the target
- Discard the half that cannot contain it
- Repeat until found or nothing is left
- Find the middle item
Why do examiners ask about the worst case?
- The worst case is always the first item
- The best case cannot be calculated
- Worst cases only happen in binary search
- The best case is luck and says little about the algorithm
Quiz Challenge, round 2: Quick Fire
Explain why a binary search needs only about 20 comparisons for a million items. [2 marks]
Give one advantage of linear search over binary search, and explain why it matters. [2 marks]
State the best case and the worst case for a linear search. [2 marks]
Describe the steps of a binary search. [3 marks]
Quiz Challenge, round 3: Big Think
A library has 50,000 books sorted by ISBN. Explain whether linear or binary search should be used to find a book, and why. [4 marks]
Explain what is meant by the efficiency of an algorithm, and why choosing an efficient algorithm matters more as the data gets bigger. [4 marks]
This or That: tick the right answer
A list changes every second and is never sorted. Which search suits it?
- Binary search
- Linear search
Worst case for a binary search of 100 items is about...
- 7 comparisons
- 50 comparisons
A linear search finds 42 in [4, 8, 15, 16, 23, 42]. How many comparisons?
- 1
- 6
A list doubles in size. What happens to linear search's worst case?
- It goes up by one
- It doubles
What is the best case for a linear search?
- The target is last
- The target is first
A binary search runs out of items to check. What does that mean?
- The list was empty all along
- The target is not in the list
In a search, what is one comparison?
- One test of an item against the target
- One pass of a sort
Is it fair to compare search algorithms on a list of five items?
- No: efficiency shows as lists grow
- Yes: small lists show it best
Memory Match: match each term to its meaning
| Term | Meaning |
|---|---|
| A Linear search | 1 What binary search compares first |
| B Binary search | 2 How many steps an algorithm needs |
| C Worst case | 3 The luckiest possible input |
| D Best case | 4 Checks each item in turn |
| E Efficiency | 5 Halves a sorted list each time |
| F Middle item | 6 The input needing the most steps |
Sort It: tick one box in each row
Linear search or binary search?
| Statement | Linear search | Binary search |
|---|---|---|
| Simple to write and hard to get wrong | ||
| One extra comparison each time the list doubles | ||
| Works on unsorted data | ||
| Worst case of n comparisons for n items | ||
| Checks each item in turn from the start | ||
| Needs a sorted list | ||
| Compares the target with the middle item | ||
| Best for a one-off search of unsorted data | ||
| Discards half of the remaining items each time | ||
| About 20 comparisons for a million items |
Answers
Round 1: Quiz Blitz
- Sorted It discards halves, which only works if the order shows which half to keep.
- The target is last or missing Every item has to be checked.
- 1000 In the worst case it compares every one of the n items.
- 10 1,000 can be halved about 10 times before nothing is left.
- True One more comparison halves the list one more time.
- 15 23 is greater than 15, so the right half, 16, 23 and 42, is kept.
- It does less work, not the same work faster Switching to binary search speeds up a program on exactly the same machine.
- The data is unsorted; The list is short Sorted and large means binary. Unsorted, or small, means linear.
- How many steps an algorithm needs Fewer steps means less time.
- True Sorting costs far more than a single linear search saves.
- Find the middle item → Compare it with the target → Discard the half that cannot contain it → Repeat until found or nothing is left Every comparison halves what is left to search.
- The best case is luck and says little about the algorithm The worst case shows how the algorithm copes as data grows.
Round 2: Quick Fire
- Each comparison halves the remaining list
- A million can only be halved about 20 times
Model answerEach comparison halves the part of the list that is left, and a million can only be halved about 20 times before nothing is left.
- It works on unsorted data
- So no time is spent sorting first, and it is simple
Model answerLinear search works on unsorted data, so no time has to be spent sorting the list first.
- Best case: the target is the first item
- Worst case: the target is last, or missing
Model answerBest case: the target is the first item checked. Worst case: the target is the last item, or is not in the list at all.
- Find the middle item
- If the target is smaller, keep only the left half
- If it is larger, keep only the right half
- Repeat until found, or until no items are left
Model answerFind the middle item of the list. If it is the target, stop. If the target is smaller, keep only the left half; if it is larger, keep only the right half. Repeat until the target is found or there are no items left.
Round 3: Big Think
- Binary search should be used
- Because the books are already in order
- Each comparison halves the remaining list
- So about 16 comparisons, instead of up to 50,000
Model answerBinary search should be used, because the books are already sorted by ISBN. Each comparison halves the remaining list, so about 16 comparisons are enough to find any book, whereas a linear search could check every book, up to 50,000 comparisons.
- Efficiency is how many steps, or how much time, an algorithm uses
- Compared by counting comparisons in the worst case
- On large data the difference is huge
- A better algorithm does less work, so it beats faster hardware
Model answerEfficiency is how many steps, and so how much time, an algorithm uses to solve a problem. Algorithms are compared by counting their comparisons in the worst case. On ten items nothing matters, but on a million the difference is huge: a linear search might need a million comparisons where a binary search needs 20. A better algorithm does less work, so it speeds a program up on the same machine, without faster hardware.
This or That
- Linear search Binary search needs a sorted list, and sorting it constantly would cost too much.
- 7 comparisons 100 halves to 50, 25, 13, 7, 4, 2, 1: about 7 comparisons.
- 6 It checks each item from the start, and 42 is sixth.
- It doubles Linear search checks every item, so twice the items means twice the comparisons.
- The target is first One comparison and it is found.
- The target is not in the list Every half that could have held the target has been discarded.
- One test of an item against the target Comparisons are the unit used to measure search efficiency.
- No: efficiency shows as lists grow On tiny lists every algorithm is instant. Differences appear as the data grows.
Memory Match
- A 4 Linear search: Checks each item in turn
- B 5 Binary search: Halves a sorted list each time
- C 6 Worst case: The input needing the most steps
- D 3 Best case: The luckiest possible input
- E 2 Efficiency: How many steps an algorithm needs
- F 1 Middle item: What binary search compares first
Sort It
- Linear search Simple to write and hard to get wrong Linear search is the simplest search.
- Binary search One extra comparison each time the list doubles One more halving covers twice the items.
- Linear search Works on unsorted data Linear search checks every item, so order does not matter.
- Linear search Worst case of n comparisons for n items In the worst case every item is checked.
- Linear search Checks each item in turn from the start That is how linear search works.
- Binary search Needs a sorted list Binary search only works on sorted data.
- Binary search Compares the target with the middle item Binary search always starts in the middle.
- Linear search Best for a one-off search of unsorted data Sorting first would cost more than it saves.
- Binary search Discards half of the remaining items each time That is what makes binary search fast.
- Binary search About 20 comparisons for a million items Halving a million 20 times leaves nothing.