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 time
II. Bubblesort runs in time
III. Mergesort runs in time
IV. Insertion sort runs in time
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 is already in ascending order:
- Statement I (Quicksort): Standard deterministic quicksort (with pivot selected as first or last element) partitions an already sorted array into sub-arrays of size and . The recurrence is:Thus, Statement I is TRUE.
- 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 comparisons, running in time rather than . Thus, Statement II is FALSE.
- Statement III (Mergesort): Mergesort always divides the list into halves and merges them regardless of initial order:It never runs in time. Thus, Statement III is FALSE.
- Statement IV (Insertion sort): For an already sorted array, each element is compared only once with its predecessor and no shifts are required. The inner loop terminates immediately, taking a total of comparisons, which is time. Thus, Statement IV is TRUE.
Therefore, Statements I and IV are TRUE. This corresponds to option (D).
D
Believing Quicksort is always 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.