P = [1, 2, 3, 5, 4]
Consider two sorting algorithms Bubble Sort (BS) and Insertion Sort (IS).
Let N1 be the total number of comparisons done by BS on the elements of P and N2 be the total number of comparisons done by IS on the elements of P.
Which of the following options is/are correct?
["B","C","D"]
Step-by-Step Solution
Insight: This is a pass-analysis question recognisable because it asks for exact operation counts on a tiny concrete array. The pivot is that Bubble Sort (standard, no early termination) has a deterministic comparison count while Insertion Sort's count is data-dependent and must be traced element by element.
Exam route:
- Compute N1 using the fixed formula: for , standard Bubble Sort does ... wait, that is only for a full sort. Let us recompute carefully: pass 1 does , pass 2 does , pass 3 does , pass 4 does . Total .
- Trace Insertion Sort on :
- key=2: compare with 1 (1 cmp), no shift.
- key=3: compare with 2 (1 cmp), no shift.
- key=5: compare with 3 (1 cmp), no shift.
- key=4: compare with 5 (shift), compare with 3 (stop) → 2 cmp, 1 shift.
- Total , shifts .
- Evaluate options: ; holds; IS does exactly 1 swap; BS in passes 2–4 compares already-sorted adjacent pairs (unnecessary), and IS compares 4 with 5 which are out of order but the comparison that stops at 3 (already in correct relative order with 4) is the boundary check — BS clearly makes unnecessary comparisons, so D holds.
Learning route:
Bubble Sort (standard, no flag) always performs exactly comparisons regardless of input. For , .
Insertion Sort comparisons depend on how far each key travels. Tracing :
- key=2 vs {1}: 1 comparison, 0 shifts.
- key=3 vs {1,2}: 1 comparison, 0 shifts.
- key=5 vs {1,2,3}: 1 comparison, 0 shifts.
- key=4 vs {1,2,3,5}: compares with 5 (shift), then with 3 (stop) → 2 comparisons, 1 shift.
Total , shifts = 1.
Option A claims — wrong because , not 4.
Option B: — true.
Option C: IS performs exactly 1 swap/shift — true.
Option D: BS in passes 2, 3, 4 compares pairs already in correct order (e.g. 1 and 2 in pass 2); IS compares 4 with 5 which are out of order but the stop-comparison with 3 is against an element already in correct relative position — both algorithms make at least one such comparison. True.
Verification: re-running both algorithms on confirms , 1 IS shift, and unnecessary comparisons in both.
Answer: B, C, D.