Revisionโ€บOCR A Levelโ€บAlgorithms
OCR A Level H446 ยท Topic 2.3

Algorithms

60 practice questions

Practice Questions

60 questions

Explain how binary search works and state the precondition.

medium

What is a linear search and when is it appropriate?

easy

Trace binary search for target=7 in [1,3,5,7,9,11]. State mid index each step.

medium

Compare the number of comparisons needed by linear and binary search on a sorted list of 1024 items (worst case).

medium

Trace a linear search for the value 7 in the list [3, 9, 7, 2, 5]. How many comparisons are made before it is found?

easy

Explain why binary search cannot be used directly on a linked list in the same efficient way as on an array.

hard

What precondition must be satisfied before binary search can be used on a list?

easy

Trace a linear search for the value 99 in the list [3, 9, 7, 2, 5], where 99 is not present. How many comparisons are made, and what does this represent?

medium

A program needs to check whether a single value exists in a small, unsorted list of 6 items, and this check will only be performed once. Which search algorithm is more appropriate?

easy

Explain why binary search has a time complexity of O(log n).

medium

How does insertion sort work?

medium

Trace bubble sort on [5,3,8,1]. Show list after each pass.

medium

Describe the divide-and-conquer approach in merge sort.

medium

What is meant by a "stable" sorting algorithm?

medium

Describe how insertion sort works, using the list [4, 2, 3] as an example of the first pass.

medium

A sorting algorithm is described as "stable". What does this mean?

hard

Merge sort is used to sort the list [8, 4, 5, 2]. Show how the list is divided, and then how it is merged back together in sorted order.

medium

Which sorting algorithm has a best-case time complexity of O(n), which occurs when the input list is already sorted?

medium

Explain how a bubble sort algorithm can be optimised to stop early if the list becomes sorted before all passes are complete.

medium

In the worst case, how many passes does a standard bubble sort make to fully sort a list of n items?

medium

Worst-case complexity of quicksort and when does it occur?

hard

How does pivot choice affect quicksort performance?

hard

Describe the steps of the quicksort algorithm.

medium

What is the average-case time complexity of quicksort?

medium

Quicksort is applied to the list [5, 1, 4, 2, 8], using the last element (8) as the pivot. Show the result of the partition step.

hard

Why might a quicksort implementation choose its pivot value randomly, rather than always using the first or last element?

medium

Explain the base case of the recursive quicksort algorithm, and why it is necessary.

medium

Which statement comparing quicksort and merge sort is correct?

hard

Explain what is meant by 'in-place' partitioning in quicksort.

medium

A quicksort implementation always chooses the first element of the (sub-)list as the pivot. What is the time complexity when this is applied to a list that is already fully sorted in ascending order?

hard

State worst-case time complexities of bubble sort, merge sort, and binary search.

medium

What makes an algorithm efficient?

easy

What is space complexity?

medium

What is the Big O notation for an algorithm that checks all pairs in a list?

medium

What does O(1) mean for an algorithm's time complexity?

easy

Which of the following correctly orders these algorithms from most to least efficient for large n: O(n), O(nยฒ), O(log n), O(1)?

medium

Explain why an algorithm's best-case time complexity is often less useful in practice than its average-case or worst-case complexity.

medium

Explain the difference between an algorithm with time complexity O(n) and one with time complexity O(n log n), giving an example of each.

medium

For very large values of n, which time complexity represents the algorithm that takes the LONGEST to run?

easy

Explain how counting the number of nested loops in an algorithm can help estimate its time complexity.

medium

What is an optimisation problem and give one example?

medium

A graph has nodes representing tasks and edges representing dependencies (task A must happen before task B). Which algorithm/technique could determine a valid order to perform all tasks?

hard

Explain what it means for a problem to be described as 'tractable'.

medium

Which of the following best describes an 'intractable' problem?

medium

Explain what is meant by the complexity classes P and NP.

hard

The Travelling Salesman Problem (finding the shortest route visiting a set of cities exactly once and returning to the start) is generally considered to be:

medium

Explain why an algorithm with exponential time complexity (e.g. O(2^n)) becomes impractical even for moderately sized inputs.

medium

Which of the following is typically given as an example of an intractable problem?

medium

Explain what is meant by a problem being 'NP-complete'.

hard

Which of the following time complexities would generally be classed as 'polynomial time' (and therefore associated with tractable problems)?

easy

What is the difference between a greedy algorithm and dynamic programming?

hard

Explain Dijkstra's shortest path algorithm.

hard

How does a pre-order depth-first search traverse a tree?

hard

What is a spanning tree of a graph and what does "minimum" mean?

hard

In Dijkstra's algorithm, why is the node with the smallest tentative distance always chosen next?

hard

Explain the difference between a depth-first and breadth-first traversal of a tree, and the data structure typically associated with each.

medium

Which algorithm would be most appropriate for finding the minimum spanning tree of a weighted, connected graph?

medium

Explain what is meant by a 'heuristic' approach to solving a problem, and when such an approach might be used.

medium

Which of the following is an example of a heuristic approach to the Travelling Salesman Problem?

medium

Discuss the trade-off between using a heuristic algorithm and an exact algorithm for a large, intractable problem.

medium

Revision Notes

Complexity & Big-O Notation

โญ Exam tip: State the Big-O AND justify it in one line โ€” e.g. "binary search is O(log n) because it halves the remaining items each comparison."
Big-O notation:Describes how an algorithm's time (or space) requirement grows as the input size n grows, ignoring constants and lower-order terms. It expresses the worst-case order of growth, letting algorithms be compared independently of the hardware they run on.
Big-ONamen=10 โ†’ n=1000 work growsโ€ฆExample
O(1)Constantno changeArray index; hash lookup (avg)
O(log n)Logarithmicbarely (โ‰ˆ 3 โ†’ 10)Binary search
O(n)Linearร—100Linear search
O(n log n)Linearithmicร—~200Merge sort, quicksort (avg)
O(nยฒ)Quadraticร—10,000Bubble / insertion sort
O(2โฟ)ExponentialastronomicallyNaive recursive Fibonacci
Time vs space complexity:Time complexity counts operations; space complexity counts extra memory used. There is often a trade-off: merge sort is O(n log n) time but needs O(n) extra space; bubble sort is slow at O(nยฒ) time but uses only O(1) extra space. The right choice depends on which resource is scarce.
Why order of growth matters
Suppose 1 operation = 1 microsecond, n = 1,000,000:
  O(n)        โ‰ˆ 1 second
  O(n log n)  โ‰ˆ 20 seconds
  O(nยฒ)       โ‰ˆ 11.5 DAYS
  O(2โฟ)       โ‰ˆ longer than the age of the universe
This is why an efficient algorithm matters far more
than a faster computer for large inputs.

P, NP and intractability

P: problems solvable in polynomial time. NP: problems where a proposed solution can be VERIFIED in polynomial time. NP-complete problems are the hardest in NP โ€” if any one had a polynomial-time solution, all of NP would (the famous open question, P = NP?). Problems with no known efficient solution are intractable; in practice we attack them with heuristics and approximation algorithms.

โš ๏ธ Common mistake: Big-O is about GROWTH, not absolute speed. An O(nยฒ) algorithm can beat an O(n log n) one for small n; Big-O tells you what happens as n gets large.

Searching & Sorting Algorithms

โญ Exam tip: Sorting/searching questions often ask for a full TRACE โ€” show the list after every pass/comparison, not just the final result.
AlgorithmBestAvg / WorstSpaceNotes
Linear searchO(1)O(n)O(1)Works on any list, sorted or not
Binary searchO(1)O(log n)O(1)Sorted list only; halves each step
Bubble sortO(n)O(nยฒ)O(1)Simple; in place; slow on large lists
Insertion sortO(n)O(nยฒ)O(1)Efficient on nearly-sorted data
Merge sortO(n log n)O(n log n)O(n)Divide & conquer; stable; needs extra space
QuicksortO(n log n)O(nยฒ) worstO(log n)Fast in practice; in place; pivot choice matters
Binary search trace: find 7 in [1,3,5,7,9,11,13]
low=0 high=6 mid=3 โ†’ list[3]=7  FOUND (1 comparison)

find 11:
 low=0 high=6 mid=3 โ†’ 7 < 11 โ†’ search right, low=4
 low=4 high=6 mid=5 โ†’ list[5]=11  FOUND (2 comparisons)

find 8 (absent):
 low=0 high=6 mid=3 โ†’ 7<8 โ†’ low=4
 low=4 high=6 mid=5 โ†’ 11>8 โ†’ high=4
 low=4 high=4 mid=4 โ†’ 9>8 โ†’ high=3
 low>high โ†’ NOT FOUND
Bubble sort โ€” first pass of [5,3,8,1]
5 vs 3 โ†’ swap โ†’ [3,5,8,1]
5 vs 8 โ†’ no swap โ†’ [3,5,8,1]
8 vs 1 โ†’ swap โ†’ [3,5,1,8]  โ† 8 now in final place
Pass 2: [3,5,1,8] โ†’ [3,1,5,8] โ†’ 5 placed
Pass 3: [3,1,5,8] โ†’ [1,3,5,8]
Pass 4: no swaps โ†’ SORTED (early exit optimisation)
Merge sort: [4,2,7,1]
SPLIT: [4,2,7,1] โ†’ [4,2] [7,1] โ†’ [4][2] [7][1]
MERGE: [4]+[2] = [2,4]   [7]+[1] = [1,7]
       [2,4] + [1,7] โ†’ compare fronts: 1,2,4,7
       = [1,2,4,7]  โœ“
๐Ÿ’ก Justify a choice with BOTH efficiency and conditions: "use binary search โ€” O(log n) โ€” but only because the data is large AND already sorted; otherwise the cost of sorting first may outweigh the benefit, so linear search could be better for a one-off search of an unsorted list."
โš ๏ธ Common mistake: Binary search on an UNSORTED list gives wrong results โ€” it is not just slow, it is incorrect. Always state the precondition that the list must be sorted.

Dijkstra's & A* Path-Finding

โญ Exam tip: A* is specific to OCR H446 โ€” be able to explain f(n)=g(n)+h(n) and why an admissible heuristic guarantees the optimal path.

Dijkstra's shortest path

Finds the shortest path from a start node to all others in a weighted graph with non-negative edge weights. It keeps a tentative shortest distance to every node (start = 0, others = โˆž), repeatedly selects the unvisited node with the smallest tentative distance, marks it visited (its distance is now final), and relaxes its neighbours (updates a neighbour's distance if going via this node is shorter). It explores outward in all directions like a flood.

Dijkstra trace (start A)
Graph: A-B=4, A-C=1, C-B=2, C-D=5, B-D=1
Init: A=0, B=โˆž, C=โˆž, D=โˆž
Visit A: B=4, C=1
Visit C (smallest=1): B=min(4,1+2)=3, D=1+5=6
Visit B (3): D=min(6,3+1)=4
Visit D (4): done
Shortest Aโ†’D = 4  (Aโ†’Cโ†’Bโ†’D)

A* search

Extends Dijkstra by adding a heuristic h(n) that estimates the cost from node n to the goal. Nodes are prioritised by f(n) = g(n) + h(n), where g(n) is the actual cost from the start so far. By steering exploration toward the goal, A* usually finds the shortest path while examining far fewer nodes than Dijkstra.

Admissible heuristic:A heuristic that NEVER overestimates the true remaining cost to the goal. If h is admissible, A* is guaranteed to find the optimal (shortest) path. Straight-line (Euclidean) distance is admissible for map navigation, because the real road route can never be shorter than the straight line.
FeatureDijkstra'sA*
Priorityg(n) โ€” cost from startf(n) = g(n) + h(n)
HeuristicNoneRequired (admissible for optimality)
Nodes exploredMore โ€” all directionsFewer โ€” guided toward the goal
Needs domain knowledge?NoYes โ€” to design a good heuristic
Use caseAny non-negative weighted graphMaps, games โ€” where a heuristic exists
๐Ÿ’ก "Why is A* usually faster than Dijkstra?" Model answer: "Dijkstra explores nodes purely by distance from the start, spreading in all directions. A* adds a heuristic estimate of the distance remaining to the goal, so it prioritises nodes that head towards the goal and therefore expands far fewer nodes, while still finding the optimal path provided the heuristic never overestimates."