Data Structures & AlgorithmsHardTCS NQT · Infosys pattern

One Lomuto partition (pivot = last element, i marks the end of the 'smaller' part, swap when a[j] < pivot) is applied to [7, 2, 9, 4, 3, 8, 6]. What is the array afterwards?

  1. A. 2 4 3 6 9 8 7
  2. B. 4 2 3 6 7 9 8
  3. C. 2 4 3 6 7 8 9
  4. D. 2 3 4 6 9 8 7

Correct answer: A. 2 4 3 6 9 8 7

Elements smaller than 6 are swapped forward in the order met: 2 (swaps with 7), 4 (swaps with 9), 3 (swaps with 7), giving 2 4 3 7 9 8 6; finally the pivot swaps with a[3] = 7: 2 4 3 6 9 8 7. The right part is not sorted, and the left part keeps the order 4, 3.

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

Exams that ask this topic