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
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
Total Unique Visitors