GATE Computer ScienceVery hardGATE CS (PYQ pattern)

Bubble sort with early exit (a pass that makes no swap ends the sort, and every pass scans the whole array from index 0 to n-2) is run on [7, 1, 3, 2, 9, 5, 8, 6, 4, 0]. How many passes (including the final swap-free one) and how many swaps are made?

  1. A. 10 passes, 24 swaps
  2. B. 9 passes, 24 swaps
  3. C. 10 passes, 27 swaps
  4. D. 11 passes, 21 swaps

Correct answer: A. 10 passes, 24 swaps

The number of swaps equals the inversion count (24). A pass moves each element at most one place to the left, and the element 0 must travel 9 places, which forces 9 passes with swaps plus one clean pass.

More Programming and Data Structures (GATE) questions

Exams that ask this topic