Question 49

Computer Awareness Searching Hard

The following integers are needed to be stored in ascending order using bubble sort.

5, 8, 22, 18, 1
Following are the results of various passes during the sorting process. Which among them is final pass

(A) \( 5,1,8,18,22 \)
(B) \( 1,5,8,18,22 \)
(C) \( 5,8,18,1,22 \)
(D) \( 5,8,1,18,22 \)
View Dynamic Solution & Explanation
Correct Solution: Option B

Step-by-step Solution:

Let's apply the Bubble Sort algorithm step by step on the given list:
Given array: \[ 5, 8, 22, 18, 1 \] Pass 1
- Compare 5 and 8 → No swap
- Compare 8 and 22 → No swap
- Compare 22 and 18 → Swap → 5, 8, 18, 22, 1
- Compare 22 and 1 → Swap → 5, 8, 18, 1, 22

Pass 2:
- Compare 5 and 8 → No swap
- Compare 8 and 18 → No swap
- Compare 18 and 1 → Swap → 5, 8, 1, 18, 22
- 22 is in its final position

Pass 3:
- Compare 5 and 8 → No swap
- Compare 8 and 1 → Swap → 5, 1, 8, 18, 22

Pass 4:
- Compare 5 and 1 → Swap → 1, 5, 8, 18, 22

Final Sorted Order: \[ 1, 5, 8, 18, 22 \]
Correct Answer:
Option B: \( 1, 5, 8, 18, 22 \)