Skip to content
Onsite

Merge Sort

MediumP1Coding

Implement merge sort. Given an integer array nums, return a new array with the same elements in ascending order.

Do not call a built-in sort; write the divide-and-conquer yourself.

Example 1

Input
nums = [5,2,3,1]
Output
[1,2,3,5]

Example 2

Input
nums = [5,1,1,2,0,0]
Output
[0,0,1,1,2,5]

Example 3

Input
nums = []
Output
[]

Constraints

  • 0 <= nums.length <= 5 * 10^4
  • -10^5 <= nums[i] <= 10^5

Hints

Run checks the 3 visible tests, Submit all 8.