GateAI
Programming in C

GATE CSE 2017 Set 2 · Q43

NAT2 marksMedium

Time

Consider the following snippet of a C program. Assume that swap (&x, &y) exchanges the contents of x and y.int main() { int array[] = {3, 5, 1, 4, 6, 2}; int done = 0; int i; while (done == 0) { done = 1; for (i=0; i<=4; i++) { if (array[i] < array[i+1]) { swap(&array[i], &array[i+1]); done = 0; } } for (i=5; i>=1; i--) { if (array[i] > array[i-1]) { swap(&array[i], &array[i-1]); done = 0; } } } printf("%d", array[3]); }The output of the program is __________.

Your answerNumerical answer

Enter a value to check

Solution

Step-by-step verified solution

Official answer: 3

Programming in C

The algorithm shown is a bidirectional bubble sort (cocktail shaker sort) sorting the array in descending order.

  1. Initial Array:

    array=[3,5,1,4,6,2]\text{array} = [3, 5, 1, 4, 6, 2]
  2. First pass of while (done == 0):

    • done is set to 11.

    • Forward loop (i = 0 to 4): Swaps adjacent elements if array[i]<array[i+1]\text{array}[i] < \text{array}[i+1]:

      • i=0i=0: 3<5  ⟹  swap  ⟹  [5,3,1,4,6,2]3 < 5 \implies \text{swap} \implies [5, 3, 1, 4, 6, 2], done = 0

      • i=1i=1: 3<13 < 1 (False)

      • i=2i=2: 1<4  ⟹  swap  ⟹  [5,3,4,1,6,2]1 < 4 \implies \text{swap} \implies [5, 3, 4, 1, 6, 2], done = 0

      • i=3i=3: 1<6  ⟹  swap  ⟹  [5,3,4,6,1,2]1 < 6 \implies \text{swap} \implies [5, 3, 4, 6, 1, 2], done = 0

      • i=4i=4: 1<2  ⟹  swap  ⟹  [5,3,4,6,2,1]1 < 2 \implies \text{swap} \implies [5, 3, 4, 6, 2, 1], done = 0

    • Backward loop (i = 5 down to 1): Swaps adjacent elements if array[i]>array[i−1]\text{array}[i] > \text{array}[i-1]:

      • i=5i=5: 1>21 > 2 (False)

      • i=4i=4: 2>62 > 6 (False)

      • i=3i=3: 6>4  ⟹  swap  ⟹  [5,3,6,4,2,1]6 > 4 \implies \text{swap} \implies [5, 3, 6, 4, 2, 1], done = 0

      • i=2i=2: 6>3  ⟹  swap  ⟹  [5,6,3,4,2,1]6 > 3 \implies \text{swap} \implies [5, 6, 3, 4, 2, 1], done = 0

      • i=1i=1: 6>5  ⟹  swap  ⟹  [6,5,3,4,2,1]6 > 5 \implies \text{swap} \implies [6, 5, 3, 4, 2, 1], done = 0

  3. Second pass of while (done == 0):

    • done is set to 11.

    • Forward loop (i = 0 to 4):

      • i=0i=0: 6<56 < 5 (False)

      • i=1i=1: 5<35 < 3 (False)

      • i=2i=2: 3<4  ⟹  swap  ⟹  [6,5,4,3,2,1]3 < 4 \implies \text{swap} \implies [6, 5, 4, 3, 2, 1], done = 0

      • i=3i=3: 3<23 < 2 (False)

      • i=4i=4: 2<12 < 1 (False)

    • Backward loop (i = 5 down to 1):

      • No elements satisfy array[i]>array[i−1]\text{array}[i] > \text{array}[i-1].

  4. Third pass:

    • No swaps occur in either loop, so done remains 11 and the while loop terminates.

  5. Output: The sorted array in descending order is [6,5,4,3,2,1][6, 5, 4, 3, 2, 1]. The element at index 33 is:

    array[3]=3\text{array}[3] = 3

Thus, the program prints 3.

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 Programming in C previous year question is Q43 of the GATE CSE 2017 Set 2 paper. Find more GATE CSE PYQs on Programming in C in Programming and Data Structures, 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 5 related Programming in C 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.

Programming in C in GATE CSE

Programming in C has 74 GATE CSE PYQs across 25 papers from 2011 to 2026. 17 of them are 2-mark NAT questions like this one, and GATE CSE 2017 Set 2 has 6.

How GATE tests Programming in C

More from GATE CSE 2017 Set 2

Share this solution

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

GateAI solution cardGATE CSE 2017 Set 2 · Q43 · NAT

Consider the following snippet of a C program. Assume that swap (&x, &y) exchanges the contents of x and y . int main() { int array[] = {3, 5, 1, 4, 6, 2}; int done = 0; int i; while (done == 0) { done = 1; for (i=0; i =1; i--) { if (array[i] > array[i-1]) { swap(&array[i], &array[i-1]); done = 0; } } } printf("%d", array[3]); } The output of the program is …

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.