Question:

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) 
 

Show Hint

Recognizing that the inner recursive function performs one pass of Bubble Sort is the key to understanding this code. The outer loop essentially runs the Bubble Sort algorithm for a fixed number of passes (equal to the length of the list), and the `count` variable accumulates the total number of swaps across all these passes.
Updated On: Feb 23, 2026
Hide Solution
collegedunia
Verified By Collegedunia

Correct Answer: 8

Solution and Explanation

Step 1: Understanding the Question:
We are given a Python script with a recursive function `fun` that is called repeatedly in a loop. We need to trace the execution and determine the final value of the `count` variable.
Step 2: Analyzing the `fun` function:
The `fun(L, i)` function iterates through the list `L` from index `i` to the end. It compares adjacent elements `L[i]` and `L[i+1]`. If they are out of order (`L[i]> L[i+1]`), it swaps them and returns 1 plus the result of the recursive call on the rest of the list. Otherwise, it just continues the recursion.
Essentially, `fun(L)` performs a single pass of the Bubble Sort algorithm and returns the number of swaps it made during that pass. The list `L` is modified in-place.
Step 3: Tracing the Loop Execution:
The main part of the code calls `fun(data)` five times (since `len(data)` is 5) and accumulates the returned number of swaps into `count`.
Initial `data` = `[5, 3, 4, 1, 2]`
- Pass 1 (Loop 1): `fun([5, 3, 4, 1, 2])` is called.
- 5> 3 $\rightarrow{}$ swap. List: [3, 5, 4, 1, 2]. Swaps: 1.
- 5> 4 $\rightarrow{}$ swap. List: [3, 4, 5, 1, 2]. Swaps: 2.
- 5> 1 $\rightarrow{}$ swap. List: [3, 4, 1, 5, 2]. Swaps: 3.
- 5> 2 $\rightarrow{}$ swap. List: [3, 4, 1, 2, 5]. Swaps: 4.
`fun` returns 4. `count` becomes 0 + 4 = 4. `data` is now `[3, 4, 1, 2, 5]`. - Pass 2 (Loop 2): `fun([3, 4, 1, 2, 5])` is called. - 3< 4 $\rightarrow{}$ no swap. - 4> 1 $\rightarrow{}$ swap. List: [3, 1, 4, 2, 5]. Swaps: 1. - 4> 2 $\rightarrow{}$ swap. List: [3, 1, 2, 4, 5]. Swaps: 2. - 4< 5 $\rightarrow{}$ no swap. `fun` returns 2. `count` becomes 4 + 2 = 6. `data` is now `[3, 1, 2, 4, 5]`. - Pass 3 (Loop 3): `fun([3, 1, 2, 4, 5])` is called. - 3> 1 $\rightarrow{}$ swap. List: [1, 3, 2, 4, 5]. Swaps: 1. - 3> 2 $\rightarrow{}$ swap. List: [1, 2, 3, 4, 5]. Swaps: 2. - 3< 4 $\rightarrow{}$ no swap. - 4< 5 $\rightarrow{}$ no swap. `fun` returns 2. `count` becomes 6 + 2 = 8. `data` is now `[1, 2, 3, 4, 5]`. - Pass 4 (Loop 4): `fun([1, 2, 3, 4, 5])` is called. The list is now sorted. No swaps will occur. `fun` returns 0. `count` becomes 8 + 0 = 8. `data` remains `[1, 2, 3, 4, 5]`. - Pass 5 (Loop 5): `fun([1, 2, 3, 4, 5])` is called. The list is still sorted. No swaps will occur. `fun` returns 0. `count` becomes 8 + 0 = 8. `data` remains `[1, 2, 3, 4, 5]`. Step 4: Final Answer:
The final value of `count` that will be printed is 8.
Was this answer helpful?
0
0

Questions Asked in GATE DA exam

View More Questions