This project is a Python implementation of Stalin Sort, a meme sorting algorithm. It is probably the most efficient "sorting" algorithm with a time complexity of
Here’s what it does:
- You start at the beginning of a list.
- Walk through the elements one by one.
- If the current element is greater than or equal to the last “kept” element, you keep it.
- If the element is smaller, you simply discard it (like it never existed).
- At the end, the result is a non-decreasing subsequence of the input list.
In other words, instead of rearranging elements into sorted order, it just discards the ones that are "out of order."
Credit goes to this repo that collects implementations in various programming languages. My goal here was to create a complete, typed, and installable Python package for Stalin Sort.
pip install stalin-sort-pythonfrom stalin_sort import stalin_sort
# Basic usage: non-strict, increasing order
print(list(stalin_sort([7, 7, 4, 2, 7])))
# [7, 7, 7]
# Strictly increasing, deduplication of elements
print(list(stalin_sort([3, 3, 2, 7], strict=True)))
# [3, 7]
# Decreasing order
print(list(stalin_sort([4, 2, 7, 3, 0], decreasing=True)))
# [4, 2, 0]
# With a key function
print(list(stalin_sort(["sort", "this", "out", "immediately"], key=len)))
# ['sort', 'this', 'immediately']- ✅ Typed
- ✅
strictand non-strict order - ✅
decreasing=Truesequences - ✅ Custom
keyfunctions (likelen) - ✅ Lazy evaluation
- ✅ Works with any iterable (lists, tuples, sets, strings, generators, ...)
- ✅ Works on any type supporting the
<operator
-
Time Complexity:
$\mathcal{O}(n)$ single pass through the data. -
Space Complexity:
$\mathcal{O}(1)$ additional space. It only stores the key of the last accepted element. - Laziness: Implemented as a generator, great for streams and large iterables.
Clone the repo and install in editable moode:
git clone https://github.com/hofjak/stalin-sort
cd stalin-sort
pip install -e .[dev]pytest
# Or
python -m pytest .\tests\⬆️ Back to top
GitHub @hofjak · Email jakob.refoh@gmail.com