Merge sort is the other classic divide-and-conquer sort. Instead of splitting carefully and combining trivially (as quick sort does), it splits trivially and does the work when combining: it merges sorted runs into longer sorted runs.
Merging
Merging two sorted lists of total length n takes O(n) time: repeatedly move the smaller front element to the output (taking from the left list when the fronts are equal, which keeps the sort stable), then copy what is left.
Bottom-up 2-way merge sort
Treat the input as n runs of length 1. Merge them in pairs into runs of length 2, then pairs of those into runs of length 4, and so on, until one run remains. Each pass handles every element once (O(n)), and there are ⌈log2n⌉ passes, so the time is O(nlogn) in every case. The merging needs an extra array of n elements.
Properties
Property
Merge sort
Time (best, average, worst)
O(nlogn)
Extra space for arrays
O(n) (a second array of n elements)
Stable
Yes (take from the left run on ties)
Linked lists
Very suitable: merging just relinks nodes, no extra array
External sorting
The basis of sorting files on disk (last section)
The top-down version (split in half recursively, sort both halves, merge) does the same work; the bottom-up version avoids recursion.
Key takeaways
Merge two sorted runs in O(n); copy the leftovers.
Bottom-up: runs of 1, 2, 4, …; ⌈log2n⌉ passes.
O(nlogn) in every case; stable; O(n) extra space for arrays.
Ideal for linked lists and for external sorting.
Practice questions
Answer on paper first, then check. The explanation matters more than the letter you chose.
Question 6 · short answer
Sort 38 27 43 3 9 82 10 with bottom-up 2-way merge sort, showing the runs after each pass.
MCA Online notes are made for reading on screen. Open https://mcastudy.online/mcs-208/searching-and-sorting-techniques/2-way-merge-sort/ to read this page.