Skip to solution
easyDSA

What is a stable sorting algorithm?

485 views
01

Understand the problem

Explain sort stability.

stabilitysorting
02

Attempt it yourself

Sketch your approach before reading the solution — that's what interviews test.

Nudge consolestandby

Stuck? Beam a request up — the console returns a conceptual nudge that guides your logic without spoiling the implementation.

03

Study the solution

A stable sort preserves the relative order of elements with equal keys. This matters when sorting by multiple criteria in passes (e.g. sort by name, then by date keeps same-date items in name order). Merge sort and insertion sort are stable; standard quicksort and heapsort are not.

Solution ready — 2 min read

Classified // press E to declassify

04

Read the code

Stable multi-key sort
Run Playground
people = [("Bob", 30), ("Ann", 25), ("Cy", 30), ("Dan", 25)]

# sort by age; Timsort is stable, so equal ages keep input order
by_age = sorted(people, key=lambda p: p[1])

# --- demo --- Ann before Dan, Bob before Cy (input order preserved)
print(by_age)   # [('Ann', 25), ('Dan', 25), ('Bob', 30), ('Cy', 30)]
05

Join the discussion

Discussion (0)

Sign in to join the discussion.

No responses yet. Be the first to share what you think.

Transmission complete // awaiting log

KEEP THE
STREAK ALIVE.

Dossier 17 of 127 decoded in the Data Structures & Algorithms track. One more won't hurt.

Back to track