- Notifications
You must be signed in to change notification settings - Fork0
selectlib is a lightweight C extension module for Python that implements several in‑place selection algorithms for efficiently finding the kth smallest element in an unsorted list.
License
grantjenks/python-selectlib
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
selectlib is a lightweight C extension module for Python that implements the quickselect algorithm. Quickselect is an efficient selection algorithm to find the kth smallest element in an unsorted list without fully sorting the data. The module supports an optional key function, allowing for flexible comparisons and custom ordering.
• Fast in-place selection using a C implementation• Optional key function for custom comparisons• Compatibility with Python 3.8 and later• Easily integratable into your Python projects
The selectlib module can be installed via pip after building from source. First, ensure you have a C compiler and Python development headers installed for your platform.
Clone the repository:
git clonehttps://github.com/grantjenks/python-selectlib.gitcd python-selectlib
Build and install:
python -m pip install -e .
Below is a simple example using quickselect to find the kth smallest element in a list:
import selectlib
data = [9, 3, 7, 1, 5, 8, 2]k = 3 # Find the element that would be at index 3 if sortedselectlib.quickselect(data, k)
print("The kth smallest element is:", data[k])
Using a key function to find the kth largest element:
data = [15, 8, 22, 5, 13]k = 2 # kth largest element when sorted in descending orderselectlib.quickselect(data, k, key=lambda x: -x)
print("The kth largest element is:", data[k])
You can run the automated tests using tox. The tests validate both correct partitioning and error cases.
tox
This will run tests on Python versions 3.8 through 3.13, as well as linting and formatting checks.
This project uses GitHub Actions for CI/CD. The following workflows are available:
• release.yml – Builds wheels for multiple platforms and publishes packages to PyPI• test.yml – Runs tests and linting on multiple Python versions
Contributions are welcome! If you find any bugs, have feature suggestions, or want to contribute improvements, feel free to open an issue or pull request on GitHub.
Documentation is provided within the source code and inline comments. For additional usage examples or API details, please refer to the source and test files.
selectlib is licensed under the Apache License, Version 2.0. See the LICENSE file for details.
About
selectlib is a lightweight C extension module for Python that implements several in‑place selection algorithms for efficiently finding the kth smallest element in an unsorted list.
Topics
Resources
License
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Uh oh!
There was an error while loading.Please reload this page.