Question

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

I. Quicksort runs in Θ(n2)\Theta(n^2) time
II. Bubblesort runs in Θ(n2)\Theta(n^2) time
III. Mergesort runs in Θ(n)\Theta(n) time
IV. Insertion sort runs in Θ(n)\Theta(n) time

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

Solution

Analyze the best-case time complexity of each sorting algorithm on an already sorted input array.

Evaluate the runtime of each algorithm when the input sequence of length nn is already in ascending order:

  1. Statement I (Quicksort): Standard deterministic quicksort (with pivot selected as first or last element) partitions an already sorted array into sub-arrays of size 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)
    Thus, Statement I is TRUE.
  1. Statement II (Bubblesort): With the standard early-termination flag (stopping if no swaps occur during a pass), an already sorted array requires only one pass of n−1n-1 comparisons, running in Θ(n)\Theta(n) time rather than Θ(n2)\Theta(n^2). Thus, Statement II is FALSE.
  1. Statement III (Mergesort): Mergesort always divides the list into halves and merges them regardless of initial order:
    T(n)=2T(n/2)+Θ(n)=Θ(nlog⁡n)T(n) = 2T(n/2) + \Theta(n) = \Theta(n \log n)
    It never runs in Θ(n)\Theta(n) time. Thus, Statement III is FALSE.
  1. Statement IV (Insertion sort): For an already sorted array, each element A[i]A[i] is compared only once with its predecessor A[i−1]A[i-1] and no shifts are required. The inner loop terminates immediately, taking a total of n−1n-1 comparisons, which is Θ(n)\Theta(n) time. Thus, Statement IV is TRUE.

Therefore, Statements I and IV are TRUE. This corresponds to option (D).

Final answer

D

Common trap

Believing Quicksort is always O(nlog⁡n)O(n \log n) or that Mergesort adapts to already sorted input.

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 2
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.