Merge Sort

Merge sort is a recursive algorithm, which means that it can be a little hard to think about. But the heart of the merge algorithm is something that we'll call the merge operation. It can sort the array, but only when each half of it is already sorted. It looks like this:

The merge operation works like this:
  1. Copy the left half into an auxiliary space.
  2. Repeatedly compare the 1st element of the left half with the 1st element of the right half:
  3. Keep going until both halves are exhausted. The resulting array will be sorted.
Note that the merge operation is linear. We'll come back to this later.

Of course, this only works if both halves are pre-sorted. So to pre-sort them, we use merge sort...recursively. Here's the full merge sort algorithm:

  1. If the left half has at least 2 elements, recurse on the left side.
  2. If the right half has at least 2 elements, recurse on the right side.
  3. Each half is now sorted, so do the merge operation.
Here it is:

And here it is on a longer data set:

Merge sort is slightly adaptive. When the left side has been exhausted during the merge operation, all elements must be in place and it can skip over the remaining right side, as shown here:

The speed-up may be significant, but it is not nearly as dramatic as with insertion sort.

Uniquely among linearithmic sorting algorithms, merge sort is stable. When the merge operation encounters a tie within the left side and the right side, it should default to choosing the left side:

Among all these generalized sorts, merge sort is widely considered the best "all around sort". It does have its weaknesses: it is O(n) in space complexity, and it is not adaptive as some other sorts. However it is stable, and it's at least slightly adaptive.

Here's everything so far:

On to quick sort...