Lesson 53 of 86IntermediatePro Interactive Lesson

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

MetricComplexity
Time ComplexityΩ(n log n) comparison lower bound
Space ComplexityDepends on algorithm
Topic ChapterChapter 9: Sorting & Selection
Difficulty LevelIntermediate

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.

Unlock the full course

All 86 lessons across 15 chapters

View pricing

Delete account

This permanently deletes your account and ends any Pro access.