Let be quicksort program to sort numbers in ascending order using the first element as the pivot. Let and be the number of comparisons made by P for the inputs and respectively. Which one of the following holds?
View worked solution
Answer: C
For a quicksort implementation that uses the first element as the pivot:
Case 1: Input
Since the input is already sorted, choosing the first element as the pivot results in the worst-case partitioning:
• Pivot : compared with ( comparisons). Remaining: .
• Pivot : compared with ( comparisons). Remaining: .
• Pivot : compared with ( comparisons). Remaining: .
• Pivot : compared with ( comparison).
Total comparisons .
Case 2: Input
• Pivot : compared with ( comparisons). Partitions into and .
• For : comparisons.
• For : Pivot is compared with ( comparisons). Partitions into and .
• For : Pivot is compared with ( comparison).
Total comparisons .
Since and , we have .
Free practice · Your answers on this page are not saved.
