Question

The worst case running times of Insertion sort, Merge sort and Quick sort, respectively, are:

Choose one option
Official answer and solution
Official answer
D
From
Official GATE 2016 answer key

Solution

Recall the asymptotic worst-case time complexities of fundamental comparison-based sorting algorithms.

Examine the worst-case time complexity of each algorithm:

  1. Insertion Sort: In the worst case (reverse sorted input), each element ii is compared with and shifts all i−1i-1 preceding elements. Total comparisons =∑i=1n−1i=Θ(n2)= \sum_{i=1}^{n-1} i = \Theta(n^2).
  2. Merge Sort: Divides the list into two halves (2T(n/2)2 T(n/2)) and merges them in Θ(n)\Theta(n) time. By the Master Theorem, T(n)=2T(n/2)+Θ(n)=Θ(nlog⁡n)T(n) = 2T(n/2) + \Theta(n) = \Theta(n \log n) in all cases (worst, average, and best).
  3. Quick Sort: In the worst case (e.g. partition produces highly unbalanced subproblems of sizes 00 and n−1n-1), the recurrence is T(n)=T(n−1)+Θ(n)=Θ(n2)T(n) = T(n-1) + \Theta(n) = \Theta(n^2).

Therefore, the worst-case running times are Θ(n2)\Theta(n^2), Θ(nlog⁡n)\Theta(n \log n), and Θ(n2)\Theta(n^2), respectively.

Final answer

D

Common trap

Stating Θ(nlog⁡n)\Theta(n \log n) for Quicksort, which is its average-case running time, rather than its worst-case running time Θ(n2)\Theta(n^2).

The answer is the one in the official GATE answer key. The solution is this site's own explanation.

Source

Paper
GATE 2016 Computer Science and Information Technology (CS) Session 1
Question
23 of 65, printed as CS Q.13
Section
Computer Science and Information Technology

Report an error in this question

GATE questions are the work of the organising institutes. This site is independent and is not affiliated with them.