Signed up recently but had issues with the confirmation email?

Click Forgot password and follow the reset steps. You can choose the same password you used when you signed up. This should then provide access.

Remember to check Spam and Junk. The email will be from auth@progressbytes.co.uk.

Revision notesQuestionsFor schools
Revise Computing
Log inStart revising free
Revision notesQuestionsFor schoolsLog in

© 2026 Revise Computing · revisecomputing.co.uk

Worried about something?Question Bank UpdatesPrivacy noticeTermsContact

Revise Computing is an independent revision resource. It is not affiliated with, or endorsed by, OCR (Oxford Cambridge and RSA Examinations).

Knowledge/A Level/H446/02 Algorithms and programming/Algorithms/Standard algorithms: sorts, Dijkstra's shortest…

Standard algorithms: sorts, Dijkstra's shortest path, A*, binary search and linear search

Standard algorithms (bubble sort, insertion sort, merge sort, quick sort, Dijkstra’s shortest path algorithm, A* algorithm, binary search and linear search).

2.3.1 Algorithms

Practise exam questions

Algorithms — Note 6 of 6

Candidates need to understand the need for searching and sorting algorithms and the pre-conditions required for each. Candidates need to be able to perform, and read, trace and write code for, bubble sort, insertion sort, merge sort, quick sort, binary search and linear search. Candidates need to be able to calculate the shortest path using Dijkstra’s shortest path algorithm and the A* algorithm, and read and trace code that performs them.

What you need to know

You need to understand how each standard algorithm works, be able to carry it out by hand on a set of data, and be able to read, trace and write code for the searching and sorting algorithms. For Dijkstra's and A*, you need to read and trace code rather than write it.

What to know

  • Searching and sorting algorithms may have preconditions, e.g. binary search needs SORTED data.
  • Linear search: check each item in order until the item is found or the end is reached. Works on unsorted data. O(n) on average and in the worst case.
  • Binary search: compare the target with the middle item; discard the half that cannot contain it; repeat until found or no items remain. Needs sorted data. O(log n) on average and in the worst case.
  • Bubble sort: repeatedly pass through the list, swapping adjacent items that are in the wrong order, until a pass makes no swaps. O(n²) on average and in the worst case.
  • Insertion sort: take each item in turn and insert it into the correct position within the sorted part of the list. O(n²) on average and in the worst case, but O(n) on already sorted data and fast for nearly sorted data.
  • Merge sort: split the list in half repeatedly until each part has one item, then merge pairs of sorted lists back together in order. O(n log n), needs extra memory.
  • Quick sort: choose a pivot; partition the list into items smaller than and larger than the pivot; recursively sort each part. Average O(n log n), worst O(n²).
  • Dijkstra's algorithm: finds the shortest path from a start node to every other node in a weighted graph. Repeatedly visit the unvisited node with the smallest known distance, and update the distances to its neighbours if a shorter route is found.
  • A* algorithm: finds the shortest path (guaranteed only if h never overestimates) using f = g + h, where g is the actual cost from the start and h is a heuristic estimate of the remaining cost. The node with the lowest f is explored next, so A* usually explores fewer nodes than Dijkstra's. A* does not stop when the destination is first discovered. It continues exploring until the destination has the lowest f-score of all open nodes. It then stops without needing to check the destination's other connections or visit every node.

Code examples

Binary search (iterative) in OCR pseudocode
text
function binarySearch(list, target)
    low = 0
    high = list.length - 1
    while low <= high
        mid = (low + high) DIV 2
        if list[mid] == target then
            return mid
        elseif list[mid] < target then
            low = mid + 1
        else
            high = mid - 1
        endif
    endwhile
    return -1
endfunction
Bubble sort with a swapped flag
python
def bubble_sort(data):
    swapped = True
    passes = 0
    while swapped:
        swapped = False
        for i in range(len(data) - 1 - passes):
            if data[i] > data[i + 1]:
                # Python swaps in one line; in an exam, swap through a temporary variable
                data[i], data[i + 1] = data[i + 1], data[i]
                swapped = True
        passes += 1
    return data
Insertion sort
python
def insertion_sort(data):
    for i in range(1, len(data)):
        current = data[i]
        pos = i
        while pos > 0 and data[pos - 1] > current:
            data[pos] = data[pos - 1]   # shift larger items right
            pos -= 1
        data[pos] = current
    return data
Merge sort
python
def merge_sort(data):
    if len(data) <= 1:
        return data
    mid = len(data) // 2
    left = merge_sort(data[:mid])
    right = merge_sort(data[mid:])
    result = []
    i = j = 0
    while i < len(left) and j < len(right):
        if left[i] <= right[j]:
            result.append(left[i])
            i += 1
        else:
            result.append(right[j])
            j += 1
    return result + left[i:] + right[j:]
Quick sort
python
def quick_sort(data):
    if len(data) <= 1:
        return data
    pivot = data[0]
    smaller = [x for x in data[1:] if x < pivot]
    larger = [x for x in data[1:] if x >= pivot]
    return quick_sort(smaller) + [pivot] + quick_sort(larger)
Dijkstra's shortest path algorithm
python
def dijkstra(graph, start):
    # graph: {node: {neighbour: weight}}
    distances = {node: float("inf") for node in graph}
    previous = {node: None for node in graph}
    distances[start] = 0
    unvisited = list(graph)
    while unvisited:
        current = min(unvisited, key=lambda n: distances[n])
        unvisited.remove(current)
        for neighbour, weight in graph[current].items():
            new_distance = distances[current] + weight
            if new_distance < distances[neighbour]:
                distances[neighbour] = new_distance
                previous[neighbour] = current
    return distances, previous

def final_path(previous, goal):
    # follow previous nodes back from the goal, then reverse
    path = []
    node = goal
    while node is not None:
        path.append(node)
        node = previous[node]
    path.reverse()
    return path

graph = {"A": {"B": 4, "C": 1}, "B": {"D": 1},
         "C": {"B": 2, "D": 5}, "D": {}}
distances, previous = dijkstra(graph, "A")
print(distances)                  # {'A': 0, 'B': 3, 'C': 1, 'D': 4}
print(previous)                   # {'A': None, 'B': 'C', 'C': 'A', 'D': 'B'}
print(final_path(previous, "D"))  # ['A', 'C', 'B', 'D']

Exam technique

  • Tip:To describe bubble sort fully, say that two items are swapped using a temporary variable, and that a flag records whether a pass made any swap. Without the flag, the sort can't tell it has finished, so it makes every pass even on sorted data.
  • Tip:In a trace table for a sort, record every change to the loop counter, the swap flag and the temporary variable, not just the array. In 2026 many students lost marks by tracing the loop wrongly or skipping it.
  • Mistake:Doing a different sort from the one named (e.g. bubble sort when asked for insertion sort) scores nothing. In insertion sort the first item starts as the sorted part; in merge sort the list is split to single items and all the sorting happens while merging.
  • Tip:For Dijkstra's and A*, show the table of distances (and heuristics) at each step, and the order in which nodes are visited. Only write distances the algorithm actually works out: rows for routes it would never calculate are not credited.
  • Mistake:Stopping Dijkstra's algorithm as soon as the destination gets a distance. That distance is only tentative: it is confirmed only when the destination is chosen as the unvisited node with the smallest tentative distance. In exams, complete the table for every node unless told otherwise, showing distances as they are replaced.
  • Mistake:Saying Dijkstra's algorithm tries every possible route, or that A*'s heuristic makes its answer more accurate. Neither tries every route. Dijkstra's isn't guided towards the goal, so it usually visits every (or nearly every) node before reaching it. The heuristic USUALLY makes A* faster, by exploring the most promising routes first; it doesn't make the result more accurate.
  • Mistake:Using binary search on unsorted data.

Past exam questions

  • June 2026Paper 2Q2(b)7marks
  • June 2026Paper 2Q2(c)4marks
  • June 2026Paper 2Q3(a)3marks
  • June 2026Paper 2Q3(b)4marks
  • June 2025Paper 2Q1(a)(i)5marks
  • June 2025Paper 2Q1(a)(ii)2marks
  • June 2025Paper 2Q1(b)5marks
  • June 2025Paper 2Q1(c)(iii)3marks
  • June 2025Paper 2Q1(c)(iv)2marks
  • June 2025Paper 2Q5(b)7marks
  • June 2025Paper 2Q5(c)2marks
  • June 2024Paper 2Q3(a)7marks
  • June 2024Paper 2Q6(a)3marks
  • June 2024Paper 2Q6(c)5marks
  • June 2023Paper 2Q1(b)6marks
  • June 2023Paper 2Q7(c)(i)5marks
  • June 2022Paper 2Q1(a)(i)1mark
  • June 2022Paper 2Q1(a)(ii)4marks
  • June 2022Paper 2Q3(b)5marks
  • June 2022Paper 2Q3(c)4marks
  • June 2022Paper 2Q5(c)(i)6marks
  • June 2022Paper 2Q5(c)(ii)2marks
  • November 2021Paper 2Q1(c)(i)2marks
  • November 2021Paper 2Q1(c)(ii)8marks
  • November 2021Paper 2Q2(a)(i)6marks
  • November 2021Paper 2Q3(a)5marks
  • November 2020Paper 2Q3(b)1mark
  • November 2020Paper 2Q4(b)3marks
  • November 2020Paper 2Q4(d)6marks
  • June 2019Paper 2Q1(b)(iii)5marks
  • June 2019Paper 2Q4(c)6marks
  • June 2019Paper 2Q5(a)6marks
  • June 2018Paper 2Q3(b)7marks
  • June 2018Paper 2Q3(c)9marks
  • June 2018Paper 2Q5(d)(i)5marks
  • June 2018Paper 2Q5(d)(ii)7marks
  • June 2017Paper 2Q1(a)(i)3marks
  • June 2017Paper 2Q1(a)(ii)6marks

Ready to test yourself?

Practise 2.3.1 questions
← Previous noteAlgorithms for stacks, queues, trees, linked lists, depth-first (post-order) and breadth-first traversal2.3.1 Algorithms

Back to Algorithms

Algorithms

Note 6 of 6

  1. 1Analysis and design of algorithms for a given situation
  2. 2The suitability of different algorithms for a given task and data set, in terms of execution time and space
  3. 3Measures and methods to determine efficiency, Big O notation
  4. 4Comparison of the complexity of algorithms
  5. 5Algorithms for stacks, queues, trees, linked lists, depth-first (post-order) and breadth-first traversal
  6. 6Standard algorithms: sorts, Dijkstra's shortest path, A*, binary search and linear search
Topic overview