StreakPeaked· Practice

ExamsGATETechnical

Let P be a quicksort program to sort numbers in ascending order using the first element as the pivot. Let t1 and t2 be the number of comparisons made by P for the inputs [1 2 3 4 5] and [4 1 5 3 2] respectively. Which one of the following holds?

  1. t1 = 5
  2. t1 < t2
  3. t1 > t2
  4. t1 = t2

Correct answer: t1 > t2

Solution

For [1 2 3 4 5] (sorted, first-element pivot) comparisons are 4+3+2+1=10. For [4 1 5 3 2], pivot 4 makes 4 comparisons, then [1 3 2] makes 2+1=3 and [5] makes 0, total 7. So t1=10 > t2=7, i.e. t1 > t2.

Related GATE Technical questions

⚔️ Practice GATE Technical free + battle 1v1 →