GATE DA 2026 | Question 58
Programming, Data Structures and Algorithms › Python Programming · NAT · 2 marks · Answer: 8
- Question 58Programming, Data Structures and Algorithmsnat2 marksConsider the given Python program.
The output of the program is __________ . (Answer in integer)pythondef 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)Answer: 8
Solution
funmakes one bubble-sort pass over the list and returns the number of swaps in that pass. The loop runs 5 passes, socountis the total number of swaps bubble sort makes. Each bubble-sort swap removes exactly one inversion, so the total equals the number of inversions in[5, 3, 4, 1, 2]: - 5 is before and larger than 3, 4, 1, 2: 4 inversions. - 3 is before and larger than 1, 2: 2. - 4 is before and larger than 1, 2: 2. - 1 before 2: 0. Total . Pass by pass: 4, then 2, then 2, then 0, then 0 swaps. Answer: 8.
More GATE PYQ on Python Programming
All GATE DA Programming, Data Structures and Algorithms PYQ →Attempt GATE DA 2026 in real GATE exam mode
Full paper with a timer, the GATE interface and a topic-wise analysis — free.
Create free account →