Computer Sciences > Gate 2016 Set-1 > Complexity
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), Θ(n22) and Θ(n Log n)
C
Θ(n2), Θ(n log n) and Θ(n log n)
D
Θ(n2), Θ(n log n) and Θ(n2)

Correct : C

Given:
Three sorting algorithms: Insertion sort, Merge sort, and Quick sort.
To find:
The worst case running times of Insertion sort, Merge sort, and Quick sort, respectively.
Solution:
Let me analyze the worst case running time for each algorithm one by one.
First, consider Insertion sort.
In the worst case, the input array is reverse sorted.
For each element at index i, we have to make i comparisons and shifts.
Total work done is proportional to 1 + 2 + 3 + ... + (n - 1) = (n(n - 1))/2, which gives Θ(n2).
Next, consider Merge sort.
Merge sort divides the array into two halves, sorts them recursively, and merges them in linear time.
The recurrence relation is T(n) = 2T(n/2) + Θ(n).
Solving this recurrence using Master theorem gives T(n) = Θ(n log n) in all cases, including the worst case.
Now, consider Quick sort.
The worst case occurs when the chosen pivot always partitions the array into two subproblems of sizes 0 and n - 1.
This happens, for instance, when the array is already sorted and we pick the first or last element as the pivot.
The recurrence becomes T(n) = T(n - 1) + Θ(n).
Solving this gives T(n) = Θ(n2).
So, combining all three in order, the worst case times are Θ(n2), Θ(n log n), and Θ(n2).
Checking the options, this matches with option D.
Answer: (D) Θ(n2), Θ(n log n) and Θ(n2)

Similar Questions

Consider a complete binary tree where the left and the right subtrees of the root are max-heaps. The lower bound for the number of operations to convert the tre...
#50 MCQ
Which one of the following is the recurrence equation for the worst case time complexity of the Quicksort algorithm for sorting n ( ≥ 2) numbers? In the recurr...
#81 MCQ
Assume that the algorithms considered here sort the input sequences in ascending order. If the input is already in ascending order, which of the following are T...
#109 MCQ

Related Topics

Insertion sort worst case Merge sort worst case Quick sort worst case Sorting algorithms complexity Time complexity algorithms GATE Computer Sciences 2016 GATE CS 2016 Set-1 Q25 Algorithm analysis

Unique Visitor Count

Total Unique Visitors

Loading......