Standard sorting algorithms
Computer ScienceΒ· Unit 9: Algorithm design & problem solvingΒ· 6 min read
1. Simple Quadratic-Time Sorting Algorithmsβ β ββββ± 20 min
π« No Calculator
Simple Sorting Algorithms
A group of in-place quadratic-time sorting algorithms, valued for their simplicity. Suitable for small or nearly sorted datasets, includes bubble, insertion and selection sort.
Example:
Insertion sort is often used to sort small datasets in more complex algorithms
All three simple algorithms incrementally build a sorted portion of the input array, modifying the array in-place to avoid extra memory overhead. Each algorithm has a different approach to expanding the sorted portion.
Sort the array using insertion sort, show all intermediate steps.
- 1
Start with the first element as the sorted portion
- 2
Initial state:
- 3
Take the next unsorted element 2, shift 4 right, insert 2 into the sorted portion
- 4
State after step 1:
- 5
Take next unsorted element 7, it is larger than 4, insert at the end of the sorted portion
- 6
State after step 2:
- 7
Take next unsorted element 1, shift 7, 4, 2 right, insert 1 at the start of the sorted portion
- 8
Final sorted array:
Exam tip:
When asked to count passes or swaps for bubble sort, count each full pass through the unsorted portion as one pass, not individual swaps.
2. Merge Sort: Divide-and-Conquer Sortingβ β β βββ± 25 min
π« No Calculator
Merge Sort
A stable divide-and-conquer sorting algorithm that recursively splits the input into two halves, sorts each half, then merges the two sorted halves into one sorted array.
Example:
Merge sort is ideal for sorting linked lists, where merging is cheap
Merge sort's consistent performance makes it a reliable choice for large datasets. Unlike quicksort, its time complexity is the same regardless of the initial order of the input.
Sort using merge sort, show the split and merge steps.
- 1
Split the original array into two equal halves
- 2
Split result: and
- 3
Split each half into single-element subarrays (trivially sorted)
- 4
Split result:
- 5
Merge the first pair: compare 3 and 1, resulting sorted subarray
- 6
Merge the second pair: compare 4 and 2, resulting sorted subarray
- 7
Merge the two sorted subarrays: take 1, 2, 3, 4
- 8
Final sorted array:
3. Quicksort: Partition-Based Divide-and-Conquer Sortingβ β β β ββ± 25 min
π« No Calculator
Quicksort
An in-place divide-and-conquer sorting algorithm that selects a pivot element, partitions the array into elements less than the pivot and elements greater than the pivot, then recursively sorts the partitions.
Example:
Quicksort is often the default sorting algorithm for general-purpose in-memory sorting due to its good average performance
Pivot selection is critical to quicksort's performance. Common pivot choices include first element, last element, middle element, or a random element. Poor pivot selection leads to worst-case quadratic time.
Sort using quicksort with the last element as pivot each step.
- 1
Original array: , pivot = 3
- 2
Partition: elements < 3 = , pivot, elements > 3 =
- 3
Recurse on left partition , pivot = 1
- 4
Partition: elements < 1 = , pivot = 1, elements > 1 = , sorted
- 5
Merge left partition result: $
- 6
Recurse on right partition , pivot = 6, sorted result
- 7
Merge all parts: left + pivot + right =
- 8
Final sorted array:
Exam tip:
Always show the partition step after each pivot selection when tracing quicksort, this is where most of the marks are awarded.
4. Comparing Sorting Algorithm Propertiesβ β β βββ± 20 min
π« No Calculator
When answering compare questions in the exam, you need to reference four core properties: time complexity (best/average/worst), space complexity, whether the algorithm is in-place, and whether it is stable.
Algorithm | Best Time | Average Time | Worst Time | In-place? | Stable? |
|---|---|---|---|---|---|
Bubble sort | Yes | Yes | |||
Insertion sort | Yes | Yes | |||
Selection sort | Yes | No | |||
Merge sort | No | Yes | |||
Quicksort | Yes | No |
Check your understanding of core properties:
Which algorithm always has worst-case time complexity?
Bubble sort
Merge sort
Quicksort
Insertion sort
Reveal answer
1 βCorrect! Merge sort always splits the input into equal halves, so it always has time regardless of input order.
Which of these algorithms is NOT in-place?
Quicksort
Insertion sort
Merge sort
Selection sort
Reveal answer
2 βCorrect! Standard merge sort requires extra memory to store temporary merged subarrays.
5. Common Pitfalls
Wrong move:
Claiming quicksort is always time
Why:
Quicksort degrades to time with poor pivot selection on sorted/reverse-sorted input
Correct move:
State that average time is and worst-case time is for quicksort
Wrong move:
Forgetting that standard merge sort is not in-place
Why:
This is a common exam question that tests knowledge of core algorithm properties
Correct move:
Remember merge sort requires extra memory, so it is not an in-place algorithm
Wrong move:
Counting the wrong number of passes for bubble sort
Why:
A common mistake is counting individual swaps as passes, leading to lost marks
Correct move:
A full pass processes all unsorted elements, so n elements need at most n-1 passes
Wrong move:
Claiming all in-place sorting algorithms are stable
Why:
Stability depends on swap implementation, not whether sorting is in-place
Correct move:
Memorize that selection sort and quicksort are unstable, bubble, insertion and merge are stable
Wrong move:
Merging before splitting recursively in merge sort
Why:
Many students mix up the order of operations for divide-and-conquer algorithms
Correct move:
Split the array first, recursively sort each half, then merge the sorted halves
6. Quick Reference Cheatsheet
Algorithm | Worst Time | In-place? | Stable? |
|---|---|---|---|
Bubble | Y | Y | |
Insertion | Y | Y | |
Selection | Y | N | |
Merge | N | Y | |
Quicksort | Y | N |
7. Frequently Asked
Which sorting algorithms do I need to memorize for 9618?
CIE 9618 requires full knowledge of 5 standard sorting algorithms: bubble sort, insertion sort, selection sort, merge sort and quicksort. You need to be able to trace their execution, compare properties, and write/complete pseudocode for them.
Do I need to memorize time complexity values for the exam?
Yes, you are expected to remember best, average and worst case time complexity, space complexity, and other properties for all 5 standard sorting algorithms.
When this came up on past exams
AI-estimated based on syllabus patterns β cross-check with official past papers for accuracy. Use only as revision-focus signals.
- 2022 Β· 2
Trace bubble sort execution on input
- 2023 Β· 2
Compare quicksort vs merge sort properties
- 2024 Β· 2
Identify stable sorting algorithms
Going deeper
What's Next
Standard sorting algorithms are a core foundation for all advanced algorithm design topics in CIE 9618. Understanding their complexity and properties will help you select the right algorithm for any problem, which is a key skill for Paper 2. You will apply this knowledge when writing pseudocode for sorting problems, analyzing efficiency of more complex algorithms, and solving mixed searching and sorting problems in your exam preparation.
