Quick-sort is run on two inputs shown below to sort in ascending order taking first element as pivot
Let and be the number of comparisons made for the inputs (i) and (ii) respectively. Then,
View worked solution
Answer: C
In Quick-sort, when the first element is chosen as the pivot:
-
Input (i) is already sorted:
The pivot is the smallest element (). Partitioning requires comparing the pivot with the remaining elements. This splits the array into an empty left subarray and a right subarray of size .
The recurrence relation for the number of comparisons is: -
Input (ii) is reverse sorted:
The pivot is the largest element (). Partitioning requires comparing the pivot with the remaining elements. This splits the array into a left subarray of size and an empty right subarray.
The recurrence relation for the number of comparisons is:
Since both cases result in the same highly unbalanced partition at every recursive step (splitting into sizes and for an array of size ), the total number of comparisons is identical:
Therefore, .
Free practice · Your answers on this page are not saved.
