- Divide the problem into sub-problems
- Usually (1,n−1) or (2n,2n)
- Solve the sub-problems
- Merge the sub-problems
Merge sort from left to middle, merge sort from middle to right and then merge the results:
- Divide: find the middle element
- Solve: merge sort the two sub-arrays
- Merge: mere the two sub-arrays
The base case is when the length of the array is 0 or 1.
For T(n), the cost will be either the base case of Θ(1), or the sum of:
- Subdivision: Θ(1) to find the center
- Solving sub-problems: 2T(2n)
- Merge: Θ(n)
{Θ(1)2T(2n)+Θ(n)n=1n>1
At each level, the cost will be:
Θ(n)=2Θ(2n)=4Θ(4n)…=Θ(n)
There are log2n levels, so the total cost is nlog2n.