GATE CS 2025 Set 1 | Question 33

Algorithms › Sorting & Searching · NAT · 1 mark · Answer: 435

  1. Question 33Algorithmsnat1 mark
    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 be an array storing 30 distinct integers in descending order. The number of swap operations that will be performed, if fun() is called with , is __________. (Answer in integer)

    Answer: 435

    Solution

    Answer: 435 fun is bubble sort. For an array sorted in strictly descending order (the worst case), every compared adjacent pair is out of order, so each comparison causes a swap. The number of comparisons (and hence swaps) is

More GATE PYQ on Sorting & Searching

All GATE CS Algorithms PYQ →

Attempt GATE CS 2025 Set 1 in real GATE exam mode

Full paper with a timer, the GATE interface and a topic-wise analysis — free.

Create free account →