Sorting Algorithms MCQs for NSCT

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

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

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

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

  1. Which of these sorting algorithms is stable in its standard textbook form?

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

    Answer: B. Merge sort

    A stable sort keeps records with equal keys in their original relative order. Merge sort is stable as long as the merge takes from the left run when keys are equal (<=). Standard quick sort, heap sort and selection sort all make long-distance swaps that can reorder equal keys.

  2. Which algorithm sorts in place (O(1) extra space) and guarantees O(n log n) time even in the worst case?

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

    Answer: A. Heap sort

    Heap sort builds a max-heap inside the array and repeatedly swaps the root to the end, using only a constant number of extra variables, and every case is O(n log n). Merge sort needs O(n) extra space for arrays, quick sort is O(n^2) in the worst case, and insertion sort is O(n^2).

  3. What is the best-case time complexity of insertion sort, and on which input does it occur?

    1. O(1), on an empty array
    2. O(n log n), on a random array
    3. O(n), on a reverse-sorted array
    4. O(n), on an already sorted array
    Show answer

    Answer: D. O(n), on an already sorted array

    On sorted input each new element is compared once with its left neighbour and stays put, giving n - 1 comparisons and no shifts. Reverse-sorted input is the worst case, with n(n - 1)/2 comparisons.

  4. Quick sort always picks the last element as pivot. On which input does it hit its O(n^2) worst case?

    1. An array of random distinct values
    2. An array that is already sorted
    3. An array whose median is at the end
    4. An array of length 2
    Show answer

    Answer: B. An array that is already sorted

    With a sorted array the last element is the maximum, so every partition splits into n - 1 and 0 elements. That gives (n - 1) + (n - 2) + ... + 1 = n(n - 1)/2 comparisons, which is O(n^2).

  5. How much auxiliary space does standard top-down merge sort on an array use?

    1. O(1)
    2. O(log n)
    3. O(n^2)
    4. O(n)
    Show answer

    Answer: D. O(n)

    The merge step copies the two runs into temporary storage of total size n. The recursion adds O(log n) stack, but the O(n) buffer dominates. In-place merging exists but is complex and rarely taught.

  6. Why is selection sort sometimes preferred when writing to memory is very expensive, such as on flash storage?

    1. It makes O(n log n) comparisons
    2. It is stable
    3. Its best case is O(n)
    4. It makes at most n - 1 swaps
    Show answer

    Answer: D. It makes at most n - 1 swaps

    Selection sort finds the minimum of the unsorted part and swaps it into place once per pass, so it does at most n - 1 swaps, the fewest writes of the simple sorts. Its comparisons are always n(n - 1)/2 and it is not stable.

  7. What is the lower bound on worst-case comparisons for any comparison-based sorting algorithm?

    1. Omega(n log n)
    2. Omega(n)
    3. Omega(log n)
    4. Omega(n^2)
    Show answer

    Answer: A. Omega(n log n)

    A comparison sort is a decision tree that must distinguish all n! orderings, so its height is at least log2(n!), which is Theta(n log n) by Stirling's approximation. Merge sort and heap sort meet this bound.

  8. Which of these sorting algorithms does not sort by comparing elements with each other?

    1. Heap sort
    2. Shell sort
    3. Counting sort
    4. Insertion sort
    Show answer

    Answer: C. Counting sort

    Counting sort tallies how many times each key value occurs and uses the running totals to place elements, so it never compares two elements. That is how it beats the Omega(n log n) comparison bound when the key range k is small.

  9. If an array of n elements contains I inversions, what is the running time of insertion sort?

    1. Theta(n log n)
    2. Theta(n + I)
    3. Theta(I log n)
    4. Theta(n * I)
    Show answer

    Answer: B. Theta(n + I)

    Each shift in insertion sort removes exactly one inversion, so there are I shifts, plus about n comparisons that end each inner loop. The total is Theta(n + I): linear for nearly sorted input and quadratic when I is about n^2/2.

  10. Bubble sort with a swapped flag stops when a full pass makes no swaps. What is its best-case time complexity?

    1. O(n)
    2. O(1)
    3. O(n log n)
    4. O(n^2)
    Show answer

    Answer: A. O(n)

    On sorted input the first pass makes n - 1 comparisons and no swaps, so the flag stops the sort after one pass. Without the flag, bubble sort always makes n(n - 1)/2 comparisons, even on sorted input.

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.