Iteration

Merge Sort

Sorting

Here's how MergeSort works. The idea is to recursively sort the left and right sides of the array, and merge them together using the Merge function we created in the previous problem.

First Few Test Cases:

We'll solve this problem by creating a recursive function called mergeSort.

1. Recursion

Sorting the whole array is the same thing as sorting the left half and the right half and then merging them together using the merge function.

You can convert this idea into code pretty easily. Here's the code:

2. Base case

The recursion keeps calling mergeSort on smaller and smaller arrays. To find the base case, just check when the recursion breaks by looking at the smallest arrays. If we call the recursion on a size 3 or size 2 array it works fine, but when we call it on a size 1 and size 0 array it breaks. So those are the base cases:

3. Code

To code the solution, just combine the recursion and base case. The code for merge comes from the Merge problem.

Here's the full code, including the merge function:

Time Complexity O(nlogn)O(n \log n), equal to the total number of operations done by merge. See below.

Space Complexity O(n)O(n). See spoiler for details - can be reduced to O(1)O(1) using advanced ideas.

Just for fun, we took the above recursion and turned it into a picture. The gray boxes are function calls and the arrows are inputs/outputs. People get those crazy pictures of MergeSort by expanding the mergeSort boxes all the way down to the base case.

Mark as Completed:
Submits:
mergeSort
Test your code to get an output here!
mergeSort(
)
Test your code to get an output here!