Interview kitsBlog

Your dream job? Lets Git IT.
Interactive technical interview preparation platform designed for modern developers.

XGitHub

Platform

  • Categories

Resources

  • Blog
  • About the app
  • FAQ
  • Feedback

Legal

  • Privacy Policy
  • Terms of Service

© 2026 LetsGit.IT. All rights reserved.

LetsGit.IT/Categories/Algorithms
Algorithmsmedium

What does it mean that a sort is stable, and why does it matter?

Tags
#sorting#stable-sort#algorithm
Back to categoryPractice quiz

Answer

A stable sort keeps the relative order of elements with equal keys. It matters when you sort by multiple fields (e.g., sort by last name, then by first name).

Advanced answer

Deep dive

A sort is *stable* if elements with equal sort keys keep their original relative order. Stability doesn’t change whether the final sequence is correctly ordered by the key; it changes what happens inside ties.

Why it matters

  • Multi-key sorting: sort by secondary key first, then sort by primary key with a stable sort.
  • UX: when many items tie, stability avoids “jumping” rows between refreshes.

Example

Input: (dept=A, name=Z), (dept=A, name=B)
Sort by dept only:
- Stable => Z stays before B (same as input)
- Unstable => the two may swap

Common pitfalls

  • Assuming your language’s default sort is stable without checking docs.
  • Relying on stability to simulate multi-key sorting, then switching to an unstable algorithm (e.g., some QuickSort variants).

Related questions

Algorithms
Heap sort: what are its time complexity, space complexity, and stability?
#heapsort#sorting#complexity
Algorithms
Counting sort: when can it be faster than O(n log n) sorting?
#counting-sort#sorting#stability
Algorithms
What is Dijkstra's Algorithm?
#graph
#shortest-path
#dijkstra
Algorithms
QuickSort vs MergeSort?
#sorting#quicksort#mergesort
Algorithms
Explain Binary Search.
#search#binary-search#algorithm