Loading practice problems…
Loading your practice workspace…
Loading code editor…
19 / 29
Sign in
Algorithms/Merge Sort

Sort an Array

medium35 min

Given an array of integers, return the values in ascending order.

Implement merge sort instead of using a built-in sorting function.

Example 1

Inputnums = [5, 2, 3, 1]

Output[1, 2, 3, 5]

Reading the values from smallest to largest gives 1, 2, 3, and 5.

Example 2

Inputnums = [5, 1, 1, 2, 0, 0]

Output[0, 0, 1, 1, 2, 5]

Both 0s come first, then both 1s, followed by 2 and 5.

Constraints

  • 1 ≤ nums.length ≤ 50,000
  • -50,000 ≤ nums[i] ≤ 50,000
Merge SortDivide and Conquer