Data Structures & AlgorithmsHardGATE CS · DSA

Bubble sort (ascending; each pass compares adjacent pairs left to right and swaps if out of order) is applied to [49, 52, 21, 37, 35, 19, 9, 44]. What does the array look like after pass 2, and how many swaps have been made so far?

  1. A. [21, 35, 19, 9, 37, 44, 49, 52]; 12 swaps
  2. B. [21, 37, 35, 19, 9, 44, 49, 52]; 12 swaps
  3. C. [49, 21, 37, 35, 19, 9, 44, 52]; 12 swaps
  4. D. [9, 19, 21, 37, 35, 52, 49, 44]; 12 swaps

Correct answer: B. [21, 37, 35, 19, 9, 44, 49, 52]; 12 swaps

Each pass bubbles the largest remaining element to the end. After 2 passes the last 2 positions are final: [21, 37, 35, 19, 9, 44, 49, 52], using 12 swaps in total (the swap count equals the inversions removed so far).

More Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questions

Exams that ask this topic