Skip to content
GATEiit

GATE DA 2026 Q58: Total swaps made by repeated recursive bubble passes

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

WhatsAppDownload image

Source: official GATE 2026 DA question paper and final answer key, organised by IIT Guwahati. Question paper · Answer key