From 367307d8894f1e82d0762eea13f1c3da1aee0a23 Mon Sep 17 00:00:00 2001 From: ANANT JAIN <139585700+anant-jain01@users.noreply.github.com> Date: Fri, 11 Oct 2024 22:26:35 +0530 Subject: [PATCH 1/5] Create stalin_sort.py --- sorts/stalin_sort.py | 47 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 sorts/stalin_sort.py diff --git a/sorts/stalin_sort.py b/sorts/stalin_sort.py new file mode 100644 index 000000000000..66661bec61a6 --- /dev/null +++ b/sorts/stalin_sort.py @@ -0,0 +1,47 @@ +""" +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 +""" +def stalin_sort(sequence: list[int]) -> list[int]: + """ + Sorts a list using the Stalin sort algorithm. + + >>> 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] + """ + if any(x < 0 for x in sequence): + raise ValueError("Sequence must only contain non-negative integers") + + result = [sequence[0]] + for i in range(1, len(sequence)): + if sequence[i] >= result[-1]: + result.append(sequence[i]) + + return result + + +if __name__ == "__main__": + import doctest + doctest.testmod() From 96f74b15566270d32fba3b703d19c29b026cb420 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Fri, 11 Oct 2024 16:57:25 +0000 Subject: [PATCH 2/5] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- sorts/stalin_sort.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/sorts/stalin_sort.py b/sorts/stalin_sort.py index 66661bec61a6..1ad0c2175d21 100644 --- a/sorts/stalin_sort.py +++ b/sorts/stalin_sort.py @@ -3,6 +3,8 @@ Elements that are not greater than or equal to the previous element are discarded. Reference: https://medium.com/@kaweendra/the-ultimate-sorting-algorithm-6513d6968420 """ + + def stalin_sort(sequence: list[int]) -> list[int]: """ Sorts a list using the Stalin sort algorithm. @@ -44,4 +46,5 @@ def stalin_sort(sequence: list[int]) -> list[int]: if __name__ == "__main__": import doctest + doctest.testmod() From 0e046f03b2795e8755ee315fc388b6f63116ebae Mon Sep 17 00:00:00 2001 From: Maxim Smolskiy Date: Sun, 31 Aug 2025 01:14:21 +0300 Subject: [PATCH 3/5] Update stalin_sort.py --- sorts/stalin_sort.py | 3 --- 1 file changed, 3 deletions(-) diff --git a/sorts/stalin_sort.py b/sorts/stalin_sort.py index 1ad0c2175d21..48074aa55855 100644 --- a/sorts/stalin_sort.py +++ b/sorts/stalin_sort.py @@ -33,9 +33,6 @@ def stalin_sort(sequence: list[int]) -> list[int]: >>> stalin_sort([1, 2, 8, 7, 6]) [1, 2, 8] """ - if any(x < 0 for x in sequence): - raise ValueError("Sequence must only contain non-negative integers") - result = [sequence[0]] for i in range(1, len(sequence)): if sequence[i] >= result[-1]: From cfba11d0f5ce21a91e18ebd8110fe4b8b65d3b71 Mon Sep 17 00:00:00 2001 From: MaximSmolskiy Date: Sat, 30 Aug 2025 22:14:48 +0000 Subject: [PATCH 4/5] updating DIRECTORY.md --- DIRECTORY.md | 1 + 1 file changed, 1 insertion(+) diff --git a/DIRECTORY.md b/DIRECTORY.md index 2df9d56e99b7..36acb3b97f1e 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -1301,6 +1301,7 @@ * [Shell Sort](sorts/shell_sort.py) * [Shrink Shell Sort](sorts/shrink_shell_sort.py) * [Slowsort](sorts/slowsort.py) + * [Stalin Sort](sorts/stalin_sort.py) * [Stooge Sort](sorts/stooge_sort.py) * [Strand Sort](sorts/strand_sort.py) * [Tim Sort](sorts/tim_sort.py) From bd93c760732b2e5fccca50ca13e9b6eaed57ca03 Mon Sep 17 00:00:00 2001 From: Maxim Smolskiy Date: Sun, 31 Aug 2025 01:16:19 +0300 Subject: [PATCH 5/5] Update stalin_sort.py --- sorts/stalin_sort.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/sorts/stalin_sort.py b/sorts/stalin_sort.py index 48074aa55855..6dd5708c7f01 100644 --- a/sorts/stalin_sort.py +++ b/sorts/stalin_sort.py @@ -34,9 +34,9 @@ def stalin_sort(sequence: list[int]) -> list[int]: [1, 2, 8] """ result = [sequence[0]] - for i in range(1, len(sequence)): - if sequence[i] >= result[-1]: - result.append(sequence[i]) + for element in sequence[1:]: + if element >= result[-1]: + result.append(element) return result