Sort the following array a using quicksort,
[6, 11, 4, 9, 8, 2, 5, 8, 13, 7]
The pivot should be chosen as the arithmetic mean of the first and the last element, i.e., (a[0] + a[size - 1]) / 2 (rounded down).
Show all important steps such as partitioning and the recursive calls to the algorithm.
I understand how to sort the array using quicksort, however I’m not sure how to calculate the pivot.
Is the pivot calculated by 6 + 7 = 13 then 13 / 2 = 6.5 (rounded down is 6) so the pivot is 2 (i.e. the 6th element)?
I know the elements less than pivot appear on the left hand side, and elements greater than the pivot appear on the right hand side, and the partition repeats this step of sorting the sub-array.
Any help would be greatly appreciated.
For quicksort, the pivot can be whatever element you want.
Check out Wikipedia.
Three choices thus :
And in you case using the mean of first and last element value would give you :