
This lesson is part of the Merge Sort series. Standard merge sort divides the input into halves even when some values are already in order. Natural merge sort notices those ordered sections and uses them as its starting runs.

Follow the animation step by step
The animation starts with [2, 4, 7, 1, 3, 0, 5, 6, 9]. Natural merge sort scans left to right and starts a new run whenever the next value is smaller than the one before it.
- Scan and mark the drops: 7 is followed by 1, and 3 is followed by 0. The ascending runs are therefore [2, 4, 7], [1, 3], and [0, 5, 6, 9].
- Start merging neighboring runs: merge [2, 4, 7] with [1, 3]. Compare the front values and write 1, then 2. The GIF shows this growing output as [1, 2].
- Complete that merge: continue with 3, 4, and 7 to get [1, 2, 3, 4, 7]. Carry [0, 5, 6, 9] forward as the final unpaired run.
- Merge the two longer runs: compare [1, 2, 3, 4, 7] with [0, 5, 6, 9]. Take 0 first, then 1, 2, 3, 4, 5, 6, 7, and 9. The result is [0, 1, 2, 3, 4, 5, 6, 7, 9].
A run is a contiguous ascending stretch. The animation starts a new run at each drop, then merges neighboring runs until one remains.

What is a natural run?
A natural run is a consecutive part of the input that is already ordered. For example:
Input: [1, 4, 7] [2, 3, 8] [0, 5, 9]
Runs: [1, 4, 7] | [2, 3, 8] | [0, 5, 9]The vertical bars show where one ascending run ends and the next begins. Merge neighboring runs, then repeat until one run remains.
Some implementations also detect descending runs. A strictly descending run can be reversed safely. If equal keys occur, reversing a non-increasing run can change their order, so a stable implementation must handle equal values carefully.
Why it can help
If the input already contains long ordered stretches, natural merge sort may need fewer merge passes than a method that begins with single-item runs. For random input, runs are usually short, and the method may behave much like ordinary merge sort. Its worst-case time remains O(n log n) when runs are merged in a balanced schedule.
Run detection adds a scan, but that scan is linear. The merge work still determines the overall cost in the worst case. The exact behavior depends on the run selection and merge schedule, so “natural” does not automatically mean faster for every input.
A simple outline
naturalMergeSort(values):
runs = findAscendingRuns(values)
while runs contains more than one run:
nextRuns = []
merge adjacent run pairs into nextRuns
carry a final unpaired run forward
runs = nextRunsFor stable sorting, keep equal values in their original relative order during both run detection and merging. If the input has one run, it is already sorted and the algorithm can finish after a single scan.
Natural merge sort is useful when data often arrives partly ordered, such as records appended in time order. It keeps the merge-sort approach while using existing structure in the input.


