Optimize the merge algorithm