ProgrammingHardTCS NQT · Infosys · Accenture

What will be the output of the following pseudocode?

What will be the output of the following pseudocode?
Integer A[5] = {5, 1, 4, 2, 8}, t
for (each j from 0 to 3)
    if (A[j] > A[j + 1])
        swap A[j] and A[j + 1]
    end if
end for
Print A[0], A[1], A[2], A[3], A[4]
  1. A. 1 2 4 5 8
  2. B. 1 4 2 5 8
  3. C. 1 4 2 8 5
  4. D. 5 1 4 2 8

Correct answer: B. 1 4 2 5 8

One bubble pass: 5 moves right past 1, 4, 2 and stops before 8, giving 1 4 2 5 8. A single pass does not fully sort the array.

More Pseudocode and Code Tracing questions

Exams that ask this topic