Question 38 What is the number of swaps required in the first pass of bubble sort if the...
Qualcomm technical mcq question, verified with a worked answer. Free to practise - no sign-up.
Question 38
What is the number of swaps required in the first pass of bubble sort if the elements given below are sorted in descending order?
22 71 10 93 61 43 9
Show answer & explanation
Answer: A. 4
In the first pass of bubble sort (descending order), adjacent elements are compared and swapped if the first is smaller than the second. Tracing through: (22,71)→swap, (71,10)→swap, (71,93)→swap, (93,61)→no swap, (61,43)→no swap, (43,9)→no swap. This yields 4 swaps, leaving 93 at the end.
Step-by-step Derivation:
Initial array: [22, 71, 10, 93, 61, 43, 9]
First pass (descending order - larger elements move right):
- Compare 22 and 71: 22 < 71 → SWAP → [71, 22, 10, 93, 61, 43, 9] (Swap 1)
- Compare 22 and 10: 22 > 10 → no swap → [71, 22, 10, 93, 61, 43, 9]
- Compare 10 and 93: 10 < 93 → SWAP → [71, 22, 93, 10, 61, 43, 9] (Swap 2)
- Compare 10 and 61: 10 < 61 → SWAP → [71, 22, 93, 61, 10, 43, 9] (Swap 3)
- Compare 10 and 43: 10 < 43 → SWAP → [71, 22, 93, 61, 43, 10, 9] (Swap 4)
- Compare 10 and 9: 10 > 9 → no swap → [71, 22, 93, 61, 43, 10, 9]
Total swaps in first pass: 4