Try to modify the merge-sort algorithm to solve this problem.