A stable sort preserves the order of tied records

← Back to Blog & Articles

A stable sorting algorithm preserves the original relative order of records whose sort keys compare equal. This matters when records carry information beyond the key used for the current ordering.

Worked example

Suppose an event list is already ordered by arrival time. Sorting it stably by priority places the high-priority events first while keeping arrival order among events of equal priority. A second approach uses a compound key containing priority and arrival time. Both approaches can express the intended order, but a stable sort lets an earlier ordering remain meaningful.

What to check

Check ties explicitly with records that have different identities and equal keys. A numerically sorted output alone cannot demonstrate stability. Also specify whether earlier or later arrivals should win. Stability concerns the order of ties, while correctness of the primary sort concerns the order of unequal keys. Treat them as separate requirements when selecting or testing an implementation.

Continue with the book

Explore the full treatment, worked examples, and exercises in Algorithms Made Simple.


Companion article for Algorithms Made Simple, published by Choral Software. Article text and original examples © 2026 Choral Software. All rights reserved.

Scroll to Top