Sorting Algorithms Compared: Worked Examples for NSCT

By the NSCT Exam Editorial Team · Updated 2026-11-24 · Facts checked against official sources on 2026-11-24

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

  1. Identify what is being compared: time (best, average, worst), extra space, stability or adaptivity.
  2. Read the input's shape. Sorted, reversed, nearly sorted, many duplicates and small key range each favour different algorithms.
  3. 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.
  4. 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.
  5. 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

Common mistakes

Practice set

These are separate from the 20-question quiz below.

  1. 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?
  2. How many swaps does selection sort make on [6, 5, 4, 3, 2, 1]?
  3. How many comparisons does insertion sort make on [6, 5, 4, 3, 2, 1]?
  4. How many comparisons does merging [2, 5, 8] with [3, 4, 9, 10] take?
  5. Counting sort on [1, 3, 1, 0, 2] with k = 3: what is the cumulative count array?
  6. How many shifts does insertion sort make on [4, 1, 3, 2]?
  7. You sort small arrays of about 20 nearly sorted items millions of times. Which algorithm fits best?
  8. 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?
  9. Why does LSD radix sort need a stable sort for each digit pass?
Show answers
  1. 2 passes and 5 passes. The small values 1 and 2 near the end move only one place per pass.
  2. 3 swaps. It pairs 6 with 1, 5 with 2 and 4 with 3; the later passes find the minimum already in place.
  3. 15 comparisons, which is n(n - 1)/2 for n = 6, its worst case.
  4. 5 comparisons. After 8 is placed the left run is empty, so 9 and 10 are copied without comparing.
  5. [1, 3, 4, 5]. The counts are [1, 2, 1, 1].
  6. 4 shifts, because [4, 1, 3, 2] has 4 inversions.
  7. Insertion sort: tiny constant factors and near-linear time on nearly sorted data.
  8. Heap sort.
  9. A later pass must keep the order created by earlier, less significant digits whenever its own digits tie.

Practise: 20 questions

Interactive quiz loading. The first 10 questions are shown below.

  1. Which sorting algorithm is stable AND guarantees O(n log n) time in the worst case?

    1. Merge sort
    2. Heap sort
    3. Quick sort
    4. Insertion sort
    Show answer

    Answer: A. Merge sort

    Merge sort always splits in half, so it is Theta(n log n) on every input, and it is stable when the merge takes from the left run on ties. Heap sort is O(n log n) but unstable, quick sort is O(n^2) in the worst case, and insertion sort is O(n^2).

  2. A sorting algorithm is called adaptive when it runs faster on input that is already nearly sorted. Which of these is adaptive in its standard form?

    1. Selection sort
    2. Insertion sort
    3. Heap sort
    4. Top-down merge sort
    Show answer

    Answer: B. Insertion sort

    Insertion sort does Theta(n + I) work for I inversions, so nearly sorted input runs in close to linear time. Selection sort always makes n(n - 1)/2 comparisons, and heap sort and plain merge sort stay Theta(n log n) whatever the order.

  3. Which row of a sorting comparison table is fully correct (worst-case time, extra space, stability)?

    1. Merge sort: O(n log n), O(1), stable
    2. Quick sort: O(n log n), O(log n), not stable
    3. Heap sort: O(n log n), O(1), not stable
    4. Insertion sort: O(n), O(1), stable
    Show answer

    Answer: C. Heap sort: O(n log n), O(1), not stable

    Heap sort builds the heap inside the array and needs only O(1) extra space, runs in O(n log n) worst case, and is not stable. Array merge sort needs O(n) extra space, quick sort's worst case is O(n^2), and insertion sort's worst case is O(n^2); O(n) is its best case.

  4. Counting sort runs in O(n + k) time for n keys in the range 0 to k. When is it asymptotically better than any comparison sort?

    1. Always, because it never compares keys
    2. Only when the input is already sorted
    3. When k is much larger than n, for example k = n^2
    4. When k = O(n), so the running time is O(n)
    Show answer

    Answer: D. When k = O(n), so the running time is O(n)

    Comparison sorts need Omega(n log n) comparisons. Counting sort costs O(n + k), so it wins when k grows no faster than n. If k = n^2, the O(n + k) cost becomes O(n^2), worse than merge sort.

  5. What does it mean to say a sorting algorithm sorts "in place"?

    1. It never moves an element more than once
    2. It keeps equal keys in their original order
    3. It needs only a constant (or very small) amount of memory beyond the input array
    4. It finishes in linear time on sorted input
    Show answer

    Answer: C. It needs only a constant (or very small) amount of memory beyond the input array

    In place means the sort rearranges the input array using O(1) extra memory (some texts also allow the O(log n) recursion stack of quick sort). Keeping equal keys in order is stability, and linear time on sorted input is adaptivity.

  6. Many C++ standard library implementations of std::sort use introsort. Which description fits introsort?

    1. Quick sort that switches to heap sort when recursion gets too deep, with insertion sort for small ranges
    2. Merge sort that switches to insertion sort for small runs
    3. Heap sort that switches to counting sort for small keys
    4. Radix sort that falls back to bubble sort
    Show answer

    Answer: A. Quick sort that switches to heap sort when recursion gets too deep, with insertion sort for small ranges

    Introsort starts as quick sort for speed, switches to heap sort when the recursion depth passes about 2 log n (removing the O(n^2) worst case), and finishes small partitions with insertion sort. Merge plus insertion describes Timsort, used by Python.

  7. Bubble sort (ascending, adjacent swaps left to right) runs on [8, 3, 5, 1, 9, 2]. What is the array after two passes?

    1. [3, 5, 1, 8, 2, 9]
    2. [1, 2, 5, 8, 9, 3]
    3. [3, 5, 1, 2, 8, 9]
    4. [3, 1, 5, 2, 8, 9]
    Show answer

    Answer: D. [3, 1, 5, 2, 8, 9]

    Pass 1 carries 9 to the end: [3, 5, 1, 8, 2, 9]. Pass 2 carries 8 to the second-last place: 3 and 5 stay, then 5 swaps with 1, 5 swaps with 2, and 8 is already before 9. Result: [3, 1, 5, 2, 8, 9].

  8. Selection sort (find the minimum of the unsorted part, swap it to the front, skip the swap when the minimum is already in place) sorts [8, 3, 5, 1, 9, 2]. How many comparisons and how many swaps does it make?

    1. 15 comparisons and 9 swaps
    2. 15 comparisons and 5 swaps
    3. 12 comparisons and 5 swaps
    4. 15 comparisons and 6 swaps
    Show answer

    Answer: B. 15 comparisons and 5 swaps

    Selection sort always makes n(n - 1)/2 = 15 comparisons for n = 6. Each of the 5 passes finds a minimum that is not already in place, so all 5 swaps happen. Nine is the inversion count, which is what bubble and insertion sort move.

  9. How many element shifts does insertion sort make to sort [8, 3, 5, 1, 9, 2]?

    1. 5
    2. 9
    3. 15
    4. 12
    Show answer

    Answer: B. 9

    Each shift moves one larger element one place right and removes exactly one inversion. The array has 9 inversions (pairs out of order), so insertion sort makes 9 shifts. It makes 12 comparisons in total.

  10. Top-down merge sort (split at the middle, merge with <=) and quick sort with Lomuto partition (last element as pivot) both sort [8, 3, 5, 1, 9, 2, 7, 4]. How many key comparisons does each make?

    1. Merge sort 24, quick sort 28
    2. Merge sort 13, quick sort 17
    3. Merge sort 17, quick sort 28
    4. Merge sort 17, quick sort 13
    Show answer

    Answer: D. Merge sort 17, quick sort 13

    Merge sort makes 4 comparisons at the pair level, 3 + 3 = 6 to build its two halves of four, then 7 for the final merge: 17. Quick sort partitions on pivot 4 (7 comparisons) giving [3, 1, 2, 4, 9, 5, 7, 8]; the parts [3, 1, 2] and [9, 5, 7, 8] cost 2 + 3, and the leftover [5, 7] costs 1: 13. Good pivots make quick sort cheaper here.

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.