Halloween DealsAmazon USHalloween week deals: useful picks before month-endAmazon US: current deals, useful picks and tech finds.Check DealsPC HealthRecommendedCrashes, freezes, slowdowns? Check your PC nowSpot repairable issues before they interrupt work.Check PCHalloween DealsAmazon USOctober picks are moving - compare before checkoutAmazon US: current deals, useful picks and tech finds.See Picks×
Skip to content
World desk3 min

How Merge Sort Uses Divide and Conquer to Run in O(n log n)

Merge sort divides an array, sorts each half, and merges the results in Θ(n log n) time. Learn why it is stable and why the standard array version needs Θ(n) extra memory.

Free tools Windows power users keep installed

One-click scans. No signup required.

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

Merge sort orders a list by splitting it into smaller parts, sorting those parts, and merging them back together. For the standard array implementation, it takes Θ(n log n) time when comparisons take constant time, uses Θ(n) extra memory, and is stable when equal items are taken from the left run first.

How merge sort works

The algorithm has two jobs: recursively produce sorted halves, then combine each pair of sorted halves into a larger sorted run. Splitting alone does not sort the items; the merge step is what puts them in order.

As an Amazon Associate I earn from qualifying purchases.

1. Divide the array

Split the array into two halves, continuing until each part has one item. A one-item run is already sorted. If the array length is odd, the halves can differ in size by one.

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

2. Sort each half

Apply the same process to each half. Once the smaller runs have been sorted, their results are ready to merge.

#1 Best Overall
Sale
Introduction to Algorithms, fourth edition
  • color: White
  • INTRODUCTION TO ALGORITHMS, FOURTH EDITION

3. Merge the sorted runs

Keep a pointer at the start of each run. Compare the pointed-to items, write the smaller one to the output, and advance that run’s pointer. Repeat until one run is empty, then copy the remaining items from the other run. Because each input run is already ordered, repeatedly choosing the smaller next item produces one ordered run.

For example, merging [2, 7] and [3, 6] proceeds by writing 2, then 3, then 6, then the remaining 7, producing [2, 3, 6, 7]. A merge examines each item at most as it advances through the runs, so its work is linear in the number of items being merged.

Why merge sort takes Θ(n log n) time

At each level of splitting, the runs together contain n items, and merging them takes Θ(n) work. Splitting in half creates about log₂ n levels before the runs reach one item. Multiplying Θ(n) work per level by Θ(log n) levels gives Θ(n log n) total time.

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

Equivalently, the recurrence is T(n) = 2T(n/2) + Θ(n): two recursive sorts handle half the items each, followed by a linear merge. This bound assumes constant-time comparisons. Princeton’s official Mergesort (Section 2.2) page describes the guarantee as time proportional to N log N regardless of input; NIST also lists merge sort’s runtime as Θ(n log n) in its merge sort reference.

Rank #3
Sale
Algorithm Design
  • Used Book in Good Condition

Is merge sort stable?

Yes, if the merge handles ties correctly. A stable sort keeps records with equal sort keys in the same relative order they had in the input. During a merge, when the next items from both runs compare equal, take the item from the left run first. That preserves the original order across the two runs. Princeton documents stability for its top-down Merge implementation and its bottom-up MergeBU implementation.

How much extra memory does it use?

The standard array implementation needs Θ(n) auxiliary storage for merging: it uses temporary space to combine the runs rather than rearranging everything solely within the original array. In its ordinary form, this is not an in-place array sort. Princeton’s documented top-down and bottom-up array implementations both specify Θ(n) extra memory.

The trade-off is predictable running time and stability at the cost of temporary storage and additional memory traffic while items are copied and merged.

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

Top-down and bottom-up merge sort

These are two ways to schedule the same basic merge work. The cited Princeton implementations have the same asymptotic time, stability, and auxiliary-memory bounds; neither is universally faster on that evidence.

Form How it proceeds Recursion Documented bounds
Top-down Splits the array recursively, then merges the sorted halves. Recursive Θ(n log n) time with constant-time comparisons; stable; Θ(n) extra memory.
Bottom-up Starts with one-item runs and merges neighboring runs in successively larger groups. Non-recursive Θ(n log n) time; stable; Θ(n) extra memory.

Choose the recursive form when its divide-and-conquer structure makes the implementation easier to follow. A bottom-up form avoids recursive calls, which can suit implementation requirements that favor iteration. The documented implementations do not establish a general speed advantage for either form.

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

What library implementations can change

The Θ(n log n) analysis describes the standard merge-sort approach, not every built-in sort. Libraries may use tuned or adaptive implementations, so check the documentation for the language and version in use.

For example, the Java SE 24 Arrays documentation describes its object-array implementation as a stable, adaptive, iterative mergesort. It notes that nearly sorted input can require approximately n comparisons, while temporary storage varies with the input. This is a version-specific implementation note, not a claim about every Java version, primitive-array sort, or other language’s built-in sorting method.

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

Further reading

For a broader treatment of sorting and algorithms, Princeton’s Algorithms, 4th Edition booksite identifies sorting, including mergesort, in Chapter 2 and provides associated online materials.

Quick Recap

SaleBestseller No. 1
Introduction to Algorithms, fourth edition
Introduction to Algorithms, fourth edition
color: White; INTRODUCTION TO ALGORITHMS, FOURTH EDITION
$99.47
SaleBestseller No. 2
SaleBestseller No. 3
Algorithm Design
Algorithm Design
Used Book in Good Condition
$223.83
SaleBestseller No. 4

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.

Leave a Reply

Your email address will not be published. Required fields are marked *

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

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Windows Errors? Fix Them Before They SpreadFree repair scan
Crashes, No Sound, or Screen Glitches?Free driver scan

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.