Computer Sciences > Gate 2016 Set-2 > Complexity
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) 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

Correct : C

Given:
Input sequence is already sorted in ascending order.
Four statements about running times of Quicksort, Bubblesort, Mergesort, and Insertion sort.
To find:
Which of the statements are TRUE.
Solution:
Let me check each statement one by one for an already sorted input.
First, looking at statement I (Quicksort):
In standard Quicksort, the pivot is usually chosen as the first or the last element.
When the array is already sorted, this choice produces the most unbalanced partition possible, of sizes 0 and n - 1.
So the recurrence relation becomes:
T(n) = T(n - 1) + Θ(n)
Solving this recurrence gives:
T(n) = Θ(n2)
So statement I is TRUE.
Now, checking statement II (Bubblesort):
An optimized Bubblesort uses a flag to detect if any swap occurred during a pass.
Since the array is already sorted, no swaps occur in the very first pass of n - 1 comparisons, and the algorithm terminates immediately.
So the running time is Θ(n), not Θ(n2).
Thus, statement II is FALSE.
Next, checking statement III (Mergesort):
Mergesort always divides the list into two equal halves irrespective of the input order.
Its recurrence relation is always:
T(n) = 2T(n/2) + Θ(n)
Solving this always yields Θ(n log n) time in all cases.
So statement III is FALSE.
Finally, checking statement IV (Insertion sort):
For an already sorted array, each element is compared only once with its predecessor and no shifts are needed.
So the inner loop runs in O(1) time for every element.
The total number of comparisons is simply n - 1, which gives a running time of Θ(n).
So statement IV is TRUE.
Therefore, statements I and IV are TRUE.
Answer:
(D) I and IV only

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
The worst case running times of Insertion sort, Merge sort and Quick sort, respectively, are:
#143 MCQ

Related Topics

sorting algorithms quicksort time complexity bubblesort time complexity mergesort time complexity insertion sort time complexity GATE computer science 2016 sorting algorithms analysis algorithm efficiency GATE 2016 Set-2

Unique Visitor Count

Total Unique Visitors

Loading......