OCR H446 A Level Computer Science · 2.3 Algorithms
Stretch tasks Algorithms and Big O Complexity
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 algorithm card to its how it works card.
- 12 cards: each algorithm has a how it works
- 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.
- 12 cards to sort into 4 boxes
- Drag a card, tap a box or press its number
- Combo points for cards sorted in a row
12 cards, about 4 minutes
Code Word
Guess the lesson's secret words. Every guess shows which letters are right.
- A clue for every word
- Tiles turn green, amber or grey after each guess
- Fewer guesses win more points
3 words, about 4 minutes
Link Up
12 tiles hide 3 groups of 4. Work out what links them.
- Pick four tiles that belong together
- Four slips and the round is over
- Fewer slips win more points
3 groups, about 3 minutes
Bug Hunt
Two statements are true and one has a bug. Catch the bug before it gets away.
- Three statements in every round
- Tap the one with the mistake
- Streaks win bonus points
5 rounds, about 4 minutes
Brain games
Train your brain on this lesson
Logic, memory and speed, built from this lesson's own key ideas. Win up to three diamonds in each game.
Odd One Out
Four tiles: three share a link and one does not. Spot the odd one out, then see what links the rest.
- Three belong together, one does not
- A hint shows the link, for fewer points
- Quick answers and streaks win more
8 rounds, about 3 minutes
Memory Flash
Study the algorithm cards, then they flip over. Find the card that goes with each clue, from memory.
- Three levels: 3, 4 and 6 cards
- Two tries to find each card
- Streaks win bonus points
3 levels, about 4 minutes
Fact or Fib
Statements fly in one at a time. Is each one a fact or a fib? Decide fast.
- Swipe, tap or use the arrow keys
- Every fib comes with its fix
- Streaks multiply your points
12 statements, about 3 minutes
The final challenge
City Run
Race a robot cat on inline skates through a neon cyber city, from Neon Station to the Hyper Tunnel. Every question is a row of neon gates across the tracks: skate through the one with the right answer, dodge the trains and barriers, and grab diamonds.
- 20 questions from this lesson, in four parts of the city
- Swipe, tap or use the arrow keys
- Dodge trains and holograms, and jump crates and cones
20 questions, about 6 minutes
Bonus challenge
Neon Roll
Roll a glowing ball down an endless neon slope, past towers lit up with neon signs of Pixel the cat. Steer, jump the gaps and dodge the blocks. Questions from this lesson wait at gates along the way: every right answer keeps the ball rolling.
- Questions from this lesson at every gate
- Hold the arrow keys or a side of the slope to steer
- 8 right answers win two diamonds, 15 win three
Endless, about 5 minutes
The games need JavaScript. Every question is below, with the answers at the end.
Read the lesson: Algorithms and Big O Complexity
Algorithms and Big O Complexity: the questions
Quiz Challenge, round 1: Quiz Blitz
What is the time complexity of binary search?
- O(n)
- O(n log n)
- O(1)
- O(log n)
What must be true of data before a binary search?
- It must be stored in a linked list
- It must have an even number of items
- It must be sorted
- It must contain no duplicates
Merge sort takes O(n log n) time in every case.
- True
- False
Which data structure does depth first search use?
- Hash table
- Two dimensional array
- Stack
- Queue
Which TWO sorts use only a fixed amount of extra memory?
Tick 2.
- Merge sort
- Quicksort
- Bubble sort
- Insertion sort
Order these complexities from the slowest growing to the fastest growing.
Number these 1 to 4.
- O(log n)
- O(n)
- O(n log n)
- O(1)
When does quicksort hit its worst case of O(n squared)?
- When it uses a stack
- When the pivot is always the smallest or largest item
- When the list is very short
- When every item is different
Dijkstra's algorithm works correctly with negative edge weights.
- True
- False
Which algorithm finds shortest paths by choosing the node with the lowest f(n) = g(n) + h(n)?
What makes an A star heuristic admissible?
- It ignores the edge weights
- It visits every node
- It never overestimates the remaining cost
- It always overestimates the cost
Which search finds the shortest path in an unweighted graph?
- Binary search
- Breadth first search
- Depth first search
- Linear search
Why might you sort data before searching it, even though sorting costs O(n log n)?
- Sorting makes the data smaller
- Unsorted data cannot be searched at all
- Sorting removes duplicates
- Many later searches each drop from O(n) to O(log n)
Quiz Challenge, round 2: Quick Fire
Explain why binary search is O(log n). [2 marks]
Explain why Dijkstra's algorithm cannot handle negative edge weights. [2 marks]
Give one advantage of merge sort over quicksort, and one of quicksort over merge sort. [2 marks]
Explain why depth first search uses a stack and breadth first search uses a queue. [2 marks]
Quiz Challenge, round 3: Big Think
Describe how Dijkstra's algorithm finds the shortest path from a start node. [6 marks]
Explain how A star improves on Dijkstra's algorithm, and when it is guaranteed to find the best path. [4 marks]
This or That: tick the right answer
Which search works on unsorted data?
- Binary search
- Linear search
Which is O(n log n) even in its worst case?
- Quicksort
- Merge sort
Which sort suits a small list that is nearly sorted?
- Merge sort
- Insertion sort
Which grows faster as n increases?
- O(n log n)
- O(n squared)
Which finds the nearest node to a start point in an unweighted graph?
- Depth first search
- Breadth first search
Dijkstra's algorithm is the same as A star with what?
- g set to zero
- h set to zero
Which statement is about space rather than time?
- Merge sort needs O(n) memory
- Merge sort takes O(n log n) steps
What is the average complexity of a hash table lookup?
- O(1)
- O(n)
Memory Match: match each algorithm to its how it works
| Algorithm | How it works |
|---|---|
| A Linear search | 1 Halves a sorted range each comparison |
| B Binary search | 2 Shortest paths with non negative weights |
| C Bubble sort | 3 Splits to single items, then merges back |
| D Merge sort | 4 Partitions the items around a pivot |
| E Quicksort | 5 Checks each item in turn |
| F Dijkstra | 6 Swaps adjacent pairs that are out of order |
Sort It: tick one box in each row
What is the time complexity of each?
| Statement | O(1) | O(log n) | O(n) | O(n squared) |
|---|---|---|---|---|
| Bubble sort | ||||
| Binary search | ||||
| Finding a word by halving a dictionary | ||||
| Pushing an item onto a stack | ||||
| Comparing every item with every other item | ||||
| A hash table lookup on average | ||||
| Linear search | ||||
| Finding the largest value in an unsorted list | ||||
| Reading an array element by its index | ||||
| Searching a balanced binary search tree | ||||
| Printing every item of a list | ||||
| Insertion sort on a reversed list |
Code Word: write the word that fits each clue
The item quicksort uses to split the data into smaller and larger values. (5 letters)
His shortest path algorithm cannot handle negative edge weights. (8 letters)
This search halves a sorted range with every comparison. (6 letters)
Link Up: find the three groups
These 12 tiles make 3 groups of 4. Write each group in the table, and what links its tiles.
- A star
- Bubble
- Depth first
- Merge
- Heuristic
- Quick
- Breadth first
- Admissible
- Linear
- Binary
- Insertion
- Dijkstra
| Group | The 4 tiles | What links them |
|---|---|---|
| Group 1 | ||
| Group 2 | ||
| Group 3 |
Bug Hunt: tick the statement that is wrong
In each set, two statements are true and one has a mistake in it.
- Binary search is O(n) because it checks every item.
- Binary search needs the data to be sorted.
- Linear search works on unsorted data.
- Merge sort is O(n log n) whatever the order of the data.
- Quicksort is O(n log n) even in its worst case.
- Merge sort needs O(n) extra memory.
- Depth first search finds the shortest path in an unweighted graph.
- Breadth first search uses a queue.
- Depth first search uses a stack.
- Dijkstra uses a heuristic estimate.
- Dijkstra's algorithm finds shortest paths from one start node.
- Dijkstra's algorithm needs non negative edge weights.
- An admissible heuristic never overestimates the remaining cost.
- A star is guaranteed the best path whatever heuristic it uses.
- A star chooses the node with the lowest value of g plus h.
Brain game: Odd One Out
Three tiles in each set share a link. Tick the odd one out, then write what links the other three.
- Bubble
- Depth first
- Breadth first
- Binary
What links the other three?
- Bubble
- Merge
- Binary
- Insertion
What links the other three?
- A star
- Depth first
- Admissible
- Dijkstra
What links the other three?
- Depth first
- Quick
- Breadth first
- Binary
What links the other three?
- A star
- Bubble
- Merge
- Insertion
What links the other three?
- Merge
- Heuristic
- Admissible
- Dijkstra
What links the other three?
Brain game: Memory Flash, on paper
Which algorithm works like this? Write your answer from memory.
Halves a sorted range each comparison
Shortest paths with non negative weights
Splits to single items, then merges back
Partitions the items around a pivot
Checks each item in turn
Swaps adjacent pairs that are out of order
Brain game: Fact or Fib
Tick Fact if the statement is true and Fib if it is not.
Breadth first search uses a queue.
- Fact
- Fib
Dijkstra's algorithm works correctly with negative edge weights.
- Fact
- Fib
Depth first search finds the shortest path in an unweighted graph.
- Fact
- Fib
Dijkstra's algorithm finds shortest paths from one start node.
- Fact
- Fib
Binary search needs the data to be sorted.
- Fact
- Fib
Merge sort takes O(n log n) time in every case.
- Fact
- Fib
An admissible heuristic never overestimates the remaining cost.
- Fact
- Fib
A star is guaranteed the best path whatever heuristic it uses.
- Fact
- Fib
Binary search is O(n) because it checks every item.
- Fact
- Fib
Dijkstra uses a heuristic estimate.
- Fact
- Fib
Quicksort is O(n log n) even in its worst case.
- Fact
- Fib
Merge sort is O(n log n) whatever the order of the data.
- Fact
- Fib
City Run
City Run is a race through a neon cyber city, played on screen. It asks 20 of the questions above, from the quiz, This or That, Memory Flash and Fact or Fib.
Neon Roll
Neon Roll is a ball rolling down an endless neon slope, played on screen. Every right answer keeps it rolling. It asks the questions above, from the quiz, This or That, Memory Flash and Fact or Fib.
Answers
Round 1: Quiz Blitz
- O(log n) Each comparison halves the range, so a million items need only about twenty comparisons.
- It must be sorted Comparing with the middle item only tells you which half to discard if the data is in order.
- True Its splitting and combining do the same work whatever the order of the data, which is its great strength.
- Stack Depth first search backtracks to the most recent branch, last in, first out, so it uses a stack or recursion.
- Bubble sort; Insertion sort Bubble and insertion sort work in place. Merge sort stores its sub lists, and quicksort needs a stack for its recursion.
- O(1) → O(log n) → O(n) → O(n log n) Constant, then logarithmic, then linear, then linearithmic.
- When the pivot is always the smallest or largest item A bad pivot leaves one side empty every time, so the list shrinks by only one item per pass.
- False Once a node is visited its distance is treated as final, so a negative edge found later could break that.
- a star g is the cost so far and h is a heuristic estimate of the cost still to go.
- It never overestimates the remaining cost An admissible heuristic guarantees that A star finds the optimal path.
- Breadth first search It explores every node one step away, then two, so it first reaches each node by the fewest edges.
- Many later searches each drop from O(n) to O(log n) The sorting cost is paid once, and every search after that is far faster.
Round 2: Quick Fire
- Each comparison halves the remaining range
- So doubling the number of items adds only one more comparison
Model answerEach comparison halves the remaining range, so doubling the number of items adds only one more comparison. A million items need only about twenty comparisons.
- Once a node is visited its distance is treated as final
- A negative edge found later could give a shorter route
Model answerOnce a node is marked visited its distance is treated as final, but a negative edge found later could give a shorter route to it, so the answer could be wrong.
- Merge sort is O(n log n) in every case, quicksort is not
- Quicksort needs less memory and is usually faster in practice
Model answerMerge sort is O(n log n) in every case, while quicksort can fall to n squared in its worst case. Quicksort needs less memory, because merge sort has to store the sub lists, and it is usually faster in practice.
- Depth first goes as deep as possible, then backtracks to the most recent branch
- Breadth first visits nodes in the order they were found, level by level
Model answerDepth first search goes as deep as possible, then backtracks to the most recent branch, which is last in, first out, like a stack. Breadth first search visits nodes in the order they were found, level by level, which is first in, first out, like a queue.
Round 3: Big Think
- The start node gets distance 0 and every other node infinity
- Every node starts unvisited
- The unvisited node with the smallest known distance becomes the current node
- The distance to each unvisited neighbour through the current node is worked out
- A shorter distance replaces the old one, with the current node as predecessor
- The current node is marked visited and the steps repeat
- The path is rebuilt by working back through the predecessors
- The weights must not be negative
Model answerThe start node is given a distance of 0 and every other node a distance of infinity, and all nodes are marked unvisited. The unvisited node with the smallest known distance becomes the current node. For each unvisited neighbour, the distance through the current node is calculated, and if it is shorter than the recorded distance it is updated and the current node is recorded as its predecessor. The current node is then marked visited, and this repeats until the destination is visited. The path is rebuilt by working back from the destination through the predecessors. The edge weights must not be negative.
- It adds a heuristic estimate of the distance still to go
- It chooses the node with the lowest f = g + h
- It explores far fewer nodes, so it is faster
- It is optimal when the heuristic is admissible, never overestimating
- Dijkstra is A star with h set to zero
Model answerA star adds a heuristic, an estimate of the remaining distance to the goal such as the straight line distance. At each step it chooses the node with the lowest f = g + h, where g is the actual cost so far. Because it prefers nodes that lead towards the goal, it explores far fewer nodes and is faster. It is guaranteed to find the best path as long as the heuristic is admissible, meaning it never overestimates the remaining cost.
This or That
- Linear search Linear search simply checks each item. Binary search needs sorted data to discard half each time.
- Merge sort Quicksort can fall to n squared with bad pivots. Merge sort does the same work every time.
- Insertion sort Insertion sort is close to O(n) on nearly sorted data and needs no extra memory.
- O(n squared) For a million items, n squared is a million million, but n log n is about twenty million.
- Breadth first search Breadth first explores level by level, so the first time it meets a node is by the fewest edges.
- h set to zero With no heuristic, A star chooses purely by the cost so far, which is exactly what Dijkstra does.
- Merge sort needs O(n) memory Space complexity is about memory. Merge sort's O(n) space is the price it pays for its speed.
- O(1) The hash function goes straight to the index, however many items there are.
Memory Match
- A 5 Linear search: Checks each item in turn
- B 1 Binary search: Halves a sorted range each comparison
- C 6 Bubble sort: Swaps adjacent pairs that are out of order
- D 3 Merge sort: Splits to single items, then merges back
- E 4 Quicksort: Partitions the items around a pivot
- F 2 Dijkstra: Shortest paths with non negative weights
Sort It
- O(n squared) Bubble sort Up to n passes of up to n comparisons.
- O(log n) Binary search Each comparison halves the range.
- O(log n) Finding a word by halving a dictionary Halving the pages each time is logarithmic.
- O(1) Pushing an item onto a stack Adding to the top takes the same time however full the stack is.
- O(n squared) Comparing every item with every other item n items each compared with n others gives n squared.
- O(1) A hash table lookup on average The hash function goes straight to the right slot.
- O(n) Linear search In the worst case every item is checked once.
- O(n) Finding the largest value in an unsorted list Every item must be looked at once.
- O(1) Reading an array element by its index The address is worked out directly, however long the array.
- O(log n) Searching a balanced binary search tree Each step down a balanced tree halves what is left.
- O(n) Printing every item of a list One step for each item is linear.
- O(n squared) Insertion sort on a reversed list Each item has to move past every item before it.
Code Word
- PIVOT The item quicksort uses to split the data into smaller and larger values.
- DIJKSTRA His shortest path algorithm cannot handle negative edge weights.
- BINARY This search halves a sorted range with every comparison.
Link Up
- Searches Linear; Binary; Depth first; Breadth first
- Sorting algorithms Bubble; Insertion; Merge; Quick
- Shortest path ideas Dijkstra; A star; Heuristic; Admissible
Bug Hunt: the wrong statement, and why
- Binary search is O(n) because it checks every item. Binary search halves the range at every comparison, so it is O(log n).
- Quicksort is O(n log n) even in its worst case. Quicksort's worst case is O(n squared), when the pivot is always the smallest or largest item.
- Depth first search finds the shortest path in an unweighted graph. It is breadth first search that finds the shortest path in an unweighted graph, by exploring level by level.
- Dijkstra uses a heuristic estimate. Dijkstra uses no heuristic. A star adds one, choosing the node with the lowest f = g + h.
- A star is guaranteed the best path whatever heuristic it uses. Only an admissible heuristic, one that never overestimates, guarantees the best path.
Odd One Out
- Bubble is the odd one out: it goes with Sorting algorithms. The other three: Searches.
- Binary is the odd one out: it goes with Searches. The other three: Sorting algorithms.
- Depth first is the odd one out: it goes with Searches. The other three: Shortest path ideas.
- Quick is the odd one out: it goes with Sorting algorithms. The other three: Searches.
- A star is the odd one out: it goes with Shortest path ideas. The other three: Sorting algorithms.
- Merge is the odd one out: it goes with Sorting algorithms. The other three: Shortest path ideas.
Memory Flash
- Binary search Halves a sorted range each comparison
- Dijkstra Shortest paths with non negative weights
- Merge sort Splits to single items, then merges back
- Quicksort Partitions the items around a pivot
- Linear search Checks each item in turn
- Bubble sort Swaps adjacent pairs that are out of order
Fact or Fib
- Fact Breadth first search uses a queue.
- Fib Dijkstra's algorithm works correctly with negative edge weights. The fix: Once a node is visited its distance is treated as final, so a negative edge found later could break that.
- Fib Depth first search finds the shortest path in an unweighted graph. The fix: It is breadth first search that finds the shortest path in an unweighted graph, by exploring level by level.
- Fact Dijkstra's algorithm finds shortest paths from one start node.
- Fact Binary search needs the data to be sorted.
- Fact Merge sort takes O(n log n) time in every case.
- Fact An admissible heuristic never overestimates the remaining cost.
- Fib A star is guaranteed the best path whatever heuristic it uses. The fix: Only an admissible heuristic, one that never overestimates, guarantees the best path.
- Fib Binary search is O(n) because it checks every item. The fix: Binary search halves the range at every comparison, so it is O(log n).
- Fib Dijkstra uses a heuristic estimate. The fix: Dijkstra uses no heuristic. A star adds one, choosing the node with the lowest f = g + h.
- Fib Quicksort is O(n log n) even in its worst case. The fix: Quicksort's worst case is O(n squared), when the pivot is always the smallest or largest item.
- Fact Merge sort is O(n log n) whatever the order of the data.

