Open In App

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

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 is TRUE?

I.   Quicksort runs in Θ(n2) time



II.  Bubblesort runs in Θ(n2) time

III. Mergesort runs in  Θ(n) time



IV.  Insertion sort runs in  Θ(n) time 

(A)

I and II only

(B)

I and III only

(C)

II and IV only

(D)

I and IV only

Answer: (D)
Explanation:

Input is already in ascending order, which means it is the worst-case situation.

Option 1: Quicksort runs in Θ (n2) time In quick sort worst case if the first or last element is selected at the pivot element.

For a Quicksort, in the worst-case recurrence, the relation will become T(n) = T(n-1) + T (1) + n, Which gives T(n) = Θ (n2) So, it is correct.

Option 2: Bubble sort runs in Θ (n2) time

If an array is already sorted in ascending order, then at that time there will be no swap after the completion of the inner for loop of bubble sort. In this way, bubble sort will take Θ (n) time complexity.

Option 3: Merge sort runs in Θ (n) time

Merge sort uses the divide and conquer policy to sort the elements. As elements are already sorted in ascending order.
Recurrence relation for merge sort: T(n) = 2 T (n/2) + n, This will give Θ (n*log(n)) time complexity.

Option 4: Insertion sort runs in Θ (n) time

When a new element that is greater than all the elements of the array is added, then there will be no swap but only a single comparison. In n -1 swaps, only 0 swaps and n-1 comparisons are there. The total time complexity in this case will be Θ (n). So, it is correct.

Quiz of this Question
Please comment below if you find anything wrong in the above post

Article Tags :