GATE Computer ScienceVery hardGATE CSE · NIELIT

The Hoare partition scheme is applied once to A = [5, 3, 8, 5, 1, 9, 5, 2, 7, 4] with pivot x = A[0] = 5, using: i = −1, j = 10; loop { do j−−; while A[j] > x; do i++; while A[i] < x; if i < j swap A[i], A[j] else return j }. What is the array after the call returns?

  1. A. [3, 1, 2, 4, 5, 9, 5, 8, 7, 5]
  2. B. [5, 3, 4, 5, 1, 2, 5, 9, 7, 8]
  3. C. [4, 3, 2, 5, 1, 9, 5, 8, 7, 5]
  4. D. [4, 3, 2, 1, 5, 5, 9, 8, 7, 5]

Correct answer: C. [4, 3, 2, 5, 1, 9, 5, 8, 7, 5]

Tracing the swaps (the scans stop on elements equal to the pivot) gives [4, 3, 2, 5, 1, 9, 5, 8, 7, 5] with returned index 4.

More Programming and Data Structures (GATE) questions

Exams that ask this topic