Data Structures & AlgorithmsHardGATE CS · DSA

One Lomuto partition (pivot = LAST element; i marks the end of the 'less than pivot' region; when a[j] < pivot, increment i and swap a[i], a[j]; finally swap the pivot into position i + 1) is applied to [30, 56, 7, 33, 29, 39, 57, 32]. What is the resulting array?

  1. A. [7, 29, 30, 32, 33, 39, 56, 57]
  2. B. [30, 7, 29, 32, 56, 39, 57, 33]
  3. C. [30, 7, 29, 32, 56, 33, 39, 57]
  4. D. [29, 7, 56, 33, 30, 39, 57, 32]

Correct answer: B. [30, 7, 29, 32, 56, 39, 57, 33]

Smaller elements are swapped forward in scan order (so their relative order is kept here, but the larger ones get shuffled). The pivot 32 lands at index 3, its final sorted position: [30, 7, 29, 32, 56, 39, 57, 33].

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

Exams that ask this topic