ProgrammingVery hardWipro · TCS NQT pattern

What will be the output of the following pseudocode? (One pass = one full sweep of the outer loop; indexed from 0.)

What will be the output of the following pseudocode? (One pass = one full sweep of the outer loop; indexed from 0.)
Integer arr[6] = {89, 69, 70, 20, 81, 27}
for (each p from 0 to 0)
    for (each i from 0 to 4 - p)
        if (arr[i] > arr[i + 1])
            Integer t = arr[i]
            arr[i] = arr[i + 1]
            arr[i + 1] = t
        end if
    end for
end for
for (each i from 0 to 5)
    Print arr[i]
end for
  1. A. 69, 70, 20, 81, 27, 89
  2. B. 89, 69, 70, 20, 81, 27
  3. C. 27, 81, 20, 70, 69, 89
  4. D. 20, 27, 69, 70, 81, 89

Correct answer: A. 69, 70, 20, 81, 27, 89

This is 1 pass of bubble sort (each pass bubbles the next-largest unsorted element toward the end, with the inner bound shrinking by 1 per pass). After 1 pass starting from [89, 69, 70, 20, 81, 27], the array is [69, 70, 20, 81, 27, 89] — not fully sorted like [20, 27, 69, 70, 81, 89], since only 1 sweep ran.

More Pseudocode and Code Tracing questions

Exams that ask this topic