Skip to content
IT glossary
AlgorithmsUpdated December 3, 2026

Merge sort

Сортировка слиянием

This sorting algorithm splits a collection into smaller pieces, sorts those pieces, and combines them into one ordered sequence.

Start with two sorted runs

Merge sort is easiest to understand by looking at the last step first. Suppose you have two lists in ascending order and want one sorted list. The smallest remaining item must be at the front of one of the two lists. Compare those two items, append the smaller one to the output, and advance only the position you took it from. Repeat until one list runs out, then append the rest of the other list. This takes linear time in the combined length of the inputs.

For example, merging [3, 8] and [1, 5, 9] picks 1 from the right, 3 from the left, 5 from the right, and 8 from the left. The left run is now empty, so 9 goes straight onto the end. If the input runs weren't already sorted, comparing their first remaining items wouldn't be enough to find the next item.

Where the sorted runs come from

In the top-down version, split the input into two nearly equal halves and recursively sort each half before merging them. A range with zero or one item is already sorted, so the recursion stops there. This is the divide-and-conquer pattern: solve smaller instances of the same problem, then combine their answers. A bottom-up implementation skips recursion by treating each item as a sorted run and merging adjacent runs in passes of increasing size.

If two records have the same sort key, take the record from the left run first. That preserves their original relative order, which is what makes this version stable. For example, when sorting a list of support tickets by priority, tickets with the same priority keep their arrival order if the original list was ordered by arrival.

Cost and practical uses

There are Θ(log n) merge levels, and at each level the combined lengths of the ranges being merged add up to Θ(n). The standard implementation that always performs the merges therefore takes Θ(n log n) time even on sorted input. For arrays, a typical implementation allocates one O(n) scratch buffer and reuses it. Top-down recursion also uses O(log n) stack space; the iterative version avoids that stack cost, not the buffer.

Sequential reads make the merge step useful for external sorting. When a file is too large to fit in memory, you can sort chunks that do fit, save those sorted runs to disk, and merge them while reading each run in blocks. Merge sort also suits linked lists: the merge can relink existing nodes instead of copying values into an array-sized buffer.

What to expect in an interview

You might be asked to write just the merge step or to build the full sort around it. State that both inputs to the merge are sorted and decide how to break ties before you code. Walk through an empty range, an odd-length array, and a case where one half runs out early. For a stability test, use distinct records with equal keys: identical integers won't show whether their relative order changed.