Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit9d52683

Browse files
anant-jain01pre-commit-ci[bot]MaximSmolskiy
authored
Create stalin_sort.py (#11989)
* Create stalin_sort.py* [pre-commit.ci] auto fixes from pre-commit.com hooksfor more information, seehttps://pre-commit.ci* Update stalin_sort.py* updating DIRECTORY.md* Update stalin_sort.py---------Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>Co-authored-by: Maxim Smolskiy <mithridatus@mail.ru>Co-authored-by: MaximSmolskiy <MaximSmolskiy@users.noreply.github.com>
1 parente3a263c commit9d52683

File tree

2 files changed

+48
-0
lines changed

2 files changed

+48
-0
lines changed

‎DIRECTORY.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1301,6 +1301,7 @@
13011301
*[Shell Sort](sorts/shell_sort.py)
13021302
*[Shrink Shell Sort](sorts/shrink_shell_sort.py)
13031303
*[Slowsort](sorts/slowsort.py)
1304+
*[Stalin Sort](sorts/stalin_sort.py)
13041305
*[Stooge Sort](sorts/stooge_sort.py)
13051306
*[Strand Sort](sorts/strand_sort.py)
13061307
*[Tim Sort](sorts/tim_sort.py)

‎sorts/stalin_sort.py‎

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
"""
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
5+
"""
6+
7+
8+
defstalin_sort(sequence:list[int])->list[int]:
9+
"""
10+
Sorts a list using the Stalin sort algorithm.
11+
12+
>>> stalin_sort([4, 3, 5, 2, 1, 7])
13+
[4, 5, 7]
14+
15+
>>> stalin_sort([1, 2, 3, 4])
16+
[1, 2, 3, 4]
17+
18+
>>> stalin_sort([4, 5, 5, 2, 3])
19+
[4, 5, 5]
20+
21+
>>> stalin_sort([6, 11, 12, 4, 1, 5])
22+
[6, 11, 12]
23+
24+
>>> stalin_sort([5, 0, 4, 3])
25+
[5]
26+
27+
>>> stalin_sort([5, 4, 3, 2, 1])
28+
[5]
29+
30+
>>> stalin_sort([1, 2, 3, 4, 5])
31+
[1, 2, 3, 4, 5]
32+
33+
>>> stalin_sort([1, 2, 8, 7, 6])
34+
[1, 2, 8]
35+
"""
36+
result= [sequence[0]]
37+
forelementinsequence[1:]:
38+
ifelement>=result[-1]:
39+
result.append(element)
40+
41+
returnresult
42+
43+
44+
if__name__=="__main__":
45+
importdoctest
46+
47+
doctest.testmod()

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp