What does it mean that a sorting algorithm is stable?

Items with equal keys keep their original order.

Start playing free

  1. It never crashes on large lists
  2. It always takes the same time
  3. Items with equal keys keep their original order ✔

Why: Say you sort students by grade. A stable sort keeps students with the same grade in the order they were already in, such as alphabetical. That lets you sort by one field, then another, and keep both orders.

Huh, didn't know that: Python's sorted() and list.sort() are guaranteed stable and use Timsort, which takes advantage of order already in the data.

The dad joke
My sorting algorithm is very stable. It has a good job and a nice house.
Like this card?
Play the game

References

Answer: Items with equal keys keep their original order. Say you sort students by grade. A stable sort keeps students with the same grade in the order they were already in, such as alphabetical. That lets you sort by one field, then another, and keep both orders.

  1. Python docs: Sorting Techniques docs.python.org/3/howto/sorting.html
    when multiple records have the same key, their original order is preserved
    Checked 2026-10-11.

Think this answer is wrong? Tap "Challenge this answer" on the card in the game and tell us why.

Test yourself in the game