Data Structures & AlgorithmsHardProduct placement · Amazon/Microsoft

Bubble sort (ascending; each pass compares adjacent pairs left to right and swaps if out of order) is applied to [5, 42, 29, 4, 41, 8, 53, 7]. What does the array look like after pass 2, and how many swaps have been made so far?

  1. A. [5, 4, 29, 8, 41, 7, 42, 53]; 8 swaps
  2. B. [5, 29, 4, 41, 8, 42, 7, 53]; 8 swaps
  3. C. [4, 5, 8, 29, 7, 41, 42, 53]; 8 swaps
  4. D. [4, 5, 29, 42, 41, 8, 53, 7]; 8 swaps

Correct answer: A. [5, 4, 29, 8, 41, 7, 42, 53]; 8 swaps

Each pass bubbles the largest remaining element to the end. After 2 passes the last 2 positions are final: [5, 4, 29, 8, 41, 7, 42, 53], 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