diff --git a/sorts/stalin_sort.py b/sorts/stalin_sort.py index 66eea8d4f99e..2c3ec7c743f5 100644 --- a/sorts/stalin_sort.py +++ b/sorts/stalin_sort.py @@ -1,43 +1,61 @@ """ -Stalin Sort algorithm: Removes elements that are out of order. -Elements that are not greater than or equal to the previous element are discarded. -Reference: https://medium.com/@kaweendra/the-ultimate-sorting-algorithm-6513d6968420 +A pure Python implementation of the Stalin sort algorithm. + +For doctests run the following command: + python3 -m doctest -v stalin_sort.py + +For manual testing run: + python3 stalin_sort.py """ +from typing import Any, Protocol -def stalin_sort(sequence: list[int]) -> list[int]: - """ - Sorts a list using the Stalin sort algorithm. +class Comparable(Protocol): + def __ge__(self, other: Any, /) -> bool: ... + + +def stalin_sort[T: Comparable](sequence: list[T]) -> list[T]: + """Sort a list of mutually comparable items using the Stalin sort algorithm. + + Iterates through the sequence and retains elements that are greater than + or equal to the last retained element, discarding any out-of-order items. + + Reference: https://medium.com/@kaweendra/the-ultimate-sorting-algorithm-6513d6968420 + + Complexity Analysis: + - Time Complexity: O(n) where n is the number of elements in the list. + - Space Complexity: O(n) auxiliary space for the output list. + + Examples: >>> stalin_sort([4, 3, 5, 2, 1, 7]) [4, 5, 7] - >>> stalin_sort([1, 2, 3, 4]) [1, 2, 3, 4] - >>> stalin_sort([4, 5, 5, 2, 3]) [4, 5, 5] - >>> stalin_sort([6, 11, 12, 4, 1, 5]) [6, 11, 12] - >>> stalin_sort([5, 0, 4, 3]) [5] - >>> stalin_sort([5, 4, 3, 2, 1]) [5] - >>> stalin_sort([1, 2, 3, 4, 5]) [1, 2, 3, 4, 5] - >>> stalin_sort([1, 2, 8, 7, 6]) [1, 2, 8] - >>> stalin_sort([]) [] - >>> stalin_sort([7]) [7] + >>> stalin_sort([2.5, -1.0, 0.0, 3.2]) + [2.5, 3.2] + >>> stalin_sort(["d", "a", "e", "c", "f"]) + ['d', 'e', 'f'] + >>> stalin_sort([1, "a"]) + Traceback (most recent call last): + ... + TypeError: '>=' not supported between instances of 'str' and 'int' """ if not sequence: return [] diff --git a/tests/test_sorts.py b/tests/test_sorts.py index 5cfbacf30e6c..303f6213ec8b 100644 --- a/tests/test_sorts.py +++ b/tests/test_sorts.py @@ -244,3 +244,13 @@ def test_bitonic_sort_comparable_items() -> None: with pytest.raises(TypeError): bitonic_sort([1, "two", 3, "four"], 0, 4, 1) + + +def test_stalin_sort_comparable_items() -> None: + from sorts.stalin_sort import stalin_sort + + assert stalin_sort(["d", "a", "e", "c", "f"]) == ["d", "e", "f"] + assert stalin_sort([2.5, -1.0, 0.0, 3.2]) == [2.5, 3.2] + + with pytest.raises(TypeError): + stalin_sort([1, "a"])