Practice
JavaScriptData StructuresReactConcepts
Sign in
← Back to problems

Merge Sort

Sortingmedium

Course · Section 11: Working With Arrays · Lecture 172: Sorting Arrays

Implement `mergeSort(arr)` returning the array sorted ascending using the merge sort algorithm.

Sample tests

Input: mergeSort([5,2,9,1,5,6])
Output: [1,2,5,5,6,9]
Input: mergeSort([1])
Output: [1]

+ 1 hidden test run on Submit.

Hints

Common pitfalls
  • Forgetting the base case of length 0 or 1.
  • An incorrect merge that drops the tail of one half.

Learning resources

  • MDN: Array.prototype.sort
Approach & explanation (try first)

Merge sort divides, sorts recursively, and merges. O(n log n) time, O(n) space.

Loading...
⌘/Ctrl + Enter

Run your code to see results.