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)
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.
Completely free for students. Educators & coaching institutes get 2 months free Pro trial to create tests.
Access unlimited practice questions, detailed step-by-step solutions, and exam study materials — completely free for students.
Create and conduct professional online assessments with AI question generation, tab proctoring, and comprehensive batch management.
Prepare for GATE 2026 Civil Engineering with the latest question papers, previous year papers, detailed solutions, syllabus, exam pattern, preparation tips, mock tests, and subject-wise study material. Download GATE 2026 CE question papers with solutions in PDF format and boost your preparation for top engineering institutes and PSU recruitment.
Prepare effectively for GATE 2026 Electronics & Communication Engineering (EC) with this comprehensive Question Paper with Solutions. This resource includes detailed explanations, concept-wise solutions, previous year paper analysis, and exam-oriented practice to help aspirants strengthen core subjects like Electronic Devices, Analog Circuits, Digital Circuits, Signals & Systems, Network Theory, Control Systems, Electromagnetics, Communications, and more. Perfect for revision, self-assessment, and boosting your GATE 2026 EC preparation strategy.
Copyright © 2026 Combe Labs Private Limited. All Rights Reserved.