GCSE · Computer Science · Edexcel · Spec 1CP2
Evaluating algorithm efficiency
Two searches. Same list. Same target. One needs 8 goes, the other needs 4. So the faster one always wins, right? Not quite. That's what this lesson is about.
Algorithms · Efficiency
Same list, same target: which search does less work?
Eight planets in alphabetical order. We're hunting for Venus, the last one. Each step adds one comparison to the trace table: rows 1 to 8 are linear search, rows 9 to 12 are binary search. The highlighted planet is the one being compared with Venus.
Before you step through: how many comparisons do you reckon each search will need to find Venus? Make a guess for both.
1 Earth2 Jupiter3 Mars4 Mercury5 Neptune6 Saturn7 Uranus8 Venus
| search | compared with Venus | match? | planets left to search | comparisons so far |
|---|---|---|---|---|
| linear | Earth | no | 8 | 1 |
Output
Step 1: Linear search starts at the front. Earth? Nope. One comparison down.
This is a trace table: it records the values step by step. Count its rows and you've counted the comparisons. That's how a trace helps you check how efficient an algorithm is.
WHAT YOU'VE LEARNED
A quick recap of today's lesson.
Don't guess which algorithm is faster. Count its comparisons. Then ask whether faster is actually the right choice.
What you need to know
- An algorithm is a set of step-by-step instructions to solve a problem. Know how two differ and you'll spot when one suits better.
- Searches are compared by counting comparisons. The worst case is the highest number of comparisons possible on a given list.
Have a goA linear search finds its item on the very first comparison. Is that its worst case?
No. That's the fewest comparisons it could make.
The worst case is the most comparisons possible on that list, not whatever happened on one lucky run.
- Linear search checks each item one at a time, ordered list or not. Worst case: every item compared, so double the list, double that.
- Binary search needs ordered items. Each comparison discards half the remaining data, so doubling the list adds at most one comparison.
Have a goSam's 300-song playlist is in a completely random order. Sam is sure binary search will find a song fastest. What's the catch?
Binary search needs the songs in order. Linear search works on them as they are.
Halving only works because the order tells you which half to throw away. With no order, Sam's binary search can't decide.
- Sorted list of eight planets: linear search's worst case (Venus) takes 8 comparisons. Binary search's worst case takes just 4.
- Linear search is simpler to write; binary search is longer and more complex, as it can take different paths. Data and context decide.
- A trace table tracks variable values and the flow of execution step by step, which helps you check how efficient an algorithm is.
- Bubble sort swaps neighbouring items that are in the wrong order. Each pass makes (items − 1) comparisons; passes repeat until one makes no changes.
Have a goAn ascending bubble sort reaches the neighbouring pair 8, 3 in the middle of a pass. What happens to them, and where does the pass go next?
Swap them to 3, 8, then compare the next neighbouring pair.
They're neighbours in the wrong order, so they swap. A pass doesn't stop at one swap: it carries on to the end of the list.
- Bubble sort is one of the slowest sorts, especially on large data. Improve it: stop after a no-swap pass, and make one fewer comparison each pass.
- Picking a sorting algorithm? Weigh its efficiency on large data, how easy it is to implement and test, its memory needs and the data's format.
The big picture
Two algorithms can do the same job with very different amounts of work. Compare searches by counting comparisons in the worst case: on a sorted list of eight planets, linear search needs 8 and binary search needs 4, because each binary comparison discards half of what's left. Double the list and linear search's worst case doubles; binary search needs at most one more. But binary search needs ordered data and is harder to write, so the choice depends on data and context. Bubble sort is one of the slowest sorts; stopping after a no-swap pass and making one fewer comparison each pass cut its waste.
Key points
Worked example
Problem
Use the improved bubble sort to put 5, 2, 7, 3, 9 into ascending order. How many comparisons does it make?
⚠ Watch out
Thinking that doubling the list doubles binary search's comparisons too. It feels fair: twice the items, twice the work. But that's only linear search. Binary search needs at most one more comparison, because its first comparison halves the doubled list straight back down.
Memory hook
Double the list: linear DOUBLES, binary adds ONE. Then ask the second question: is the data even in order?
Check yourself
A sorted list has 32 items. What's linear search's worst case? Binary search needed at most 5 for 16 items, so what now? Give one reason to still choose linear search.
Flashcards
(16)How is the efficiency of searching algorithms compared?
What is the worst-case scenario for a searching algorithm?
How does a linear search work?
Linear search: what happens to the worst case if the list doubles?
How does a binary search work?
Binary search: what happens to the worst case if the list doubles?
Sorted list of eight planets: worst-case comparisons for each search?
Which search needs the data to be in order?
Which search is simpler to write, and why is the other one harder?
What decides which searching algorithm is more suitable?
How can a trace table help you evaluate an algorithm?
How does bubble sort work?
How many comparisons does one full bubble sort pass make?
Two ways to make bubble sort more efficient?
Why can the improved bubble sort make one fewer comparison after each pass?
Factors to consider when choosing a sorting algorithm?
Tap any card to flip it, or use Study as deck to go through them one at a time. In the full lesson these run as a spaced-repetition deck — you rate each card Hard, Good or Easy and the tricky ones keep coming back until they stick.
Learning with Lightbulb is opening soon
You can use this lesson now. Join the waitlist and we'll let you know when the full Lightbulb experience is ready.
Keep me postedMore Edexcel GCSE Computer Science topics
How this lesson was checked. This Edexcel GCSE Computer Science (specification 1CP2)lesson was published through Lightbulb Learning's human-designed editorial process — the educational standards, accuracy rules and publication checks it must pass were authored and approved by Philip Halpin. It passed subject-specific assessment, automated educational checks and technical publication verification before going live (publication checks completed 31 October 2026). Published pages are monitored, human spot-checking is ongoing across the lesson library, and anything found wrong is corrected or withdrawn. How our lessons are made and checked. Spotted a mistake? Email hello@lightbulblearning.co and we'll review it.