GATE DA 2025 · Insertion Sort

MSQ · 1 mark · Problem PROG-SORT-0014

Suppose that insertion sort is applied to the array \([1, 3, 5, 7, 9, 11, x, 15, 13]\) and it takes exactly two swaps to sort the array. Select all possible values of \(x\).
  1. A
    10
  2. B
    12
  3. C
    14
  4. D
    16

Solve it on GATE DA practice →More Searching & Sorting Algorithms problems

Show the worked solutionSimulate insertion sort on \([1,3,5,7,9,11,x,15,13]\), tracking swaps for each candidate \(x\):\(x=10\): insert \(10\) into \([1,3,5,7,9,11]\): \(1\) swap (past \(11\)). Insert \(15\): no swap. Insert \(13\): \(1\) swap (past \(15\)). Total: \(2\) swaps. Matches.\(x=12\): insert \(12\): no swap. Insert \(15\): no swap. Insert \(13\): \(1\) swap (past \(15\), stops at \(12\)). Total: \(1\) swap only. Doesn't match.\(x=14\): insert \(14\): no swap. Insert \(15\): no swap. Insert \(13\): \(2\) swaps (past \(15\), then past \(14\)). Total: \(2\) swaps. Matches.\(x=16\): insert \(16\): no swap. Insert \(15\): \(1\) swap (past \(16\)). Insert \(13\): \(2\) swaps (past \(16\), then past \(15\)). Total: \(3\) swaps. Doesn't match.Answer: x=10 and x=14Click Here for Detail SolutionGateOverflow discussion: https://gateoverflow.in/461003/gate-da-2025-question-19

GATE DA practice: past exam problems by chapter, with worked solutions · gatedaprep.com