The Merge Process
- Explain and trace the merge sort algorithm on a particular data sequence.
Consider the following two sorted list of numbers, and . We are interested in combining (merging) these two lists, such that the resulting merged list remains sorted.
Here is the naive approach:
Step | Details | Runtime |
---|---|---|
I | Make a list, , large enough to hold all elements of & | |
II | Copy elements of to | |
III | Copy elements of to | |
VI | Sort |
depending on the language/data structure cost to construct the auxiliary space.
The will be linearithmic at best (for comparison-based sorting).
This solution does not make any use of the knowledge the two inputs were sorted, to begin with. So naturally, the question is: can we do better? And the answer is yes; with a bit of creativity, we can copy the numbers in and to , one at a time, and keep them in sorted order.