Problem · Array

Implement Merge Sort

Learn this problem
MediumSalesforce logoSalesforceNEW GRADONSITE INTERVIEW
See Salesforce hiring insights

Problem statement

Given an integer array nums, return a new array containing the same values in nondecreasing order.

Implement merge sort: recursively split the range, sort both halves, and merge the sorted halves.

Function

mergeSort(nums: int[]) → int[]

Examples

Example 1

nums = [5,2,3,1]return = [1,2,3,5]

The two sorted halves merge into nondecreasing order.

Example 2

nums = [5,1,1,2,0,0]return = [0,0,1,1,2,5]

Duplicate values are retained.

Constraints

  • 0 <= nums.length <= 100000
  • -1000000000 <= nums[i] <= 1000000000

More Salesforce problems

drafts saved locally
public int[] mergeSort(int[] nums) {
    // Write your code here
}
nums[5,2,3,1]
expected[1,2,3,5]
checking account