Fast median finding
Project description
BFPRT
Median of Medians Quickselect Algorithm
About
This package implements fast median finding with the median of medians selection algorithm, also known as BFPRT (named after the authors of Blum et al. (1973)). It can be used to find the kth smallest value in a list, also known as the "kth order statistic".
When k is halfway through a list, then quickselect finds the median of the list in O(n) time. While this asymptotically linear algorithm is provably optimal, the constant factor overhead is known to be large, making this approach less useful in practice. In theory, however, the median of medians trick can be a very powerful proof step.
This package is intended primarily as a learning tool rather than a practical implementation. For faster runtime on most reasonably sized problems, prefer standard implementations of the median.
Installation
pip install bfprt
Usage
from bfprt import select_fast
# Items can have any type that implements less-than comparison
items = [4, 2, 1, 9, 5, 8]
# We want to get the kth smallest element from the list
k = 3
# This is the index of the kth smallest element
selected_index = select_fast(items, 0, 5, i)
# If k is len(items) // 2, then this is the median
kth_order_statistic = items[selected_index]
Installing from source
Install using Poetry
poetry install
Project details
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
File details
Details for the file bfprt-0.2.1.tar.gz
.
File metadata
- Download URL: bfprt-0.2.1.tar.gz
- Upload date:
- Size: 3.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.8.3 CPython/3.9.12 Darwin/22.6.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 451b070f9c77115bbcc657761a0ffde953a8de42146632f2bdf6b4031de4af2a |
|
MD5 | e2a1a7e5032ce77b4b808b716ef2b911 |
|
BLAKE2b-256 | bcedde49b56d7c92a31e8e617cca13f6abbe77ef46c13f23c8ae03bd529740ba |
File details
Details for the file bfprt-0.2.1-py3-none-any.whl
.
File metadata
- Download URL: bfprt-0.2.1-py3-none-any.whl
- Upload date:
- Size: 4.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.8.3 CPython/3.9.12 Darwin/22.6.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | a58f4ef2fa044ec751d5f65eea60d170507a0b1b739212bf9112c925538dac0d |
|
MD5 | 1c4344468ee0beee8664c72ce3a9f74e |
|
BLAKE2b-256 | a9fad3dc6649a5ccb479efe52c0cb1fe99207e51f170fecd52e4c0c60353beac |