Skip to content

Commit 58e9ab5

Browse files
committed
test: cover insertion sort stability with equal-priority items
1 parent 84b73d0 commit 58e9ab5

1 file changed

Lines changed: 11 additions & 0 deletions

File tree

‎sorts/insertion_sort.py‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,17 @@ def insertion_sort[T: Comparable](collection: MutableSequence[T]) -> MutableSequ
5656
True
5757
>>> values
5858
[1, 2, 4, 7]
59+
60+
Equal-priority items retain their original relative order:
61+
>>> from dataclasses import dataclass, field
62+
>>> @dataclass(order=True)
63+
... class Item:
64+
... priority: int
65+
... label: str = field(compare=False)
66+
>>> items = [Item(2, "A"), Item(1, "X"), Item(2, "B"), Item(1, "Y")]
67+
>>> [(item.priority, item.label) for item in insertion_sort(items)]
68+
[(1, 'X'), (1, 'Y'), (2, 'A'), (2, 'B')]
69+
5970
>>> import random
6071
>>> collection = random.sample(range(-50, 50), 100)
6172
>>> insertion_sort(collection) == sorted(collection)

0 commit comments

Comments
 (0)