Skip to main content

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, k)

# 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

bfprt-0.3.0.tar.gz (4.0 kB view hashes)

Uploaded Source

Built Distribution

bfprt-0.3.0-py3-none-any.whl (4.2 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page