Let P be a quicksort program to sort numbers in ascending order using the first element as the pivot. Let t1 and t2 be the number of comparison made by P for the inputs [1 2 3 4 5] and [4 1 5 3 2] respectively. Which one of the following holds?
Let P be a quicksort program to sort numbers in ascending order using the first element as the pivot. Let t1 and t2 be the number of comparison made by P for the inputs [1 2 3 4 5] and [4 1 5 3 2] respectively. Which one of the following holds? Correct Answer t<sub>1</sub> > t<sub>2</sub>
Concept:
If the array is sorting is increasing or non-increasing order, choosing either first or last element given the worst-case time complexity in Quick Sort.
Explanation
In every step of quick sort, numbers are divided as per the following recurrence.
T(n) = T(n-1) + O(n)
Average case time complexity (t2) = O(logn)
Worst case time complexity (t1) = O(n2)
Since t1 has the worst case ∴ t1 > t2
Important Point:
Number of comparisons will vary based on implementation
Always in ascending or descending array (worst case) number of comparisons is higher