Which one of the sorting algorithm given in options will perform sorting of the array 8 9...
Qualcomm technical mcq question, verified with a worked answer. Free to practise - no sign-up.
Which one of the sorting algorithm given in options will perform sorting of the array 8 9 10 2 4 in ascending order?
If the result of 1st pass is = 8 9 2 4 10
If the result of 2nd pass is= 8 2 4 9 10 and so on.
Show answer & explanation
Bubble sort compares adjacent elements and swaps them if they are in the wrong order, moving the largest unsorted element to its correct position at the end of each pass. The given pass results match bubble sort's behavior: after pass 1, the largest element (10) moves to the end; after pass 2, the second-largest element (9) is in place. Heap sort, merge sort, and quick sort do not produce these specific intermediate results.
Step-by-step Derivation:
Tracing bubble sort on [8, 9, 10, 2, 4]:
Pass 1 (compare adjacent pairs, swap if needed):
- Compare 8,9: no swap → [8, 9, 10, 2, 4]
- Compare 9,10: no swap → [8, 9, 10, 2, 4]
- Compare 10,2: swap → [8, 9, 2, 10, 4]
- Compare 10,4: swap → [8, 9, 2, 4, 10] ✓ (matches given result)
Pass 2 (largest element 10 is now in place, repeat for remaining):
- Compare 8,9: no swap → [8, 9, 2, 4, 10]
- Compare 9,2: swap → [8, 2, 9, 4, 10]
- Compare 9,4: swap → [8, 2, 4, 9, 10] ✓ (matches given result)
This incremental, adjacent-element swapping pattern is characteristic of bubble sort, not the divide-and-conquer or heap-based approaches of the other algorithms.