01 · Question
Given a deck of cards, sort it by card value while preserving the original relative order of cards with equal values.
Example:
- Input:
[(9, clubs), (4, spades), (9, spades), (4, clubs)] - Output:
[(4, spades), (4, clubs), (9, clubs), (9, spades)]
02 · Analysis
This is exactly the guarantee of a stable sort. Sort only by value; equal values retain their original order.
Python's built-in sort is stable. In Go, use sort.SliceStable. If a language offers no stable sort, attach the original index as the final tie-breaker.