GATE | GATE-CS-2016 (Set 1) | Question 23

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

(A) Θ(n log n), Θ(n log n) and Θ(n2)

(B) Θ(n2), Θ(n2) and Θ(n Log n)



(C) Θ(n2), Θ(n log n) and Θ(n log n)

(D) Θ(n2), Θ(n log n) and Θ(n2)


Answer: (D)

Explanation:

  • Insertion Sort takes Θ(n2) in worst case as we need to run two loops. The outer loop is needed to one by one pick an element to be inserted at right position. Inner loop is used for two things, to find position of the element to be inserted and moving all sorted greater elements one position ahead. Therefore the worst case recursive formula is T(n) = T(n-1) + Θ(n).
  • Merge Sort takes Θ(n Log n) time in all cases. We always divide array in two halves, sort the two halves and merge them. The recursive formula is T(n) = 2T(n/2) + Θ(n).
  • QuickSort takes Θ(n2) in worst case. In QuickSort, we take an element as pivot and partition the array around it. In worst case, the picked element is always a corner element and recursive formula becomes T(n) = T(n-1) + Θ(n). An example scenario when worst case happens is, arrays is sorted and our code always picks a corner element as pivot.

 

 

Quiz of this Question



My Personal Notes arrow_drop_up