Suppose that insertion sort is applied to the array and it takes exactly two swaps to sort the array. Select all possible values of .
- (A)
10
- (B)
12
- (C)
14
- (D)
16
Suppose that insertion sort is applied to the array and it takes exactly two swaps to sort the array. Select all possible values of .
10
12
14
16
ANSWER
The possible values of are and , options (A) and (C).
Official answer: A, CSource: Official answer key, GATE 2025 DA (IIT Roorkee)
Insertion sort moves each element left by adjacent swaps until it reaches its place. Each adjacent swap removes exactly one inversion, and the sort ends with none. So the number of swaps equals the number of inversions, meaning pairs with . We need exactly inversions.
The pair is already an inversion, so it contributes . The entries are increasing and smaller than and , so no other pair among the fixed entries is an inversion. We therefore need exactly inversion that involves .
Inversions involving = (number of earlier elements in greater than ) + (number of later elements in smaller than ).
: only is greater among the earlier elements, giving . Neither nor is smaller than , giving . Total with is , so the array has inversions and takes swaps. Valid.
: no earlier element exceeds , and neither nor is smaller than . That gives inversions with , so the array has inversion and takes swap. Not valid.
: no earlier element exceeds . Among the later elements, counts and does not, giving . The array has inversions and takes swaps. Valid.
: no earlier element exceeds , but both and count, giving . The array has inversions and takes swaps. Not valid.
Hence the possible values are and , which are options (A) and (C).
KEY IDEA
With adjacent swaps, insertion sort performs exactly one swap per inversion. Count the inversions in the array and set the total equal to .
WATCH OUT FOR
Counting only the inversion that creates and forgetting that is already an inversion. That leads to wanting inversions with , which gives instead of and .
From the GATE 2025 DA paper. Every question of this paper · Download the full paper · All 18 Sorting & hashing questions, with notes