# Merge Sort

> A sort that splits the list in halves, sorts them, then merges them back.

**Field:** Software Development  
**Category:** Algorithms & Data Structures  
**Also known as:** Mergesort

## Think of it like

Think of it like two piles of cards. Sort each pile, then join them in order.

## Definition

Merge sort splits a list in half, sorts each half, then merges the two halves. It keeps splitting until each piece has one item, which is already sorted. Merging two sorted lists is quick, because you only compare their front items. It always takes O(n log n) steps, even in the worst case. It needs extra memory the size of the list.

## Example

Lucy merge-sorts 8 numbers. They split into 8 single items, then merge into pairs, fours and one sorted list of 8.

## Why it matters

Its speed never gets bad, and it keeps equal items in their first order. Many built-in sorts use its ideas.

## Common mistake

Forgetting the extra memory. On a huge list, the copy it needs can be too big.

## Three principles

- **Split, sort, merge.** Again and again.
- **Always O(n log n).** No bad case.
- **Needs extra memory.** About the list's size.

## Related terms

- [Sorting Algorithm](https://learn.benibix.com/term/development/sorting-algorithm)
- [Quicksort](https://learn.benibix.com/term/development/quicksort)
- [Divide and Conquer](https://learn.benibix.com/term/development/divide-and-conquer)
- [Recursion](https://learn.benibix.com/term/development/recursion)

## Told apart from

- [Merge Sort vs Quicksort](https://learn.benibix.com/compare/merge-sort-vs-quicksort): Merge sort and quicksort are both fast, but they differ. Merge sort is always O(n log n) and needs extra memory. Quicksort sorts in place and is often faster, but bad pivots make it slow.

---

Source: https://learn.benibix.com/term/development/merge-sort
Author: Behnia Zarei (a developer in Vienna, Austria, who builds and teaches data, AI and business intelligence at BENIBIX)
Publisher: BENIBIX Learn (https://learn.benibix.com/)
Last updated: 2026-11-19
Licence: CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Quote freely with attribution.
