Assume that the algorithms considered here sort the input sequences in ascending order. If the input is already in the ascending order, which of the following are TRUE?
- Quicksort runs in time
- Bubblesort runs in time
- Mergesort runs in time
- Insertion sort runs in time
View worked solution
Answer: D
To determine which statements are true when the input sequence is already sorted in ascending order, let us analyze each sorting algorithm's behavior:
-
I. Quicksort runs in time:
In the standard implementation of Quicksort (where the pivot is chosen as the first, last, or a fixed element), an already sorted array results in extremely unbalanced partitions at each step (one subproblem of size and another of size ). This yields the recurrence relation: Solving this gives a time complexity of . Thus, Statement I is TRUE. -
II. Bubblesort runs in time:
An optimized version of Bubblesort uses a modified flag to check if any swaps were made during a pass. If the input is already sorted, the algorithm terminates after the first pass, requiring only comparisons and swaps. This results in a best-case time complexity of . Thus, Statement II is FALSE. -
III. Mergesort runs in time:
Mergesort is a divide-and-conquer algorithm that always divides the array into two equal halves and merges them, regardless of the initial order of elements. Its recurrence relation is always: This results in a time complexity of in all cases (best, worst, and average). Thus, Statement III is FALSE. -
IV. Insertion sort runs in time:
In Insertion Sort, each element is compared with its predecessor. Since the array is already sorted, each element (from index to ) is compared only once with the element immediately to its left and no shifts are needed. The total number of comparisons is , which gives a best-case time complexity of . Thus, Statement IV is TRUE.
Since statements I and IV are true, the correct option is D.
Free practice · Your answers on this page are not saved.
