Sorting Algorithms MCQs for NSCT
Sorting algorithms put elements into order, and NSCT-style questions test both their cost (time, space, swaps) and their behaviour (array state after k passes, stability). This is topic 5 of the Data Structures & Algorithms area, which carries 10% of the NSCT according to HEC's Areas of Competencies. In 2026 candidates averaged 40.2% accuracy in this area, per the HEC NSCT report and APP.
Sorting matters because it unlocks binary search, and heap sort reuses the heap from Non-Linear Data Structures. All complexities below follow CLRS.
| Algorithm | Best | Average | Worst | Extra space | Stable |
|---|---|---|---|---|---|
| Bubble (with flag) | O(n) | O(n^2) | O(n^2) | O(1) | Yes |
| Selection | O(n^2) | O(n^2) | O(n^2) | O(1) | No |
| Insertion | O(n) | O(n^2) | O(n^2) | O(1) | Yes |
| Merge | O(n log n) | O(n log n) | O(n log n) | O(n) | Yes |
| Quick | O(n log n) | O(n log n) | O(n^2) | O(log n) stack on average | No |
| Heap | O(n log n) | O(n log n) | O(n log n) | O(1) | No |
Bubble sort
Bubble sort compares adjacent pairs and swaps them if they are out of order, so the largest remaining value "bubbles" to the end of each pass. On [5, 1, 4, 2, 8], pass 1 gives [1, 4, 2, 5, 8] and pass 2 gives [1, 2, 4, 5, 8]. Total swaps equal the number of inversions (pairs out of order), here 4.
Exam angle: without an early-exit flag it always makes n(n - 1)/2 comparisons; with the flag the best case is O(n). Small values near the end ("turtles") move only one place per pass.
Selection sort
Selection sort finds the minimum of the unsorted part and swaps it to the front. [29, 10, 14, 37, 13] becomes [10, 29, 14, 37, 13] after one pass and [10, 13, 14, 37, 29] after two. It always makes n(n - 1)/2 comparisons but at most n - 1 swaps.
Exam angle: it is not adaptive (sorted input costs the same) and not stable, because its long swaps can jump equal keys.
Insertion sort
Insertion sort takes each element in turn and shifts larger elements in the sorted prefix right to make room. [7, 4, 5, 2, 9, 1] becomes [2, 4, 5, 7, 9, 1] after three insertions. Each shift removes one inversion, so the running time is Theta(n + I) for I inversions.
Exam angle: for nearly sorted data it is effectively linear, which is why Timsort, used by Python's sorted(), combines it with merging.
Merge sort
Merge sort splits the array in half, sorts each half recursively and merges them: T(n) = 2T(n/2) + Theta(n) = Theta(n log n). Merging runs of length m and n takes between min(m, n) and m + n - 1 comparisons. Merging [1, 4, 7, 10] with [2, 3, 9] takes 6.
Exam angle: it is stable if the merge takes from the left run on ties (<=). It needs O(n) extra space for arrays but suits linked lists, where merging just relinks nodes.
Quick sort
Quick sort partitions around a pivot, then sorts each side. In Lomuto partition the last element is the pivot:
def partition(a, lo, hi):
p, i = a[hi], lo - 1
for j in range(lo, hi):
if a[j] <= p:
i += 1
a[i], a[j] = a[j], a[i]
a[i + 1], a[hi] = a[hi], a[i + 1]
return i + 1
On [8, 3, 9, 4, 7, 1, 6] (pivot 6) this gives [3, 4, 1, 6, 7, 9, 8] with the pivot at index 3. Average time is O(n log n), but an already sorted input with a last-element pivot gives n(n - 1)/2 comparisons, the O(n^2) worst case. Random or median-of-three pivots avoid that in practice.
Exam angle: merge sort works in the combine step; quick sort works in the divide (partition) step.
Heap sort
Heap sort builds a max-heap in O(n), then repeatedly swaps the root with the last heap element and sifts down. Bottom-up building turns [4, 10, 3, 5, 1, 2, 8] into [10, 5, 8, 4, 1, 2, 3]; the first extraction gives [8, 5, 3, 4, 1, 2, 10].
Exam angle: it is the answer to "in place and O(n log n) worst case", but it is not stable.
Stability
A stable sort keeps equal keys in their original order. Sorting (3, a), (3, b), (1, c) with selection sort gives (1, c), (3, b), (3, a), so the two 3s are reversed. Stability matters when you sort by a second key after a first, for example by marks after sorting by name.
Exam angle: stable are bubble, insertion, merge, counting and Timsort. Unstable in standard form are selection, quick and heap. Counting sort (O(n + k)) and radix sort are not comparison sorts, so they escape the Omega(n log n) lower bound.
Common traps
- Calling heap sort stable because it is O(n log n) everywhere. Stability and complexity are separate properties.
- Saying quick sort's worst case is O(n log n). It is O(n^2); only its average is O(n log n).
- Saying selection sort's best case is O(n). It makes n(n - 1)/2 comparisons on any input.
- Forgetting that a build-max-heap result is a heap, not a sorted array.
- Counting bubble sort swaps by passes. Swaps equal inversions, whatever the pass count.
- Assuming merge sort is in place. Array merge sort needs an O(n) buffer.
Worked examples
1. Understanding. Which of insertion, heap, quick and selection sort is stable? Answer: insertion sort. It shifts elements one step at a time and stops at the first element that is not larger, so equal keys never pass each other.
2. Application. Bubble sort runs on [9, 4, 7, 1, 3]. What is the array after two passes? Answer: [4, 1, 3, 7, 9]. Pass 1 carries 9 to the end: [4, 7, 1, 3, 9]. Pass 2 carries 7 to its place: [4, 1, 3, 7, 9].
3. Analysis. Selection sort makes 10 comparisons on [1, 2, 3, 4, 5] and also on [5, 4, 3, 2, 1]. What does this tell you? Answer: selection sort is not adaptive. It always scans the whole unsorted part, giving n(n - 1)/2 = 10 comparisons for n = 5. Only the swaps differ (0 against 2). Insertion sort would need just 4 comparisons on the sorted input.
Practise
The quiz below has 100 Sorting Algorithms questions, filterable by tier and difficulty.
Practise: 100 questions
FAQs
Which sorting algorithm is the fastest?
No single algorithm wins everywhere. Quick sort is usually fastest on random arrays in practice, merge sort gives a guaranteed O(n log n) and stability, heap sort gives O(n log n) in place, and insertion sort wins on small or nearly sorted data. Library sorts such as Timsort and introsort combine several of these.
Why is quick sort used if its worst case is O(n^2)?
Its average is O(n log n) with small constants and good cache behaviour, and it sorts in place. A random or median-of-three pivot makes the O(n^2) case extremely unlikely, and introsort switches to heap sort if recursion gets too deep, which removes the worst case altogether.
What does it mean for a sort to be stable?
A stable sort keeps records with equal keys in the order they had before sorting. For example, if Ali and Omar both score 85 and Ali comes first in the input, a stable sort by marks keeps Ali before Omar. Merge, insertion and bubble sort are stable.
Can sorting be faster than O(n log n)?
Not with comparisons alone: any comparison sort needs Omega(n log n) comparisons in the worst case. Counting sort, radix sort and bucket sort beat this by using the key values directly, for example O(n + k) for counting sort with keys from 0 to k.
Next step
Continue with Hashing MCQs, compare every algorithm side by side in sorting algorithms compared, go back to the DSA MCQ hub, or take an NSCT mock test.