Merge sort¶
Merge sort is a sorting algorithm based on the divide-and-conquer strategy, involving the "divide" and "merge" phases shown in the figure below.
- Divide phase: Recursively split the array from the midpoint, transforming the sorting problem of a long array into that of shorter arrays.
- Merge phase: Stop dividing when the length of the sub-array is 1, start merging, and continuously combine two shorter ordered arrays into one longer ordered array until the process is complete.
Algorithm workflow¶
As shown in the figure below, the "divide phase" recursively splits the array from the midpoint into two sub-arrays from top to bottom.
- Calculate the midpoint
mid
, recursively divide the left sub-array (interval[left, mid]
) and the right sub-array (interval[mid + 1, right]
). - Continue with step
1.
recursively until the sub-array interval length is 1 to stop.
The "merge phase" combines the left and right sub-arrays into a single ordered array from bottom to top. Note that merging starts with sub-arrays of length 1, and each sub-array is ordered during the merge phase.
It is observed that the order of recursion in merge sort is consistent with the post-order traversal of a binary tree.
- Post-order traversal: First recursively traverse the left subtree, then the right subtree, and finally handle the root node.
- Merge sort: First recursively handle the left sub-array, then the right sub-array, and finally perform the merge.
The implementation of merge sort is shown in the following code. Note that the interval to be merged in nums
is [left, right]
, while the corresponding interval in tmp
is [0, right - left]
.
Algorithm characteristics¶
- Time complexity of \(O(n \log n)\), non-adaptive sort: The division creates a recursion tree of height \(\log n\), with each layer merging a total of \(n\) operations, resulting in an overall time complexity of \(O(n \log n)\).
- Space complexity of \(O(n)\), non-in-place sort: The recursion depth is \(\log n\), using \(O(\log n)\) stack frame space. The merging operation requires auxiliary arrays, using an additional space of \(O(n)\).
- Stable sort: During the merging process, the order of equal elements remains unchanged.
Linked List sorting¶
For linked lists, merge sort has significant advantages over other sorting algorithms, optimizing the space complexity of the linked list sorting task to \(O(1)\).
- Divide phase: "Iteration" can be used instead of "recursion" to perform the linked list division work, thus saving the stack frame space used by recursion.
- Merge phase: In linked lists, node addition and deletion operations can be achieved by changing references (pointers), so no extra lists need to be created during the merge phase (combining two short ordered lists into one long ordered list).
Detailed implementation details are complex, and interested readers can consult related materials for learning.