Skip to content

Commit 3f7a296

Browse files
committed
Type stalin sort for any comparable items
Part of #15234 - Update stalin_sort to support any comparable items using Comparable protocol - Add doctests for floats, strings, and TypeError handling - Add test_stalin_sort_comparable_items in tests/test_sorts.py
1 parent 215c1f0 commit 3f7a296

2 files changed

Lines changed: 43 additions & 15 deletions

File tree

‎sorts/stalin_sort.py‎

Lines changed: 33 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,61 @@
11
"""
2-
Stalin Sort algorithm: Removes elements that are out of order.
3-
Elements that are not greater than or equal to the previous element are discarded.
4-
Reference: https://medium.com/@kaweendra/the-ultimate-sorting-algorithm-6513d6968420
2+
A pure Python implementation of the Stalin sort algorithm.
3+
4+
For doctests run the following command:
5+
python3 -m doctest -v stalin_sort.py
6+
7+
For manual testing run:
8+
python3 stalin_sort.py
59
"""
610

11+
from typing import Any, Protocol
712

8-
def stalin_sort(sequence: list[int]) -> list[int]:
9-
"""
10-
Sorts a list using the Stalin sort algorithm.
1113

14+
class Comparable(Protocol):
15+
def __ge__(self, other: Any, /) -> bool: ...
16+
17+
18+
def stalin_sort[T: Comparable](sequence: list[T]) -> list[T]:
19+
"""Sort a list of mutually comparable items using the Stalin sort algorithm.
20+
21+
Iterates through the sequence and retains elements that are greater than
22+
or equal to the last retained element, discarding any out-of-order items.
23+
24+
Reference: https://medium.com/@kaweendra/the-ultimate-sorting-algorithm-6513d6968420
25+
26+
Complexity Analysis:
27+
- Time Complexity: O(n) where n is the number of elements in the list.
28+
- Space Complexity: O(n) auxiliary space for the output list.
29+
30+
Examples:
1231
>>> stalin_sort([4, 3, 5, 2, 1, 7])
1332
[4, 5, 7]
14-
1533
>>> stalin_sort([1, 2, 3, 4])
1634
[1, 2, 3, 4]
17-
1835
>>> stalin_sort([4, 5, 5, 2, 3])
1936
[4, 5, 5]
20-
2137
>>> stalin_sort([6, 11, 12, 4, 1, 5])
2238
[6, 11, 12]
23-
2439
>>> stalin_sort([5, 0, 4, 3])
2540
[5]
26-
2741
>>> stalin_sort([5, 4, 3, 2, 1])
2842
[5]
29-
3043
>>> stalin_sort([1, 2, 3, 4, 5])
3144
[1, 2, 3, 4, 5]
32-
3345
>>> stalin_sort([1, 2, 8, 7, 6])
3446
[1, 2, 8]
35-
3647
>>> stalin_sort([])
3748
[]
38-
3949
>>> stalin_sort([7])
4050
[7]
51+
>>> stalin_sort([2.5, -1.0, 0.0, 3.2])
52+
[2.5, 3.2]
53+
>>> stalin_sort(["d", "a", "e", "c", "f"])
54+
['d', 'e', 'f']
55+
>>> stalin_sort([1, "a"])
56+
Traceback (most recent call last):
57+
...
58+
TypeError: '>=' not supported between instances of 'str' and 'int'
4159
"""
4260
if not sequence:
4361
return []

‎tests/test_sorts.py‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,3 +244,13 @@ def test_bitonic_sort_comparable_items() -> None:
244244

245245
with pytest.raises(TypeError):
246246
bitonic_sort([1, "two", 3, "four"], 0, 4, 1)
247+
248+
249+
def test_stalin_sort_comparable_items() -> None:
250+
from sorts.stalin_sort import stalin_sort
251+
252+
assert stalin_sort(["d", "a", "e", "c", "f"]) == ["d", "e", "f"]
253+
assert stalin_sort([2.5, -1.0, 0.0, 3.2]) == [2.5, 3.2]
254+
255+
with pytest.raises(TypeError):
256+
stalin_sort([1, "a"])

0 commit comments

Comments
 (0)