A stable sort preserves the relative order of elements that compare equal, if two records have the same key, the one that appeared first in the input stays first in the output. It matters for multi-key sorting: to sort records by department and, within each department, by name, you sort by name first (stable), then by department (stable), and stability guarantees the name ordering survives within each department's group. Without stability, the second sort can scramble the equal-department elements, losing the name order. Insertion sort, bubble sort, and mergesort are stable; selection sort, quicksort, and heapsort are not (without extra tie-breaking work, like appending the original index to the key). So if your sort key has ties whose original order is meaningful, you need a stable algorithm or you must make the key unique.
Data Structures & Algorithms · Interview question
What does it mean for a sort to be stable, and when does it matter?
A strong answer
What a weak answer sounds like
You know the answer. Do you know what gets you dinged?
Pro breaks down the answer most candidates actually give to this question — and the specific reason an interviewer marks it down. It’s the difference between sounding correct and sounding senior, on all 472 questions.
From the lesson
Sorting Basics
The O(n²) simple sorts vs the O(n log n) fast ones, and why on embedded the simple, in-place insertion sort often beats the asymptotically-better choice at small n.