Let P be the quicksort program where the larger of the first two distinct elements is chosen as the pivot element. Which of the following input permutations will lead the best performance of P?
[1234]
[4321]
[1324]
[1342]
Answers
Answer:
your answer is 1324
Explanation:
would be t1>t2, because the first case is the worst case of quicksort i.e. minimum number is chosen as pivot. Hence in the worst case the comparisons are high.
The splitting occurs as
[1][2345]
[2][345]
[3][45]
[4][5]
and
[123][45]
[1][23][4][5]
[2][3]
Number of recursive calls remain the same, but in second case the number of elements passed for the recursive call is less and hence the number of comparisons also less.
1342
Explanation:
A sorting approach that sequences a listing with the aid of using constantly dividing the listing into elements and transferring the decreased objects to 1 aspect and the better objects to the other.
It begins evolved with the aid of using selecting one object withinside the complete listing to function as a pivot point.
The pivot might be the primary object or a randomly selected one.
In the easy QuickSort algorithm, we pick a detail as a pivot, partition the array around pivot and recur for subarrays on the left and proper of a pivot.
Consider an array which has many redundant elements.