You are viewing a free preview of this lesson.
Subscribe to unlock all 10 lessons in this course and every other course on LearningBro.
This lesson compares the three standard sorting algorithms — bubble sort, merge sort, and insertion sort — as required by OCR J277 Section 2.2. The OCR GCSE Computer Science exam frequently asks students to compare these algorithms, evaluate their efficiency, and recommend the most appropriate one for a given scenario.
| Feature | Bubble Sort | Insertion Sort | Merge Sort |
|---|---|---|---|
| How it works | Swaps adjacent elements | Inserts into sorted portion | Divide and conquer |
| Best case | O(n) | O(n) | O(n log n) |
| Worst case | O(n²) | O(n²) | O(n log n) |
| Average case | O(n²) | O(n²) | O(n log n) |
| Space complexity | O(1) — in place | O(1) — in place | O(n) — extra memory |
| Stable? | Yes | Yes | Yes |
| Adaptive? | Yes (with flag) | Yes (naturally) | No |
OCR Exam Tip: Learn this comparison table. A common 4–6 mark question asks you to compare two or three of these algorithms. Always address time complexity, space complexity, and suitability for different data sizes.
Time complexity describes how the number of operations grows as the dataset gets larger. It is expressed using Big-O notation.
| Notation | Name | Description |
|---|---|---|
| O(1) | Constant | Does not change with input size |
| O(n) | Linear | Grows in proportion to input size |
| O(n²) | Quadratic | Grows as the square of input size |
| O(n log n) | Linearithmic | Between linear and quadratic |
| O(log n) | Logarithmic | Grows very slowly |
For sorting:
| n (items) | O(n²) operations | O(n log n) operations |
|---|---|---|
| 10 | 100 | ~33 |
| 100 | 10,000 | ~664 |
| 1,000 | 1,000,000 | ~9,966 |
| 10,000 | 100,000,000 | ~132,877 |
This table clearly shows why merge sort (O(n log n)) is dramatically faster than bubble sort and insertion sort (O(n²)) for large datasets.
Space complexity describes how much additional memory an algorithm needs beyond the original data.
OCR Exam Tip: If a question mentions "limited memory" or a device with "small storage", this is a hint to choose bubble sort or insertion sort over merge sort, because merge sort needs extra memory.
Understanding when each algorithm performs at its best or worst is crucial:
| Algorithm | Best Case Scenario | Worst Case Scenario |
|---|---|---|
| Bubble sort | Data already sorted — one pass, no swaps, O(n) | Data in reverse order — maximum passes and swaps, O(n²) |
| Insertion sort | Data already sorted — each element needs only one comparison, O(n) | Data in reverse order — each element must shift through entire sorted portion, O(n²) |
| Merge sort | Always O(n log n) regardless of initial order | Always O(n log n) — consistent performance |
A key advantage of merge sort is its consistent performance — it always takes O(n log n) time, regardless of whether the data is sorted, random, or reversed. Bubble sort and insertion sort can be very fast on nearly-sorted data but extremely slow on reversed data.
Best choice: Insertion sort Reason: With nearly sorted data, insertion sort runs in nearly O(n) time and has very low overhead.
Best choice: Merge sort Reason: O(n log n) is dramatically faster than O(n²) for large datasets, and the order of the data does not affect merge sort's performance.
Best choice: Bubble sort or insertion sort Reason: Both sort in place with O(1) extra space, whereas merge sort needs O(n) extra space.
Best choice: Merge sort (or insertion sort if memory is constrained) Reason: For medium to large datasets, merge sort's O(n log n) is reliable.
Question: A teacher needs to sort 50,000 student exam results. The data is currently in random order. Compare the suitability of merge sort and bubble sort for this task. (4 marks)
Answer:
OCR Exam Tip: When comparing algorithms in the exam, always structure your answer around: (1) time complexity, (2) space complexity, and (3) suitability for the given scenario. This framework ensures you cover all the key points.
flowchart TD
A([Start: choose a sorting algorithm]) --> B{Dataset size?}
B -- Small < 50 --> C{Data nearly sorted?}
C -- Yes --> D[Insertion sort]
C -- No --> E[Insertion or bubble sort]
B -- Large >= 50 --> F{Memory constrained?}
F -- Yes --> G[Insertion sort]
F -- No --> H{Worst case matters?}
H -- Yes --> I[Merge sort]
H -- No --> I
This decision tree captures the standard exam-ready reasoning. Memorise it — it will help you answer "which sort should be used?" questions quickly.
Assume a computer performs 10⁸ (100 million) operations per second. Here is an approximate comparison of how long each algorithm would take in the worst case:
| n | Bubble/Insertion (O(n²)) | Merge (O(n log n)) |
|---|---|---|
| 100 | 10,000 ops ≈ 0.0001 s | ~664 ops, negligible |
| 1,000 | 1,000,000 ops ≈ 0.01 s | ~9,966 ops, negligible |
| 10,000 | 100,000,000 ops ≈ 1 s | ~132,877 ops ≈ 0.0013 s |
| 100,000 | 10,000,000,000 ops ≈ 100 s | ~1,660,964 ops ≈ 0.017 s |
| 1,000,000 | 10¹² ops ≈ 10,000 s (2.8 hours) | ~20,000,000 ops ≈ 0.2 s |
This shows dramatically how O(n²) algorithms become unusable for large data, while O(n log n) algorithms remain practical.
Subscribe to continue reading
Get full access to this lesson and all 10 lessons in this course.