MergeSort
There are two styles of MergeSort, one for arrays and one for linked lists.
The magic of mergesort is based on two facts:
- a list of length 1 is inherrently sorted
- it is easy to merge two sorted lists into a new sorted list
This means we just need to split repeatedly until we have lists of length 1, and then merge them together on the way back up.
The way we do the merge differs depending on whether it's an array or linked list.
array version
For merging two arrays, repeatedly compare the head of each and take the smaller.
linked list version
For merging two linked lists, we can just "cut" and "splice" them together in the appropriate place.