Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

iTechGuides is reader-supported. When you buy through links on our site, we may earn an affiliate commission. As an Amazon Associate I earn from qualifying purchases. Learn more

Merge sort orders an array by splitting it into smaller parts, sorting those parts, and merging them back together. Its standard array implementation takes Θ(n log n) time when each comparison takes constant time, and uses Θ(n) extra memory. It is stable when equal keys are taken from the left run first during merging.

How merge sort works

Merge sort follows three steps: divide the array, sort each part, then merge the sorted parts. The divide-and-conquer process continues until each part contains one item; a one-item run is already sorted.

1. Divide the array

Split the array into two roughly equal halves. Repeat the split on each half until the subarrays are small enough to be trivially sorted.

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

2. Sort each half

Apply the same process recursively to both halves. Each recursive call returns an ordered run.

3. Merge the ordered runs

Compare the first unmerged item in each run, copy the smaller item into the output, and advance in that run. Continue until one run is exhausted, then copy the remaining items from the other run. The result is one sorted run.

For example, merging [2, 6, 9] and [1, 6, 8] proceeds by selecting 1, 2, 6, then 6, followed by 8 and 9. The merge scans the runs and writes each item once, so its work is linear in the total number of items being merged.

Why merge sort takes Θ(n log n) time

At each level of recursion, the merge work across all subarrays totals Θ(n): together, those merges process all n items. Dividing the input in half creates Θ(log n) levels. Thus the standard recurrence is T(n) = 2T(n/2) + Θ(n), which yields Θ(n log n) time when comparisons take constant time.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Princeton’s official Mergesort booksite says its algorithm guarantees to sort N items in time proportional to N log N, regardless of the input. NIST also lists merge sort’s runtime as Θ(n log n) in its algorithm reference.

Rank #3
Sale
Cracking the Coding Interview: 189 Programming Questions and Solutions
  • Careercup, Easy To Read
  • Condition : Good
  • Compact for travelling

Is merge sort stable?

Yes, the standard merge operation can preserve stability. A stable sort keeps records with equal sort keys in their original relative order. To do that, when the next items in the left and right runs compare equal, take the item from the left run first. Princeton’s Merge implementation documentation describes its algorithm as stable.

How much extra space does merge sort use?

The ordinary array implementation needs Θ(n) auxiliary storage for merging. It is therefore not an in-place array sort in its standard form. The additional storage is the main trade-off for its predictable Θ(n log n) running time and stability.

Top-down recursive and bottom-up iterative merge sort

Top-down merge sort divides recursively; bottom-up merge sort starts with one-item runs and repeatedly merges adjacent runs into larger sorted runs. Princeton documents both approaches with Θ(n log n) time, stability, and Θ(n) extra memory, while its bottom-up implementation is explicitly non-recursive.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Version How it builds sorted runs Recursion Time and extra memory in Princeton’s implementation
Top-down Splits the array into halves, then merges sorted halves. Recursive Θ(n log n) time; Θ(n) extra memory.
Bottom-up Repeatedly merges neighboring runs, growing their size each pass. Non-recursive Θ(n log n) time; Θ(n) extra memory.

The choice is an implementation decision, not a universal speed ranking. Recursion may make the divide-and-conquer structure easier to follow; an iterative version avoids recursive calls. The cited implementations share the same asymptotic bounds.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

What a library’s merge sort behavior tells you

Algorithm names do not establish how every built-in sort works. For example, Oracle’s Java SE 24 Arrays documentation describes its object-array implementation as a stable, adaptive, iterative mergesort. For nearly sorted input, it can use approximately n comparisons; temporary storage varies with the input. This is a version-specific implementation note, not a description of every Java version or every language’s built-in sorting method.

When merge sort is a useful choice

  • Choose it when a predictable Θ(n log n) time bound matters, including for input that may already be ordered differently.
  • Choose a stable implementation when equal-key records must retain their original order.
  • Account for Θ(n) auxiliary memory when sorting arrays with the standard implementation.
  • Check the documentation for the exact language and runtime version if you are relying on a built-in sort’s algorithm, stability, or behavior on nearly sorted input.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.