Data Structures & AlgorithmsVery hardProduct placement · Amazon/Microsoft

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 [28, 7, 13, 18, 27, 11, 44, 47]. What is the array afterwards and the returned index j?

  1. A. [11, 7, 13, 18, 27, 28, 44, 47]; j = 5
  2. B. [7, 13, 18, 27, 11, 28, 47, 44]; j = 4
  3. C. [7, 11, 13, 18, 27, 28, 44, 47]; j = 4
  4. D. [11, 7, 13, 18, 27, 28, 44, 47]; j = 4

Correct answer: D. [11, 7, 13, 18, 27, 28, 44, 47]; j = 4

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 [11, 7, 13, 18, 27, 28, 44, 47] with j = 4.

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

Exams that ask this topic