GATE Computer ScienceVery hardGATE CSE · NIELIT

Lomuto partition (pivot = last element, i starts at −1, for each j with A[j] ≤ pivot do i++ and swap A[i], A[j]; finally swap A[i+1] with the pivot) is applied to [8, 3, 10, 1, 7, 12, 5, 9, 2, 6]. Counting every executed swap statement, including swaps of an element with itself, how many swaps occur, and what is the final index of the pivot?

  1. A. 5 swaps, pivot at index 4
  2. B. 4 swaps, pivot at index 4
  3. C. 5 swaps, pivot at index 3
  4. D. 6 swaps, pivot at index 5

Correct answer: A. 5 swaps, pivot at index 4

Elements ≤ 6 are 3,1,5,2 giving 4 in-loop swaps, plus 1 final swap = 5; the pivot lands at index 4 (final array 3 1 5 2 6 12 10 9 8 7).

More Algorithms questions

Exams that ask this topic