Refactored merge function to improve readability and allowing result to be written back to original array to be sorted.