Data Structures & AlgorithmsVery hardTCS Digital · Infosys SP

One Hoare partition with the FIRST element as pivot (i moves right while a[i] < pivot, j moves left while a[j] > pivot, swap while i < j, return j) is applied to [11, 22, 2, 55, 7, 32, 57, 44]. What is the array afterwards and the returned index j?

  1. A. [7, 2, 22, 55, 11, 32, 57, 44]; j = 2
  2. B. [2, 7, 11, 22, 32, 57, 44, 55]; j = 1
  3. C. [7, 2, 22, 55, 11, 32, 57, 44]; j = 1
  4. D. [2, 7, 11, 22, 32, 44, 55, 57]; j = 1

Correct answer: C. [7, 2, 22, 55, 11, 32, 57, 44]; j = 1

Hoare's scheme swaps out-of-place pairs from both ends and does NOT put the pivot in its final position; it only guarantees a[0..j] ≤ pivot ≤ a[j+1..]. Tracing gives [7, 2, 22, 55, 11, 32, 57, 44] with j = 1.

More Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questions

Exams that ask this topic