Sobes.tech
Senior

What is the difference between stable and unstable sorting algorithms?

sobes.tech AI

Answer from AI

Stable sorting preserves the relative order of elements with equal keys (values) from the original array. That is, if two elements are equal by key, they will appear in the same order after sorting as they did before.

Unstable sorting does not guarantee this order — elements with equal keys may change places.

Example: if you have a list of objects with fields "name" and "age", and you sort by age, stable sorting will preserve the order of names for the same age, while unstable sorting will not.

In Go, the standard library provides both stable (e.g., sort.SliceStable) and unstable (sort.Slice) sorting methods.

What is the difference between stable and unstable… - sobes.tech