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 [3, 37, 10, 52, 30, 23, 22, 38]. What does the array look like after pass 2, and how many swaps have been made so far?

  1. A. [3, 10, 23, 22, 30, 37, 38, 52]; 8 swaps
  2. B. [3, 10, 37, 30, 23, 22, 38, 52]; 8 swaps
  3. C. [3, 10, 37, 52, 30, 23, 22, 38]; 8 swaps
  4. D. [3, 10, 30, 23, 22, 37, 38, 52]; 8 swaps

Correct answer: D. [3, 10, 30, 23, 22, 37, 38, 52]; 8 swaps

Each pass bubbles the largest remaining element to the end. After 2 passes the last 2 positions are final: [3, 10, 30, 23, 22, 37, 38, 52], using 8 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