I have to do an exercise from my algorithm book. Suppose a mergesort is implemented to split an array by α, which is in a range from 0.1 to 0.9.
This is the original method to calculate the split point
middle = fromIndex + (toIndex - fromIndex)/2;
I would like to change it to this:
factor = 0.1; //varies in range from 0.1 to 0.9
middle = fromIndex + (toIndex - fromIndex)*factor;
So my questions are:
- Does this impact the computational complexity?
- What’s the impact on recursion tree depths?
This does change the actual complexity, but not the asymptotic complexity.
If you think about the new recurrence relation you’ll get, it will be
Looking over the recursion tree, each level of the tree still has a total of Θ(n) work per level, but the number of levels will be greater. Specifically, let’s suppose that 0.5 ≤ α < 1. Then after k recursive calls, the size of the smallest block remaining in the recursion will have size n αk. The recurrence stops when this hits size one. Solving, we get:
In other words, varying α varies the constant factor on the logarithmic term of the depth of the recursion. The above equation is minimized when α = 0.5 (since we are subject to the restriction that α ≥ 0.5), so this would be the optimal way to split. However, picking other splits still gives runtime Θ(n log n), though with a higher constant term.
Hope this helps!