An array holds 7, 2, 9, 4. A bubble sort puts it into ascending order. What does the array hold after the first pass, and how many comparisons were made?
- Compare 7 and 2: wrong order, so swap. The array is now 2, 7, 9, 4.
- Compare 7 and 9: correct order, so no swap. The array is still 2, 7, 9, 4.
- Compare 9 and 4: wrong order, so swap. The array is now 2, 7, 4, 9.
- There are 4 items, so the pass made 4 − 1 = 3 comparisons.
Answer: After the first pass the array holds 2, 7, 4, 9, and 3 comparisons were made.