merge function for mergesort