Merge Sort

Implement mergeSort

Given an array of integers, sort it in non-decreasing order using **Merge Sort** — repeatedly split the array in half, sort each half, then merge the two sorted halves back together. This is the classic divide-and-conquer sort: splitting always takes O(log n) levels, and merging two sorted halves takes O(n) work at each level, giving O(n log n) overall — reliably faster than the simple O(n²) sorts on large inputs, and (unlike quicksort) with no bad-input case that degrades it.

Example 1:

Input: arr = [7,2,9,4,2,8]

Output: [2,2,4,7,8,9]

Example 2:

Input: arr = []

Output: []

Example 3:

Input: arr = [5]

Output: [5]

+ 5 hidden test cases run on Submit.

Constraints:

  • 0 ≤ arr.length ≤ 200
  • -1000 ≤ arr[i] ≤ 1000
  • Sort in non-decreasing order

arr =

[7, 2, 9, 4, 2, 8]