INDEX | HOME

MergeSort

There are two styles of MergeSort, one for arrays and one for linked lists.

The magic of mergesort is based on two facts:

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.

Copyright 2026 Joseph Graham (joseph@xylon.me.uk)