GGCSE Revision Hub

3.1.4 Sorting algorithms

AQA Computer Science 8525Topic 3.1 — Fundamentals of algorithmsFoundation & Higher

An algorithm is a sequence of steps that can be followed to complete a task. This topic is about how algorithms are written down, how they are built out of smaller parts, and how you judge whether one is better than another. It is the foundation of Paper 1: almost every programming question assumes you can do what is set out here. Learn it as a set of tools for thinking — decomposition, abstraction, tracing and comparing — rather than as a list of definitions.

What you need to know

What a sort does

A sorting algorithm puts a set of data into order. Two are on the specification: merge sort and bubble sort. Sorting is also the reason binary search needs its data prepared, so the two topics are linked: the cost of sorting is part of the cost of searching.

Key fact: A sorting algorithm puts data into order, and the cost of sorting affects the cost of searching.

How bubble sort works

Bubble sort compares adjacent items and swaps them if they are in the wrong order, repeatedly passing through the list until a whole pass makes no swaps. Each pass moves the largest remaining item to the end, so the list is sorted after n-1 passes. It is simple to describe and to trace, and that is why it appears in exams.

Key fact: Bubble sort repeatedly swaps adjacent out-of-order items and makes n-1 passes at most.

How merge sort works

Merge sort is a divide-and-conquer algorithm. It splits the list into halves, sorts each half recursively, and then merges the two sorted halves back together by comparing the front items of each and taking the smaller. The splitting stops when each piece has one item, which is trivially sorted, and the merging rebuilds the list in order.

Key fact: Merge sort splits the list in half, sorts each half, then merges the sorted halves.

How many comparisons each takes

Bubble sort takes roughly n²/2 comparisons — for 100 items about 5000 comparisons. Merge sort takes about n log2(n) — for 100 items about 700. So on a large list merge sort is dramatically faster, and the gap widens as the list grows, because bubble sort grows with the square of n while merge sort grows with n times log n.

Key fact: Bubble sort is about n²/2 comparisons; merge sort is about n log2(n) — about 700 on 100 items.

Advantages and disadvantages

Merge sort is fast on large lists and its running time is predictable — it always takes the same number of comparisons for a given size. Its disadvantage is that it needs extra space to hold the halves while it works. Bubble sort is very simple, needs almost no extra space, and works on data that is nearly sorted, but it is slow on anything large.

Key fact: Merge sort: fast, predictable, but needs extra space. Bubble sort: simple, low on space, but slow on large lists.

Which one to recommend

A recommendation answer needs the same shape as a search answer: name the algorithm, give the number of comparisons or passes, and give the condition. 'Merge sort, because about 700 comparisons instead of about 5000 on 100 items, unless memory is very tight.' That last clause shows you understand the trade-off, and it is usually where the top band lies.

Key fact: Recommend with the numbers and the condition, and mention the trade-off.

Flashcards

Exam questions

Written in the style of AQA Computer Science, with a mark scheme for each one. No question here is a real past paper question.

mcq1 mark

Which sorting algorithm is likely to take fewer comparisons on a list of 1000 items?

  • Merge sort, because it grows with n log n rather than n squared
  • Bubble sort, because it compares adjacent items
  • Bubble sort, because it needs no extra memory
  • They always take the same number of comparisons

Mark scheme: 1 mark for choosing merge sort. Bubble sort takes roughly n²/2 comparisons and merge sort roughly n log2(n), so on 1000 items merge sort takes about 10,000 comparisons against about 500,000.

short3 marks

Explain in English how merge sort works, and why it is called a divide-and-conquer algorithm.

Mark scheme: 1 mark for explaining that the list is split into halves which are then sorted separately. 1 mark for explaining that the two sorted halves are then merged back together by repeatedly taking the smaller of the two front items. 1 mark for explaining that the process repeats until each piece has a single item, which is why it is called divide and conquer.

six6 marks

Explain in English the differences between bubble sort and merge sort, and recommend one for a large list on a device with limited memory.

Mark scheme: Level 1 (1-2 marks): A short answer saying merge sort is faster but needs more memory. Level 2 (3-4 marks): A structured answer covering the mechanics and comparison counts of each algorithm. Level 3 (5-6 marks): A detailed answer describing how bubble sort repeatedly swaps adjacent items until a pass makes no swaps, needing up to n-1 passes and about n²/2 comparisons, describing how merge sort splits, sorts recursively and merges, needing about n log2(n) comparisons, explaining that merge sort's disadvantage is the extra space needed for the halves while bubble sort needs almost none, and concluding with a justified recommendation that names the trade-off rather than simply choosing one.

Other sections in this topic

Practise these questions

Answer them in the interactive view and they get marked against the same mark scheme, one mark at a time, with the reason for each mark.

Open the interactive topic →