GATE DA 2026 · Bubble Sort

MSQ · 2 marks · Problem PROG-SORT-0016

Consider the problem of sorting the given array in ascending order: \(P=[1,2,3,5,4]\) Consider two sorting algorithms Bubble Sort \(\text{(BS)}\) and Insertion Sort \(\text{(IS)}\). Let \(\text{N1}\) be the total number of comparisons done by \(\text{BS}\) on the elements of \(\text{P}\) and \(\text{N2}\) be the total number of comparisons done by \(\text{IS}\) on the elements of \(\text{P}\). Which of the following options is/are correct?
  1. A
    \({N} 1=10, {~N} 2=4\)
  2. B
    \({N} 1>{N} 2\)
  3. C
    \(\text{IS}\) on \(\text{P}\) will perform only one swap
  4. D
    Both \(\text{BS}\) and \(\text{IS}\) on \(\text{P}\) will make at least one unnecessary comparison (i.e., comparing elements that are already in correct order)

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

Show the worked solution\(P=[1,2,3,5,4]\), needing just one adjacent swap (5,4) to become sorted.Bubble Sort (standard version, checking all adjacent pairs every pass until a pass makes no swaps): Pass 1 makes \(4\) comparisons (\((1,2),(2,3),(3,5),(5,4)\), with exactly one swap on the last pair) sorting the array; subsequent passes (2, 3, 4) still each scan their shrinking sublist (\(3,2,1\) comparisons respectively) to confirm no more swaps are needed. Total comparisons \(N1=4+3+2+1=10\), with just \(1\) swap.Insertion Sort: shifting element \(2,3,5\) each needs only \(1\) comparison against its predecessor (no shift needed, since each is already larger); inserting \(4\) needs \(2\) comparisons (against \(5\), which shifts right, then against \(3\), where it stops) and exactly \(1\) shift. Total comparisons \(N2=1+1+1+2=5\), with just \(1\) shift/swap.So \(N1=10>N2=5\) (N1>N2 is true); Insertion Sort does perform only one swap (true); and since the array is nearly sorted already (only one inversion), both algorithms still perform several comparisons beyond the bare minimum needed to detect/fix that single inversion — so both make at least one "unnecessary" comparison/swap is also true.Answer: N1>N2; IS performs only one swap; and both BS and IS make at least one unnecessary comparison/swap.Click Here for Detail SolutionGateOverflow discussion: https://gateoverflow.in/523176/gate-da-2026-question-39

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