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)
Correct Answer :
Solution :
The correct option/answer is 8.
Let's analyze the function fun(L, i=0) and the loop step-by-step to understand how it processes the list data = [5, 3, 4, 1, 2] and counts the operations.
The function fun(L, i=0) performs a single pass of the Bubble Sort algorithm from index i to the end of the list L. Specifically:
- If the current index i is at or beyond the last index (i >= len(L) - 1), it returns 0.
- If the element at L[i] is greater than the element at L[i+1], it swaps them and returns 1 plus the result of the recursive call fun(L, i+1).
- Otherwise, it does not swap them and simply returns the result of the recursive call fun(L, i+1).
Thus, one complete call to fun(data) starting with i=0 will traverse the entire list, swap adjacent elements that are out of order, and return the total number of swaps performed during that pass.
Now, let's trace the execution of the loop: for _ in range(len(data)): count += fun(data). The list data initially is [5, 3, 4, 1, 2], and the length is 5, so the loop runs 5 times.
Pass 1:
We call fun([5, 3, 4, 1, 2], 0):
- i = 0: Compare data[0] (5) and data[1] (3). Since 5 > 3, swap them. List becomes [3, 5, 4, 1, 2]. Swap count increments (returns 1 + recursive call).
- i = 1: Compare data[1] (5) and data[2] (4). Since 5 > 4, swap them. List becomes [3, 4, 5, 1, 2]. Swap count increments.
- i = 2: Compare data[2] (5) and data[3] (1). Since 5 > 1, swap them. List becomes [3, 4, 1, 5, 2]. Swap count increments.
- i = 3: Compare data[3] (5) and data[4] (2). Since 5 > 2, swap them. List becomes [3, 4, 1, 2, 5]. Swap count increments.
- i = 4: i >= 4 (since len(L) - 1 = 4), return 0.
Total swaps in Pass 1 = 1 + 1 + 1 + 1 + 0 = 4.
The list is now [3, 4, 1, 2, 5], and count becomes 4.
Pass 2:
We call fun([3, 4, 1, 2, 5], 0):
- i = 0: Compare 3 and 4. No swap.
- i = 1: Compare 4 and 1. Swap them. List becomes [3, 1, 4, 2, 5]. Swap count increments.
- i = 2: Compare 4 and 2. Swap them. List becomes [3, 1, 2, 4, 5]. Swap count increments.
- i = 3: Compare 4 and 5. No swap.
- i = 4: Return 0.
Total swaps in Pass 2 = 1 + 1 = 2.
The list is now [3, 1, 2, 4, 5], and count becomes 4 + 2 = 6.
Pass 3:
We call fun([3, 1, 2, 4, 5], 0):
- i = 0: Compare 3 and 1. Swap them. List becomes [1, 3, 2, 4, 5]. Swap count increments.
- i = 1: Compare 3 and 2. Swap them. List becomes [1, 2, 3, 4, 5]. Swap count increments.
- i = 2: Compare 3 and 4. No swap.
- i = 3: Compare 4 and 5. No swap.
- i = 4: Return 0.
Total swaps in Pass 3 = 1 + 1 = 2.
The list is now [1, 2, 3, 4, 5], and count becomes 6 + 2 = 8.
Pass 4 & 5:
For the remaining two passes, the list is already sorted: [1, 2, 3, 4, 5]. No adjacent elements will be out of order, so no swaps will occur, and fun(data) will return 0 for both passes.
Thus, the final value of count remains 8.
Access expert-curated educational resources and study materials—completely free.
Create, conduct, and manage professional online assessments with Mindyard. Perfect for teachers and institutes.
Copyright © 2026 Mindyard. All Rights Reserved.