ProgrammingHardTCS NQT · Accenture pattern

What will be the output of the following pseudocode?

What will be the output of the following pseudocode?
Integer a[] = {4, 7, 1, 9, 3}
for (i = 0 to 3)
    if (a[i] > a[i + 1])
        swap(a[i], a[i + 1])
    end if
end for
Print a
  1. A. 1 3 4 7 9
  2. B. 4 1 7 3 9
  3. C. 4 1 7 9 3
  4. D. 1 4 7 3 9

Correct answer: B. 4 1 7 3 9

This is a single bubble-sort pass: it moves the largest value (9) to the end but does not sort the rest. 7 > 1 swaps, 7 < 9 no swap, 9 > 3 swaps → 4 1 7 3 9.

More Pseudocode and Code Tracing questions

Exams that ask this topic