add optimization to avoid merge if two partitions are already sorted.