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: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:
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...