Sorting Algorithms Compared: Worked Examples for NSCT
Sorting comparison questions ask you to choose between algorithms, not just run one: which is stable, which is in place, which is fastest on this input, or which algorithm produced these counts. To answer them you need the property table in your head and a feel for the real numbers behind it. Data Structures & Algorithms carries 10% of the NSCT according to HEC's Areas of Competencies, and 2026 candidates averaged just 40.2% accuracy there, per the HEC NSCT report and APP.
This page builds on Sorting Algorithms MCQs, which explains how each algorithm works. Here the algorithms are put side by side on the same inputs. Every count below was produced by running the algorithms in Python.
The method, step by step
- Identify what is being compared: time (best, average, worst), extra space, stability or adaptivity.
- Read the input's shape. Sorted, reversed, nearly sorted, many duplicates and small key range each favour different algorithms.
- Check hard constraints first: "must be stable", "O(1) extra memory", "guaranteed worst case" or "data does not fit in RAM" usually eliminates two options at once.
- If counts are given, match them to formulas: n(n - 1)/2 comparisons on every input means not adaptive; swaps equal to inversions means bubble or insertion sort.
- Trace only when you must, and then only the passes asked for.
The comparison table
Complexities follow CLRS. Adaptive means faster on nearly sorted input; in place means O(1) extra memory beyond the array.
| Algorithm | Best | Average | Worst | Extra space | Stable | Adaptive |
|---|---|---|---|---|---|---|
| Bubble (with flag) | O(n) | O(n^2) | O(n^2) | O(1) | Yes | Yes |
| Selection | O(n^2) | O(n^2) | O(n^2) | O(1) | No | No |
| Insertion | O(n) | O(n^2) | O(n^2) | O(1) | Yes | Yes |
| Merge (array) | O(n log n) | O(n log n) | O(n log n) | O(n) | Yes | No |
| Quick (Lomuto) | O(n log n) | O(n log n) | O(n^2) | O(log n) stack on average | No | No |
| Heap | O(n log n) | O(n log n) | O(n log n) | O(1) | No | No |
| Counting | O(n + k) | O(n + k) | O(n + k) | O(n + k) | Yes | No |
| Radix (LSD, d digits) | O(d(n + b)) | O(d(n + b)) | O(d(n + b)) | O(n + b) | Yes | No |
Here k is the largest key and b is the base (10 for decimal digits). Some texts count quick sort's recursion stack and call it "not in place"; most call it in place.
Same input, three simple sorts
Worked example. Run bubble, selection and insertion sort on [6, 2, 9, 4, 1, 7] and count the work.
| After pass | Bubble (with flag) | Selection | Insertion |
|---|---|---|---|
| 1 | [2, 6, 4, 1, 7, 9] | [1, 2, 9, 4, 6, 7] | [2, 6, 9, 4, 1, 7] |
| 2 | [2, 4, 1, 6, 7, 9] | [1, 2, 9, 4, 6, 7] | [2, 6, 9, 4, 1, 7] |
| 3 | [2, 1, 4, 6, 7, 9] | [1, 2, 4, 9, 6, 7] | [2, 4, 6, 9, 1, 7] |
| 4 | [1, 2, 4, 6, 7, 9] | [1, 2, 4, 6, 9, 7] | [1, 2, 4, 6, 9, 7] |
| 5 | no swaps, stop | [1, 2, 4, 6, 7, 9] | [1, 2, 4, 6, 7, 9] |
| Comparisons | 15 | 15 | 11 |
| Swaps or shifts | 8 swaps | 4 swaps | 8 shifts |
The array has 8 inversions (pairs in the wrong order). Bubble swaps and insertion shifts both equal 8, because each one fixes exactly one inversion. Selection sort fixes several inversions per swap, which is why it makes only 4. In selection pass 2 the minimum, 2, is already in place, so nothing moves.
Exam angle: "fewest writes" points to selection sort; "fewest comparisons on this input" usually points to insertion sort.
Best, average and worst case in real numbers
Worked example. Comparisons made on 8 distinct keys:
| Algorithm | Sorted 1..8 | Reversed 8..1 | Mixed [5, 8, 1, 6, 3, 7, 2, 4] |
|---|---|---|---|
| Bubble (with flag) | 7 | 28 | 27 |
| Selection | 28 | 28 | 28 |
| Insertion | 7 | 28 | 22 |
| Merge (top-down) | 12 | 12 | 17 |
| Quick (Lomuto, last pivot) | 28 | 28 | 13 |
Three patterns to remember. Selection sort's row never changes: 28 = n(n - 1)/2. Insertion and bubble sort collapse to n - 1 = 7 on sorted input. Lomuto quick sort hits 28 on both sorted and reversed input, because the pivot is always the largest or smallest key, while merge sort barely notices the order.
Stability tested on records
A stable sort keeps records with equal keys in their input order.
Worked example. Sort (3, p), (1, q), (3, r), (2, s), (1, t) by the number.
| Algorithm | Output | Equal keys kept in order? |
|---|---|---|
| Insertion, bubble, merge | 1q 1t 2s 3p 3r | Yes |
| Selection | 1q 1t 2s 3r 3p | No, the 3s swapped |
| Quick (Lomuto) | 1q 1t 2s 3r 3p | No |
| Heap | 1q 1t 2s 3r 3p | No |
Selection sort's first pass swaps (3, p) with (1, q), sending p behind r. One long-distance swap is enough to break stability.
Exam angle: stability matters for multi-key sorting. To order by marks with ties in name order, sort by name first, then do a stable sort by marks.
Space and in-place sorting
Heap sort is the classic answer to "O(n log n) worst case and O(1) extra space". Merge sort on an array needs an O(n) buffer, but on a linked list it only relinks nodes. Quick sort uses O(log n) stack space on average and O(n) in its worst case, unless you always recurse into the smaller part first, which caps the stack at O(log n).
Worked example. Build-max-heap on [6, 2, 9, 4, 1, 7] gives [9, 4, 7, 2, 1, 6], entirely inside the original array. No second array is ever allocated.
Non-comparison sorts
Counting and radix sort never compare two keys, so the Omega(n log n) lower bound for comparison sorts does not apply to them.
Worked example (counting sort). Keys [2, 0, 3, 2, 1, 0, 2] with k = 3. Counts are [2, 1, 3, 1]; prefix sums give [2, 3, 6, 7]. Placing from the right, the last 2 goes to index 5, then 4, then 3, keeping the 2s in input order.
Worked example (LSD radix sort). [603, 48, 215, 9, 370, 82, 451]:
| Pass | Digit | Result |
|---|---|---|
| 1 | Units | [370, 451, 82, 603, 215, 48, 9] |
| 2 | Tens | [603, 9, 215, 48, 451, 370, 82] |
| 3 | Hundreds | [9, 48, 82, 215, 370, 451, 603] |
Each pass must be stable, otherwise the order from earlier digits is lost.
Speed tricks
- 28 on everything for n = 8 means selection sort. Learn n(n - 1)/2 for n = 5 to 10: 10, 15, 21, 28, 36, 45.
- Swaps equal inversions for bubble sort; shifts equal inversions for insertion sort.
- "Stable and O(n log n) guaranteed" is merge sort. "In place and O(n log n) guaranteed" is heap sort.
- Small key range means counting sort; fixed-length numbers or strings mean radix sort.
- Larger than RAM means external merge sort.
Common mistakes
- Calling quick sort's worst case O(n log n). It is O(n^2); only its average is O(n log n).
- Saying merge sort is in place. The array version needs O(n) extra memory.
- Assuming every O(n log n) sort is stable. Heap and quick sort are not.
- Forgetting that counting sort becomes slow when k is huge, for example sorting 100 numbers in the range 0 to 10^9.
- Counting selection sort's swaps as inversions. It makes at most n - 1 swaps.
Practice set
These are separate from the 20-question quiz below.
- Bubble sort with a flag runs on [2, 1, 3, 4, 5, 6] and on [3, 4, 5, 6, 1, 2]. How many passes does each need?
- How many swaps does selection sort make on [6, 5, 4, 3, 2, 1]?
- How many comparisons does insertion sort make on [6, 5, 4, 3, 2, 1]?
- How many comparisons does merging [2, 5, 8] with [3, 4, 9, 10] take?
- Counting sort on [1, 3, 1, 0, 2] with k = 3: what is the cumulative count array?
- How many shifts does insertion sort make on [4, 1, 3, 2]?
- You sort small arrays of about 20 nearly sorted items millions of times. Which algorithm fits best?
- Which is the only algorithm in the table above that is not stable, not adaptive, but uses O(1) space and O(n log n) worst case?
- Why does LSD radix sort need a stable sort for each digit pass?
Show answers
- 2 passes and 5 passes. The small values 1 and 2 near the end move only one place per pass.
- 3 swaps. It pairs 6 with 1, 5 with 2 and 4 with 3; the later passes find the minimum already in place.
- 15 comparisons, which is n(n - 1)/2 for n = 6, its worst case.
- 5 comparisons. After 8 is placed the left run is empty, so 9 and 10 are copied without comparing.
- [1, 3, 4, 5]. The counts are [1, 2, 1, 1].
- 4 shifts, because [4, 1, 3, 2] has 4 inversions.
- Insertion sort: tiny constant factors and near-linear time on nearly sorted data.
- Heap sort.
- A later pass must keep the order created by earlier, less significant digits whenever its own digits tie.
Practise: 20 questions
FAQs
Which sorting algorithm is best overall?
None is best everywhere. For general in-memory data, library hybrids win: Timsort (merge plus insertion) in Python and Java objects, introsort (quick plus heap plus insertion) in many C++ libraries. For exams, match the constraint: stability, memory, worst case, input shape or key range.
Is quick sort faster than merge sort?
On random arrays in memory, quick sort is usually faster in practice because it works in place and uses the cache well. Merge sort wins when you need stability, a guaranteed O(n log n), linked lists or external sorting. The table above shows quick sort can also lose badly on sorted input with a poor pivot.
Why is insertion sort used inside fast algorithms?
For about 10 to 50 elements its low overhead beats the recursion and bookkeeping of merge or quick sort, and it is near-linear on nearly sorted runs. Timsort and introsort both hand small pieces to insertion sort for that reason.
Do I need to memorise comparison counts?
No, memorise the formulas instead. n(n - 1)/2 is the worst case for the simple sorts, n - 1 is the best case for insertion and flagged bubble sort, and inversions give swaps or shifts. With those you can rebuild any count in the exam.
Next step
Attempt the 20 comparison MCQs below, then review the mechanics in Sorting Algorithms MCQs and the growth-rate maths in Big-O practice. Return to the DSA MCQ hub or test yourself under time pressure with an NSCT mock test.