Optimize merge algorithm.