A typed 'Stalin Sort' implementation with strict/decreasing/key options.
Project description
Stalin Sort
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 $\mathcal{O}(n)$.
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.
Table of Contents
Installation
pip install stalin-sort-python
Usage
from 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']
Features
- ✅ 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
Performance
- 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.
Development
Clone the repo and install in editable moode:
git clone https://github.com/hofjak/stalin-sort
cd stalin-sort
pip install -e .[dev]
Run Tests
pytest
# Or
python -m pytest .\tests\
License
⬆️ Back to top
GitHub @hofjak · Email jakob.refoh@gmail.com
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file stalin_sort_python-1.0.1.tar.gz.
File metadata
- Download URL: stalin_sort_python-1.0.1.tar.gz
- Upload date:
- Size: 7.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.11.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
fe5e92f5a293950cecbfdf6467d8202634288700bc1a5eefb8ad15b727b12032
|
|
| MD5 |
f3874740eb3a35da89e29b6b908f543c
|
|
| BLAKE2b-256 |
eff7e4ed41c60d4e9f5b2a5bdac2dd4fe1d56b4fc0f345a20ff0d5115039df22
|
File details
Details for the file stalin_sort_python-1.0.1-py3-none-any.whl.
File metadata
- Download URL: stalin_sort_python-1.0.1-py3-none-any.whl
- Upload date:
- Size: 5.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.11.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f3a8f9cba7bc1844c724588a4d085cdb57cec312c4e9c99b28878406861d5f2c
|
|
| MD5 |
d2779673ebf452384c36a14f8ed3d14e
|
|
| BLAKE2b-256 |
66b12b6f25f6b6cc9d9af1eda42ca2c13c2261d7c5051aa9070d701497cb9b9b
|