Skip to contentKEEP THE CRAFTTHE SANCTUARY

Sort an Array

Write merge sort: sort each half, then merge the two sorted halves.

The problem

Implement Solution.sortArray(nums) and return the numbers in ascending order. Write merge sort yourself: do not call sorted(), list.sort() or nums.sort(). For example, [5, 2, 3, 1] returns [1, 2, 3, 5]. 1 <= len(nums) <= 50000 and -50000 <= nums[i] <= 50000.

Starter code

class Solution:
    def sortArray(self, nums):
        pass

Public examples

  1. nums = [5, 2, 3, 1]gives[1, 2, 3, 5]
  2. nums = [9]gives[9]
  3. nums = [-3, 0, 4]gives[-3, 0, 4]

Solving it in the practice book needs an active paid plan.

Open it in the practice book
One thought at a time.