GCSE Computer Science · public explanation

Sorting Algorithms

Follow bubble, merge and insertion sorts through worked examples. Track each change to the data and check which algorithms your GCSE board requires.

Read the explanation and worked examples, then choose your exam board to continue with activities and practice in your account.

OCR J277On the specificationOCR J277 2.1.3

Apply sorting algorithms using OCR Exam Reference Language or a studied language only when requested.

AQA 8525 (2027+)On the specificationAQA 8525 (2027+) 3.1.4

Apply sorting algorithms in the precise response format printed on the paper.

Sort the values, without losing or inventing an item

Sorting rearranges a collection into a stated order. In this worked checkpoint example, [14, 5, 9, 3, 12] must become [3, 5, 9, 12, 14]. Ascending means smallest to largest. Descending reverses that ordering. An array index names a position, starting at 0 here; the value at that position can change when items move.

The destination is easy to recognise, but an exam may ask for the route: a comparison, a completed pass, a split or an intermediate merge. Several methods can reach the same final list, so a final sorted answer does not prove that a requested intermediate row is correct.

Bubble sort repeatedly checks neighbours. Merge sort splits the problem, then builds ordered results from smaller ordered inputs. Both work on an initially unsorted list; do not confuse them with binary search, which requires ordered input. Every original item must still be present at the end, including repeated equal values.

AQA focus

AQA: bubble and merge

For AQA 8525, understand the mechanisms of bubble and merge sort and compare their advantages and disadvantages. The worked traces and questions here assess those methods. Supplied algorithm questions use AQA pseudocode; follow the response form requested by the question.

Bubble sort: compare two neighbours, then decide whether to swap

A comparison asks whether the left value is greater than its right neighbour. If it is, swap the two for ascending order. If not, leave them in place. Then move one position right. Work from the changed list, not from the original list each time.

Start with 14 and 5 at positions 0 and 1. They are out of order, so the first swap gives [5, 14, 9, 3, 12]. The next pair is now 14 and 9, not 5 and 9: the next comparison uses positions 1 and 2. In a swap, a temporary variable preserves one value while an array position is overwritten.

Pair of positionsTwo values BEFORE comparisonActionWhole list AFTER comparison
0 and 114 and 5Swap[5, 14, 9, 3, 12]
1 and 214 and 9Swap[5, 9, 14, 3, 12]
2 and 314 and 3Swap[5, 9, 3, 14, 12]
3 and 414 and 12Swap[5, 9, 3, 12, 14]

A pass is several comparisons, not one swap

A pass scans the currently active neighbouring pairs from left to right. In the first pass, the largest value moves to the far right. In this example that is 14. The remaining values are [5, 9, 3, 12], so the first pass has not sorted the whole list.

The version used here shortens the active region after each pass: the final 14 no longer needs comparison. A swapped flag starts False for each pass and becomes True if any pair swaps. A COMPLETE pass with no swaps confirms the remaining neighbouring pairs are in order, so the algorithm stops.

The table includes the final no-swap verification pass. A different specified implementation might use a fixed number of passes or scan the full list each time. Read the question's convention before counting work; do not silently mix versions.

Completed passWhole listSwaps this passValue comparisons this pass
1[5, 9, 3, 12, 14]44
2[5, 3, 9, 12, 14]13
3[3, 5, 9, 12, 14]12
4[3, 5, 9, 12, 14]01

Merge sort: split first, but do not call the halves sorted yet

Merge sort divides the list into smaller parts until each has at most one item. A one-item list is already ordered: there is no neighbouring pair out of order. Splitting the original list in two does not sort either half.

For an odd-sized list, this worked version puts the smaller half on the left. Thus [14, 5, 9, 3, 12] splits into [14, 5] and [9, 3, 12]. Other explicitly stated split conventions can also be valid; preserve the convention shown in a question.

After the splits, merge ordered child results upwards. [14] and [5] become [5, 14]. [3] and [12] become [3, 12], then [9] and [3, 12] become [3, 9, 12]. Only then can the final two ordered inputs be merged.

Part being splitLeft childRight child
[14, 5, 9, 3, 12][14, 5][9, 3, 12]
[14, 5][14][5]
[9, 3, 12][9][3, 12]
[3, 12][3][12]

Merge two ordered inputs using two front pointers

A pointer here is the index of the next unused item in one input. In the final merge, L = [5, 14] and R = [3, 9, 12]. Both pointers begin at index 0 in their OWN input. Compare L[0] = 5 with R[0] = 3. Copy 3 to the output, and advance only the right pointer.

Now compare 5 with 9. Copy 5 and advance only the left pointer. The next pair is 14 and 9; copy 9. Then compare 14 and 12; copy 12. The right input is exhausted. END below means there is no unused item in that input, not an actual value to compare.

Copy the remaining 14 from the left input without another value comparison. If both front values are equal, this version copies the left one first, keeping earlier equal items before later equal items. Nothing is dropped, and appending a leftover tail preserves its existing order.

L index BEFORER index BEFOREFront valuesActionOutput AFTER
005 and 3Copy from right; advance only that pointer[3]
015 and 9Copy from left; advance only that pointer[3, 5]
1114 and 9Copy from right; advance only that pointer[3, 5, 9]
1214 and 12Copy from right; advance only that pointer[3, 5, 9, 12]
1END14 and ENDCopy from left; no comparison[3, 5, 9, 12, 14]

OCR focus

OCR insertion sort: hold a key and grow the ordered prefix

OCR also requires insertion sort. A prefix is the beginning part of a list. Initially the first item alone is an ordered prefix. Lift the next item as the key, keep it safe separately, shift larger prefix values one position right, then insert the key into the gap.

Use the same checkpoint list. Holding 5 lets 14 shift right, then 5 goes at position 0. Holding 9 next shifts 14, but not 5. When key 3 is held, shift 14, 9 and 5 in that order, then insert 3 at the start. The final key 12 shifts 14 and is inserted before it.

The held key has not vanished. During a shift trace, show it separately from the array's empty slot. Equal preceding values do not shift in this version because only values strictly greater than the key move. The prefix grows by one after each completed insertion.

Key heldValues shifted right, in orderWhole list AFTER insertion
514[5, 14, 9, 3, 12]
914[5, 9, 14, 3, 12]
314, 9, 5[3, 5, 9, 14, 12]
1214[3, 5, 9, 12, 14]

Continue with your exam board

The course combines lesson videos, interactive activities, topic questions and original exam mocks. Sign in to use the learning tools and save your progress; your account’s course access applies.

Independent learning resources. Not endorsed by AQA or OCR. These public introductions complement the full course and the official specification.