LOGBOOK

HELP

Quiz Entry - updated: 2026.12.02

What does it mean that a sort is stable, and which detail of the merge decides whether merge-sort is stable?

Stable means that elements with equal keys keep their input order. Merge-sort is stable only if the merge breaks ties in favour of the left half: take from $A$ when $A$'s front $\le$ $B$'s front. With a strict $<$ the right half wins every tie and equal keys swap places.

Merging the halves of 2a 5 2b 1: with a strict less-than 2b ends up before 2a, with less-or-equal the input order is kept

* The left half always holds the elements that came first in the input, so preferring it on ties preserves their order. *

Stability only matters when equal keys belong to distinguishable records. Typical case: a list of people already sorted by name is sorted again by city. A stable sort keeps the people of each city in name order, an unstable one may shuffle them. Sorting by several keys one after the other, least important key first, works only with a stable sort.

Merge-sort keeps the input order everywhere except possibly at ties: every element of the left half $A$ came before every element of the right half $B$. If the merge takes from $A$ on a tie, equal keys come out in input order. Many textbook versions write the test as A.first() < B.first(); then a tie sends $B$'s element first, and 2a 5 2b 1 comes out as 1 2b 2a 5. One character decides it.

Go deeper:

From Quiz: ADS / Merge-Sort and Quick-Sort | Updated: Dec 02, 2026