โ† Back to subjects
โญ 0
AQA A-level Computer Science (7517) ยท Fundamentals of algorithms
Mini-Lesson

Fundamentals of algorithms

This mini-lesson covers AQA 4.3 โ€” Fundamentals of algorithms: the standard algorithms you are expected to trace, code and, crucially, compare by complexity.

  • Graph traversal โ€” breadth-first and depth-first
  • Reverse Polish notation and stack evaluation
  • Searching โ€” linear, binary and binary-tree search
  • Sorting โ€” bubble and merge sort
  • Big-O โ€” comparing algorithms honestly
  • Dijkstra's shortest-path algorithm

Work through each screen, answer the questions as you go (some are recall, some are calculations you must actually work out) and collect โญ stars. Press Start when you're ready.

4.3 ยท Complexity

Big-O โ€” the language of comparison

Big-O describes how the run time (or memory) of an algorithm grows as the input size n grows. It is about the shape of the growth, not the exact number of steps: constants and lower-order terms are dropped, because for large n they stop mattering.

3nยฒ + 500n + 9000 โ†’ O(nยฒ)
OrderNameExample
O(1)constantarray access, hash lookup
O(log n)logarithmicbinary search
O(n)linearlinear search
O(n log n)linearithmicmerge sort
O(nยฒ)polynomialbubble sort
O(2โฟ)exponentialbrute-force subsets
O(n!)factorialbrute-force travelling salesman

Read it properly: O(nยฒ) means "grows no faster than n squared" โ€” it is an upper bound on the worst case. Doubling n makes an O(nยฒ) algorithm take four times as long, but an O(log n) algorithm take just one extra step.

4.3 ยท Searching

Linear and binary search

Linear search inspects each element in turn until it finds the target or runs off the end. It works on any list, sorted or not. Worst case: n comparisons โ€” O(n).

Binary search requires the list to be sorted. It checks the middle item; if the target is smaller it discards the whole upper half, otherwise the lower half. Each comparison halves the search space.

low โ† 0 : high โ† n-1 WHILE low <= high mid โ† (low + high) DIV 2 IF a[mid] = target THEN RETURN mid ELSE IF a[mid] < target THEN low โ† mid + 1 ELSE high โ† mid - 1 ENDWHILE RETURN -1
max comparisons = โŒˆlogโ‚‚(n + 1)โŒ‰1 000 000 items โ†’ only 20 comparisons

Binary tree search is the same halving idea on a BST: at each node go left if smaller, right if larger. O(log n) on a balanced tree; O(n) if the tree has degenerated into a chain.

The catch: binary search needs sorted data, and sorting costs O(n log n). Sorting once to search many times is a win. Sorting to perform a single search is not.

Calculate

Your turn โ€” binary search

1What is the maximum number of comparisons a binary search needs on a sorted list of 1000 items?
comparisons
Hint: Each comparison halves the list. Find the smallest k with 2^k greater than or equal to 1001. Try 2^9 = 512 and 2^10 = 1024.
Quick check

Search preconditions

?A list of 5000 names is in random order and a single name must be found once. Which is the better algorithm?
4.3 ยท Sorting

Bubble sort and merge sort

Bubble sort makes repeated passes, comparing adjacent pairs and swapping them if they are out of order. After pass 1 the largest item has bubbled to the end, so each pass can examine one fewer element.

comparisons = (nโˆ’1) + (nโˆ’2) + โ€ฆ + 1 = n(nโˆ’1)/2 โ†’ O(nยฒ)
  • Best case (already sorted, with a "swap made?" flag): one pass, O(n).
  • Worst / average: O(nยฒ). Space: O(1) โ€” it sorts in place.

Merge sort is divide and conquer: split the list in half, recursively sort each half, then merge the two sorted halves by repeatedly taking the smaller of the two front elements.

  • Splitting produces logโ‚‚ n levels; each level merges all n items โ†’ O(n log n) in every case.
  • Space: O(n) โ€” it needs a temporary array. Bubble sort does not.

The honest trade-off: merge sort is far faster on large data but uses extra memory and is harder to code. Bubble sort is trivial to write and needs no extra space, but is unusable beyond a few thousand items. There is no free lunch โ€” you trade time against space.

Calculate

Your turn โ€” bubble sort comparisons

2How many comparisons does a standard bubble sort make in total on a list of 8 items in the worst case?
comparisons
Hint: Pass 1 makes 7 comparisons, pass 2 makes 6, and so on down to 1. Add them up โ€” or use n(nโˆ’1)/2.
Calculate

Your turn โ€” merge sort levels

3Merge sort is run on a list of 32 items. How many levels of splitting are needed before every sublist contains exactly one item?
levels
Hint: 32 โ†’ 16 โ†’ 8 โ†’ 4 โ†’ 2 โ†’ 1. Count the arrows. That is logโ‚‚ 32.
Quick check

Sorting complexity

?Which statement about merge sort is correct?
4.3 ยท Graph traversal

Breadth-first and depth-first search

Both traversals visit every reachable vertex exactly once; both keep a visited set. The only difference is the structure holding the frontier:

Breadth-first (BFS)Depth-first (DFS)
Uses a queue (FIFO)Uses a stack (LIFO), or recursion
Visits all neighbours, then their neighbours โ€” expanding in ringsPlunges as deep as possible, then backtracks
Finds the shortest path in an unweighted graphNatural for maze solving, topological sort, cycle detection
Can use a lot of memory (whole frontier queued)Memory proportional to the depth

Both are O(V + E) using an adjacency list.

Exam trap: BFS finds the fewest edges, not the lowest cost. On a weighted graph the fewest-edges route can easily be the most expensive one โ€” that is exactly why Dijkstra exists.

4.3 ยท Optimisation

Dijkstra's shortest-path algorithm

Dijkstra finds the shortest path from one source vertex to every other vertex in a graph with non-negative weights.

  • Set the source's distance to 0 and every other to โˆž. Mark all unvisited.
  • Take the unvisited vertex with the smallest distance (a priority queue does this).
  • Relax each of its edges: if distance(current) + weight < distance(neighbour), update the neighbour and record the current vertex as its predecessor.
  • Mark the current vertex visited โ€” its distance is now final. Repeat.

Use the predecessor pointers backwards from the destination to reconstruct the route.

Why non-negative weights matter: Dijkstra assumes that once a vertex is finalised no shorter route to it can appear. A negative edge could later reduce that distance, breaking the assumption โ€” so Dijkstra is simply wrong on graphs with negative weights. A* is Dijkstra plus a heuristic estimate of the remaining distance, which steers the search towards the goal and explores far fewer vertices.

Calculate

Your turn โ€” Dijkstra

4Run Dijkstra from A on this weighted, undirected graph. What is the length of the shortest path from A to E?
EdgeWeightEdgeWeight
A โ€“ B4C โ€“ D8
A โ€“ C2C โ€“ E10
B โ€“ C1D โ€“ E2
B โ€“ D5B โ€“ E12
Hint: Aโ†’C costs 2. Is Aโ†’Cโ†’B (2+1) cheaper than Aโ†’B (4)? Then find the cheapest way to D, and finally to E. Do not just take the direct edge.
Quick check

Dijkstra

?Why does Dijkstra's algorithm fail on a graph containing a negative edge weight?
4.3 ยท RPN

Reverse Polish notation

Reverse Polish (postfix) notation puts the operator after its operands: 3 4 + instead of 3 + 4.

Its virtues:

  • No brackets are ever needed;
  • no precedence rules are needed;
  • it is evaluated by a single left-to-right pass using a stack.

Evaluation: read left to right. Push every operand. On meeting an operator, pop two values, apply the operator (second-popped op first-popped), and push the result.

Evaluate: 6 2 / 3 4 * + 6 push 6 stack: 6 2 push 2 stack: 6 2 / pop 2, pop 6 โ†’ 3 stack: 3 3 push 3 stack: 3 3 4 push 4 stack: 3 3 4 * pop 4, pop 3 โ†’ 12 stack: 3 12 + pop 12, pop 3 โ†’ 15 stack: 15

Link back: the post-order traversal of an expression tree is the reverse Polish form. That is not a coincidence โ€” both put the operator after its operands.

Calculate

Your turn โ€” evaluate the RPN

5Using a stack, evaluate the reverse Polish expression 5 3 4 * + 2 โˆ’.
Hint: Work left to right. The first operator you meet is *, acting on 3 and 4. Then + acts on 5 and that result. Then โˆ’ subtracts 2.
Quick check

Graph traversal

?A puzzle solver must find the route through an unweighted maze grid that uses the fewest moves. Which traversal guarantees it?
Sort it

Sort the complexities

Tap an operation, then tap its order of complexity.

๐Ÿชถ O(log n)

โž– O(n)

๐Ÿข O(nยฒ)

Match it

Match the algorithm to its description

Tap an item on the left, then its partner on the right.

Description
Algorithm
Recap

The big ideas to know

Big-O: O(1) ยท O(log n) ยท O(n) ยท O(n log n) ยท O(nยฒ) ยท O(2โฟ) ยท O(n!) โ€” drop constants and lower terms

Searching: linear O(n), any data ยท binary O(log n), needs sorted data ยท BST O(log n) balanced

Sorting: bubble O(nยฒ) time, O(1) space ยท merge O(n log n) always, O(n) space

Traversal: BFS = queue = fewest edges ยท DFS = stack = deep then backtrack ยท both O(V + E)

Dijkstra: relax edges from the closest unvisited vertex; non-negative weights only; A* adds a heuristic

RPN: operator after operands ยท no brackets or precedence ยท evaluate with a stack ยท equals post-order

That is the whole of AQA 4.3. Press Finish to see your score.

๐Ÿ†

Mini-lesson complete!

โญโญโญ

You've worked through Fundamentals of algorithms for AQA A-level Computer Science (7517). ๐ŸŽ‰

Your stars: 0 / 0

Next: test yourself in the Evaluate stage Confidence Quiz, then lock it in with Verify.

๐Ÿ“ฃ Smashed it? Share your score

Challenge a mate to beat your stars, or show a parent how you got on.

โ†’ Back to all subjects