Sorting Algorithms
Aligned to the AQA 7517 specification
- Level
- Intermediate
- Reading time
- 6 min
- Published
- 13 June 2026
- Updated
- 1 July 2026
On this page
Key takeaways
- Bubble sort repeatedly compares adjacent elements and swaps them if out of order; it runs in O(n²) time, is in-place at O(1) space, and is stable.
- Merge sort is a divide-and-conquer algorithm that runs in O(n log n) time in all cases but needs O(n) extra space for an auxiliary array; it is also stable.
- Optimised bubble sort adds a swapped flag so it terminates early when a pass makes no swaps, giving best-case O(n) on an already-sorted array.
- For large datasets merge sort beats bubble sort because O(n log n) grows far slower than O(n²); basic bubble sort always runs n-1 passes.
- A sort is stable if elements with equal keys keep their original relative order; both bubble sort and merge sort are stable.
Studying this for an exam?
Generate a personalised learning path for this subject. Free to get started.
Key terms
- Bubble sort
- A sorting algorithm that repeatedly compares adjacent elements and swaps them if they are in the wrong order, bubbling the largest unsorted element to the end on each pass.
- Merge sort
- A divide-and-conquer sorting algorithm that recursively splits the array in half until single elements remain, then merges sorted sub-arrays back together.
- Divide and conquer
- An approach that splits a problem into smaller sub-problems, solves them, and combines the results; merge sort divides the array then conquers by merging.
- Stable sort
- A sorting algorithm in which elements with equal keys retain their original relative order after sorting.
- In-place
- Describes a sort that rearranges the original array using only a constant amount of extra memory, as bubble sort does with a single temp variable.
- Swapped flag
- A boolean used in optimised bubble sort that, if a complete pass makes no swaps, signals the array is already sorted so the algorithm can stop early.
- Base case
- The condition in a recursive algorithm that returns without recursing; in merge sort it is a sub-array of length one or zero, which is trivially sorted.
Frequently asked questions
Merge sort is faster for large datasets. It runs in O(n log n) in all cases, while bubble sort is O(n²). For a 1000-element array bubble sort may need up to 10⁶ comparisons versus about 10,000 for merge sort.
Merge sort is recursive, so without the base case IF LEN(arr) <= 1 THEN RETURN arr it would recurse indefinitely. The base case (a single element is trivially sorted) is what makes the recursion terminate.
No. Bubble sort is in-place: it rearranges the original array using only a single temp variable, giving O(1) space. Merge sort, by contrast, requires an extra array for merging at O(n) space.
Generate revision on any topic you study
Type any topic you're studying and Aicademy generates a complete lesson, quiz, and flashcard set, personalised to your level.
Lessons on anything
Structured, level-matched lessons on any topic you study
Practice quizzes
Find out what you actually know before the exam does
Flashcard sets
Lock in key concepts with instant revision cards
Ask Aica
Stuck on something? Get a clear explanation, any time
Searching Algorithms
Dijkstra's Shortest Path Algorithm
Related lessons
5 min
6 min