Sort Stability and the Comparison Lower Bound
Check whether equal keys retain their order, then consider the worst-case limit of comparison sorting.
Core Intuition & How It Works
A sort is stable if equal keys keep their original relative order—vital for multi-key sorting. Decision-tree analysis proves that any comparison-based sort requires at least Ω(n log n) comparisons in the worst case because n! permutations must be distinguished.
In the Byte Sized Algorithms interactive classroom, you can step forward and backward through this algorithm with your keyboard while watching the visual diagram, memory state, and highlighted pseudocode update together.
Time & Space Complexity
| Metric | Complexity |
|---|---|
| Time Complexity | Ω(n log n) comparison lower bound |
| Space Complexity | Depends on algorithm |
| Topic Chapter | Chapter 9: Sorting & Selection |
| Difficulty Level | Intermediate |
Key Pattern to Recognize
Verify stability whenever sorting records by a secondary field followed by a primary field (e.g., sort by date, then by category).
Common Pitfalls & Edge Cases
Assuming an unstable sort can be made stable just by tweaking comparisons without adding original indices as tie-breakers.
Real-World & Technical Interview Applications
Spreadsheet multi-column sorting, database ORDER BY plans, and choosing between comparison and radix sorts.