Stability
A sorter is stable if equal keys maintain their relative order. Insertion Sort is stable; Selection Sort is typically not (depending on the implementation).
Stability matters when additional data is attached to keys.
Where used
Multilevel sorting (first by name, then by grade) and UI tables: stable sorting preserves the previous order. Collections.sort / TimSort are stable; classic Quicksort is typically not.
Depth
A sort is stable if records with the same sort key appear in the same mutual sequence afterward as they did before. This is relevant when multiple sorting steps are combined, such as sorting by first name and then by last name.
Stability is a property of the concrete implementation, not just the algorithm name. Insertion Sort can be stable by only moving strictly larger elements. In contrast, Selection Sort with a wide swap can move an equivalent record over another.
Difficulty levels
- Check stability on records with a key and additional identifier.
- Make a comparison condition stable or unstable.
- Plan multilevel sorts in the correct order.
Pitfalls
Stability cannot be observed with arrays containing entirely different values. A check requires equal keys with distinguishable accompanying data; otherwise, a loss of order remains invisible.
Tasks
Card Info
- Topic: Algorithms and Data Structures
- Difficulty: Beginner
- Completed: 0 users