Data Structures & AlgorithmsModerateTCS NQT · Accenture pattern

Bubble sort (each pass compares adjacent pairs left to right and swaps if out of order) runs on [6, 2, 9, 1, 5]. What is the array after the first TWO passes?

  1. A. [2, 6, 1, 5, 9]
  2. B. [1, 2, 5, 6, 9]
  3. C. [2, 1, 6, 5, 9]
  4. D. [2, 1, 5, 6, 9]

Correct answer: D. [2, 1, 5, 6, 9]

Pass 1 carries 9 to the end: [2, 6, 1, 5, 9]. Pass 2 carries 6 to the second-last slot: 2 < 6 (no swap), 6 > 1 swap, 6 > 5 swap → [2, 1, 5, 6, 9]. [2, 6, 1, 5, 9] is the state after only one pass; the fully sorted array needs a third pass.

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

Exams that ask this topic