GateAI
Sorting

GATE CSE 2025 Set 1 · Q33

NAT1 markEasy

Time

The pseudocode of a function fun() is given below:fun(int A[0,...,n-1]){ for i=0 to n-2 for j=0 to n-i-2 if (A[j]>A[j+1]) then swap A[j] and A[j+1] }Let A[0,…,29]A[0, \dots, 29] be an array storing 30 distinct integers in descending order. The number of swap operations that will be performed, if the function fun() is called with A[0,…,29]A[0, \dots, 29] as argument, is ________. (Answer in integer)

Your answerNumerical answer

Enter a value to check

Solution

Step-by-step verified solution

Official answer: 435

Sorting

The given pseudocode implements the standard Bubble Sort algorithm to sort an array in ascending order.

When the input array of n=30n = 30 distinct elements is strictly in descending order (reverse sorted), every adjacent comparison A[j] > A[j+1] evaluates to true. Thus, a swap is performed on every iteration of the inner loop.

The total number of swap operations equals the total number of comparisons (or total number of inversions):

Total swaps=∑i=0n−2(n−1−i)=n(n−1)2\text{Total swaps} = \sum_{i=0}^{n-2} (n - 1 - i) = \frac{n(n - 1)}{2}

For n=30n = 30:

Total swaps=30×292=15×29=435\text{Total swaps} = \frac{30 \times 29}{2} = 15 \times 29 = 435

Therefore, the number of swap operations performed is 435.

Try the question first, then check your work.

Think the official key or this write-up is wrong?

Report error or challenge

Examiner trap

The misconception this question tests

This Sorting previous year question is Q33 of the GATE CSE 2025 Set 1 paper. Find more GATE CSE PYQs on Sorting in Algorithms, or browse all GATE CSE PYQs.

Free diagnostic

Where do you stand for GATE?

Take the 20-minute diagnostic: the 3 topics that cost you the most marks, and a rank range. Or sit 3 related Sorting PYQs now with no account.

One GATE CSE PYQ every morning

An official question by email at 7 AM IST, with a link to its verified solution. Unsubscribe in one click.

Sorting in GATE CSE

Sorting has 3 GATE CSE PYQs across 3 papers from 2015 to 2025. This is the only 1-mark NAT question among them, and it is the only one in GATE CSE 2025 Set 1.

How GATE tests Sorting

More from GATE CSE 2025 Set 1

Share this solution

A plain-text card for WhatsApp or Telegram study groups.

GateAI solution cardGATE CSE 2025 Set 1 · Q33 · NAT

The pseudocode of a function fun() is given below: fun(int A[0,...,n-1]){ for i=0 to n-2 for j=0 to n-i-2 if (A[j]>A[j+1]) then swap A[j] and A[j+1] } Let A[0, , 29] be an array storing 30 distinct integers in descending order. The number of swap operations that will be performed, if the function fun() is called with A[0, , 29] as argument, is … (Answer in integer)

Topic-wise GATE CS PYQs with verified steps

Independent practice explanation verified by the GateAI team. GATE is conducted by the IITs; question text follows the official paper.