AlgoFrames

Implement merge sort

Standard · Write code · about 15 min

Sort nums ascending with merge sort and return the sorted array. Split the range in half, sort each half recursively, then merge the two sorted halves through a buffer.

The largest test has 10,000 values, so an O(n²) sort is too slow.

Example 1

Input:

[[5,2,4,7,1,3,2,6]]

Output:

[1,2,2,3,4,5,6,7]

Example 2

Input:

[[3,1]]

Output:

[1,3]

Constraints

  • 0 ≤ nums.length ≤ 10,000
  • -100,000 ≤ nums[i] ≤ 100,000

Learn the idea first: Merge sort, a visual lesson.

More Merge sort problems