GateAI
Searching, Sorting and Hashing

GATE CSE 2014 Set 1 · Q14

MCQ1 markMedium

Time

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

Select one answerSingle correct

Pick an option to check

Solution

Step-by-step verified solution

Official answer: C) t1 > t2

Searching, Sorting and Hashing

When Quicksort uses the first element as the pivot:

  1. Input 1:[1 2 3 4 5] is already sorted.

    • Partitioning an already sorted array of size nn using the first element results in the worst-case behavior with unbalanced partitions of sizes 00 and n−1n-1.

    • The number of comparisons made is:

      t1=(n−1)+(n−2)+⋯+1=4+3+2+1=10t_1 = (n-1) + (n-2) + \dots + 1 = 4 + 3 + 2 + 1 = 10
  2. Input 2:[4 1 5 3 2]

    • First pivot is 44, which partitions into elements smaller than 44 ([1, 3, 2]) and elements greater than 44 ([5]), requiring 44 comparisons.

    • Sub-array [1, 3, 2] with pivot 11 splits into [] and [3, 2], requiring 22 comparisons.

    • Sub-array [3, 2] with pivot 33 requires 11 comparison.

    • Sub-array [5] has size 1 (0 comparisons).

    • Total comparisons t2=4+2+1=7t_2 = 4 + 2 + 1 = 7.

Since t1=10t_1 = 10 and t2=7t_2 = 7, we have t1>t2t_1 > t_2. Hence, (C) is the correct option.

Try the question first, then check your work.

Think the official key or this write-up is wrong?

Report error or challenge

Examiner trap

The misconception this question tests

This Searching, Sorting and Hashing previous year question is Q14 of the GATE CSE 2014 Set 1 paper. Find more GATE CSE PYQs on Searching, Sorting and Hashing in Algorithms, or browse all GATE CSE PYQs.

Free diagnostic

Where do you stand for GATE?

Take the 20-minute diagnostic: the 3 topics that cost you the most marks, and a rank range. Or sit 5 related Searching, Sorting and Hashing PYQs now with no account.

One GATE CSE PYQ every morning

An official question by email at 7 AM IST, with a link to its verified solution. Unsubscribe in one click.

Searching, Sorting and Hashing in GATE CSE

Searching, Sorting and Hashing has 27 GATE CSE PYQs across 20 papers from 2012 to 2026. 9 of them are 1-mark MCQs like this one, and GATE CSE 2014 Set 1 has 3.

How GATE tests Searching, Sorting and Hashing

More from GATE CSE 2014 Set 1

Share this solution

A plain-text card for WhatsApp or Telegram study groups.

GateAI solution cardGATE CSE 2014 Set 1 · Q14 · MCQ

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?

Topic-wise GATE CS PYQs with verified steps

Independent practice explanation verified by the GateAI team. GATE is conducted by the IITs; question text follows the official paper.