GATE DA 2026 Set 1 — Question 58
Go beyond PYQs with Success TrackerAI-powered personalised practice and doubt support. Unlimited practice on eligible plans; AI usage limits apply.NAT+2 / -0MediumBubble SortSearching & SortingProgramming, Data Structures & AlgorithmsRecursionFunctions, File & Error Handling
Programming, Data Structures & Algorithms → Functions, File & Error Handling → Bubble Sort
Last updated
Question
Consider the given Python program.The output of the program is __________ . (Answer in integer)
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
8 to 8
Solution
The function
fun(L) implements one pass of the Bubble Sort algorithm recursively. It iterates through the list L and swaps adjacent elements if they are in the wrong order (). It returns the total number of swaps performed in that pass.The main loop runs len(data) times (5 times), which is sufficient to fully sort the array using Bubble Sort.Initial Array: [5, 3, 4, 1, 2]Pass 1:- Compare 5, 3: Swap.
[3, 5, 4, 1, 2]. Count = 1. - Compare 5, 4: Swap.
[3, 4, 5, 1, 2]. Count = 2. - Compare 5, 1: Swap.
[3, 4, 1, 5, 2]. Count = 3. - Compare 5, 2: Swap.
[3, 4, 1, 2, 5]. Count = 4. - Total swaps in Pass 1: 4.
count= 4.
[3, 4, 1, 2, 5])- Compare 3, 4: No swap.
- Compare 4, 1: Swap.
[3, 1, 4, 2, 5]. Count = 1. - Compare 4, 2: Swap.
[3, 1, 2, 4, 5]. Count = 2. - Compare 4, 5: No swap.
- Total swaps in Pass 2: 2.
count= 4 + 2 = 6.
[3, 1, 2, 4, 5])- Compare 3, 1: Swap.
[1, 3, 2, 4, 5]. Count = 1. - Compare 3, 2: Swap.
[1, 2, 3, 4, 5]. Count = 2. - Compare 3, 4: No swap.
- Compare 4, 5: No swap.
- Total swaps in Pass 3: 2.
count= 6 + 2 = 8.
[1, 2, 3, 4, 5])- Array is sorted. No swaps occur.
- Total swaps in Pass 4: 0.
count= 8.
- Array is sorted. No swaps occur.
- Total swaps in Pass 5: 0.
count= 8.
Continue learning with Success Tracker
A step still unclear? Work through it with support
Use Success Tracker to ask about the reasoning, then try another GATE DA question to check your understanding.
AI-powered practice· Unlimited practice on eligible plans
- PYQs with solutions
- Attempt available previous-year questions, then compare your reasoning with the worked solution. Coverage varies by stream.
- Practice that adapts
- Choose a topic, work on weaker areas and bookmark questions to revisit. Your attempts feed your progress tracking.
- AI doubt support
- Ask follow-up questions about a step or concept while practising, instead of stopping at the final answer.
Unlimited practice is available on eligible plans. Free practice and AI usage have limits; check the current plan allowances before choosing.
This page stays readable without an account. AI responses can be wrong; check them against the solution and source material.
More questions on Functions, File & Error Handling
2026 Set 1 Q15Consider that the quick sort algorithm is used to sort an array of distinct randomly ordered…2026 Set 1 Q16Consider the given Python program. [code] Which of the following is the correct output of this…2026 Set 1 Q25You are given the following Pre-order and In-order traversals of a Binary Tree T with nodes E, F,…2026 Set 1 Q31Let A be a sorted array containing 1000 distinct integers. You perform a recursive binary search on…2026 Set 1 Q39A recursive function in Python is given. [code] Now, consider the following function call:…