GATE DA 2026 Q58: Total swaps made by repeated recursive bubble passes
- Programming, Data Structures and Algorithms
- 2 marks
- NAT
- No negative marking
Question
Consider the given Python program.
def fun(L, i=0):
if i >= len(L)-1:
return 0
if L[i] > L[i+1]:
L[i+1], L[i] = L[i], L[i+1]
return 1+fun(L, i+1)
else:
return fun(L, i+1)
data = [5, 3, 4, 1, 2]
count = 0
for _ in range(len(data)):
count += fun(data)
print(count)The output of the program is __________. (Answer in integer)
Try it: numerical answer: type the value
Numerical answer. No negative marking.
Show answer and explanation
Answer: 8 (official GATE 2026 final answer key)
Concept used: Each call is one bubble-sort pass; bubble sort swaps exactly once per inversion.
First step: Count inversions of [5, 3, 4, 1, 2]: 5 is larger than 4 later elements, 3 than 2, 4 than 2.
Full step-by-step solution
Detailed working, common mistakes and linked mock tests will be available with a free GATEiit account. Log-in is coming soon.
Share this question
Source: official GATE 2026 DA question paper and final answer key, organised by IIT Guwahati. Question paper · Answer key
More Programming, Data Structures and Algorithms PYQs
- 2026 · Q15Expected-time recurrence for randomised-input quicksort
- 2026 · Q16Python mutable default argument output
- 2026 · Q25Binary tree from preorder and inorder traversals
- 2026 · Q31Worst-case comparisons in binary search of 1000 elements
- 2026 · Q39Number of calls made by a Fibonacci-like recursion
- 2026 · Q40Reachability in a graph and its reverse