OCR H446 A Level Computer Science · 2.3 Algorithms

Stretch tasks Algorithms and Big O Complexity

Task 1

Quiz Challenge

A quiz game against four computer players, then short and long written answers.

  1. Quiz Blitz12 questions against the clock
  2. Quick Fire4 short answers
  3. Big Think2 long answers

About 20 minutes

Task 2

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

Task 3

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

Task 4

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

Task 5

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

Task 7

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.

Logic

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

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

Speed

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

  1. What is the time complexity of binary search?

    • O(n)
    • O(n log n)
    • O(1)
    • O(log n)
  2. 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
  3. Merge sort takes O(n log n) time in every case.

    • True
    • False
  4. Which data structure does depth first search use?

    • Hash table
    • Two dimensional array
    • Stack
    • Queue
  5. Which TWO sorts use only a fixed amount of extra memory?

    Tick 2.

    • Merge sort
    • Quicksort
    • Bubble sort
    • Insertion sort
  6. 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)
  7. 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
  8. Dijkstra's algorithm works correctly with negative edge weights.

    • True
    • False
  9. Which algorithm finds shortest paths by choosing the node with the lowest f(n) = g(n) + h(n)?

  10. 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
  11. Which search finds the shortest path in an unweighted graph?

    • Binary search
    • Breadth first search
    • Depth first search
    • Linear search
  12. 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

  1. Explain why binary search is O(log n). [2 marks]

  2. Explain why Dijkstra's algorithm cannot handle negative edge weights. [2 marks]

  3. Give one advantage of merge sort over quicksort, and one of quicksort over merge sort. [2 marks]

  4. Explain why depth first search uses a stack and breadth first search uses a queue. [2 marks]

Quiz Challenge, round 3: Big Think

  1. Describe how Dijkstra's algorithm finds the shortest path from a start node. [6 marks]

  2. 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

  1. Which search works on unsorted data?

    • Binary search
    • Linear search
  2. Which is O(n log n) even in its worst case?

    • Quicksort
    • Merge sort
  3. Which sort suits a small list that is nearly sorted?

    • Merge sort
    • Insertion sort
  4. Which grows faster as n increases?

    • O(n log n)
    • O(n squared)
  5. Which finds the nearest node to a start point in an unweighted graph?

    • Depth first search
    • Breadth first search
  6. Dijkstra's algorithm is the same as A star with what?

    • g set to zero
    • h set to zero
  7. Which statement is about space rather than time?

    • Merge sort needs O(n) memory
    • Merge sort takes O(n log n) steps
  8. What is the average complexity of a hash table lookup?

    • O(1)
    • O(n)

Memory Match: match each algorithm to its how it works

AlgorithmHow it works
A Linear search1 Halves a sorted range each comparison
B Binary search2 Shortest paths with non negative weights
C Bubble sort3 Splits to single items, then merges back
D Merge sort4 Partitions the items around a pivot
E Quicksort5 Checks each item in turn
F Dijkstra6 Swaps adjacent pairs that are out of order

Sort It: tick one box in each row

What is the time complexity of each?

StatementO(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

  1. The item quicksort uses to split the data into smaller and larger values. (5 letters)

  2. His shortest path algorithm cannot handle negative edge weights. (8 letters)

  3. 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

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.

  1. Halves a sorted range each comparison

  2. Shortest paths with non negative weights

  3. Splits to single items, then merges back

  4. Partitions the items around a pivot

  5. Checks each item in turn

  6. 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.

  1. Breadth first search uses a queue.

    • Fact
    • Fib
  2. Dijkstra's algorithm works correctly with negative edge weights.

    • Fact
    • Fib
  3. Depth first search finds the shortest path in an unweighted graph.

    • Fact
    • Fib
  4. Dijkstra's algorithm finds shortest paths from one start node.

    • Fact
    • Fib
  5. Binary search needs the data to be sorted.

    • Fact
    • Fib
  6. Merge sort takes O(n log n) time in every case.

    • Fact
    • Fib
  7. An admissible heuristic never overestimates the remaining cost.

    • Fact
    • Fib
  8. A star is guaranteed the best path whatever heuristic it uses.

    • Fact
    • Fib
  9. Binary search is O(n) because it checks every item.

    • Fact
    • Fib
  10. Dijkstra uses a heuristic estimate.

    • Fact
    • Fib
  11. Quicksort is O(n log n) even in its worst case.

    • Fact
    • Fib
  12. 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

  1. O(log n) Each comparison halves the range, so a million items need only about twenty comparisons.
  2. It must be sorted Comparing with the middle item only tells you which half to discard if the data is in order.
  3. True Its splitting and combining do the same work whatever the order of the data, which is its great strength.
  4. Stack Depth first search backtracks to the most recent branch, last in, first out, so it uses a stack or recursion.
  5. 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.
  6. O(1) → O(log n) → O(n) → O(n log n) Constant, then logarithmic, then linear, then linearithmic.
  7. 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.
  8. False Once a node is visited its distance is treated as final, so a negative edge found later could break that.
  9. a star g is the cost so far and h is a heuristic estimate of the cost still to go.
  10. It never overestimates the remaining cost An admissible heuristic guarantees that A star finds the optimal path.
  11. Breadth first search It explores every node one step away, then two, so it first reaches each node by the fewest edges.
  12. 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 answer

    Each 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 answer

    Once 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 answer

    Merge 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 answer

    Depth 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 answer

    The 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 answer

    A 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

  1. Linear search Linear search simply checks each item. Binary search needs sorted data to discard half each time.
  2. Merge sort Quicksort can fall to n squared with bad pivots. Merge sort does the same work every time.
  3. Insertion sort Insertion sort is close to O(n) on nearly sorted data and needs no extra memory.
  4. O(n squared) For a million items, n squared is a million million, but n log n is about twenty million.
  5. Breadth first search Breadth first explores level by level, so the first time it meets a node is by the fewest edges.
  6. h set to zero With no heuristic, A star chooses purely by the cost so far, which is exactly what Dijkstra does.
  7. 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.
  8. 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

  1. O(n squared) Bubble sort Up to n passes of up to n comparisons.
  2. O(log n) Binary search Each comparison halves the range.
  3. O(log n) Finding a word by halving a dictionary Halving the pages each time is logarithmic.
  4. O(1) Pushing an item onto a stack Adding to the top takes the same time however full the stack is.
  5. O(n squared) Comparing every item with every other item n items each compared with n others gives n squared.
  6. O(1) A hash table lookup on average The hash function goes straight to the right slot.
  7. O(n) Linear search In the worst case every item is checked once.
  8. O(n) Finding the largest value in an unsorted list Every item must be looked at once.
  9. O(1) Reading an array element by its index The address is worked out directly, however long the array.
  10. O(log n) Searching a balanced binary search tree Each step down a balanced tree halves what is left.
  11. O(n) Printing every item of a list One step for each item is linear.
  12. O(n squared) Insertion sort on a reversed list Each item has to move past every item before it.

Code Word

  1. PIVOT The item quicksort uses to split the data into smaller and larger values.
  2. DIJKSTRA His shortest path algorithm cannot handle negative edge weights.
  3. 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

  1. Binary search is O(n) because it checks every item. Binary search halves the range at every comparison, so it is O(log n).
  2. 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.
  3. 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.
  4. Dijkstra uses a heuristic estimate. Dijkstra uses no heuristic. A star adds one, choosing the node with the lowest f = g + h.
  5. 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

  1. Bubble is the odd one out: it goes with Sorting algorithms. The other three: Searches.
  2. Binary is the odd one out: it goes with Searches. The other three: Sorting algorithms.
  3. Depth first is the odd one out: it goes with Searches. The other three: Shortest path ideas.
  4. Quick is the odd one out: it goes with Sorting algorithms. The other three: Searches.
  5. A star is the odd one out: it goes with Shortest path ideas. The other three: Sorting algorithms.
  6. Merge is the odd one out: it goes with Sorting algorithms. The other three: Shortest path ideas.

Memory Flash

  1. Binary search Halves a sorted range each comparison
  2. Dijkstra Shortest paths with non negative weights
  3. Merge sort Splits to single items, then merges back
  4. Quicksort Partitions the items around a pivot
  5. Linear search Checks each item in turn
  6. Bubble sort Swaps adjacent pairs that are out of order

Fact or Fib

  1. Fact Breadth first search uses a queue.
  2. 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.
  3. 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.
  4. Fact Dijkstra's algorithm finds shortest paths from one start node.
  5. Fact Binary search needs the data to be sorted.
  6. Fact Merge sort takes O(n log n) time in every case.
  7. Fact An admissible heuristic never overestimates the remaining cost.
  8. 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.
  9. 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).
  10. 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.
  11. 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.
  12. Fact Merge sort is O(n log n) whatever the order of the data.