Data Structures & AlgorithmsVery hardGATE CS · TCS NQT · Product Company SDE

Quicksort with Lomuto partitioning (last element as pivot) sorts [52, 89, 77, 41, 89, 97, 29, 94]. How many swaps happen in total, across all recursive calls?

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

Correct answer: B. 6

Each partition call swaps a[i] with a[j] whenever a smaller-or-equal element is found, plus the final pivot swap; summed over all recursive calls this totals 6 swaps.

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

Exams that ask this topic