An open API service indexing awesome lists of open source software.

https://github.com/frjnn/adqselect

Rust nth_element implementation that leverages Andrei Alexandrescu's Adaptive Quickselect algorithm.
https://github.com/frjnn/adqselect

nthelement order-statistics quickselect rust

Last synced: about 2 months ago
JSON representation

Rust nth_element implementation that leverages Andrei Alexandrescu's Adaptive Quickselect algorithm.

Awesome Lists containing this project

README

          

# adqselect

[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
[![frjnn](https://circleci.com/gh/frjnn/adqselect.svg?style=shield)](https://app.circleci.com/pipelines/github/frjnn/adqselect)
[![codecov](https://codecov.io/gh/frjnn/adqselect/branch/master/graph/badge.svg)](https://codecov.io/gh/frjnn/adqselect)

A lightweight crate that brings to Rust an `nth_element` implementation that leverages Andrei Alexandrescu's __adaptive quickselect__ algorithm. Also available on [crates.io](https://crates.io/crates/adqselect).

## Installation

Be sure that your `Cargo.toml` looks somewhat like this:
```toml
[dependencies]
adqselect = "0.1.3"
```
## Usage

Bring the crate into scope:

```rust
extern crate adqselect;

use adqselect::nth_element;
```
then simply call `nth_element` on a vector.

```rust
let mut v = vec![10, 7, 9, 7, 2, 8, 8, 1, 9, 4];
nth_element(&mut v, 3, &mut Ord::cmp);

assert_eq!(v[3], 7);
```

This implementation also handles generic data types as long as they satisfy the `PartialEq` and `PartialOrd` traits.

## Implementation

Link to the [original paper: Fast Deterministic Selection](https://arxiv.org/abs/1606.00484) by Andrei Alexandrescu.

## Performance

The algorithm is based on a refined version of Median of Medians and it guarantees linear deterministic time complexity.

## Benchmarks

Here are some benchmarks against other crates: [floydrivest](https://crates.io/crates/floydrivest), [order-stat](https://crates.io/crates/order-stat), [kth](https://crates.io/crates/kth) and [pdqselect](https://crates.io/crates/pdqselect).

Results

Violin Plot


Violin Plot

This chart shows the relationship between function/parameter and iteration time. The thickness of the shaded
region indicates the probability that a measurement of the given function/parameter would take a particular
length of time.


Line Chart


Line Chart

This chart shows the mean measured time for each function as the input (or the size of the input) increases.



adqselect on 1.000 random unsigned integers







PDF of Slope




Regression







adqselect on 10.000 random unsigned integers







PDF of Slope




Regression







adqselect on 100.000 random unsigned integers







PDF of Slope




Regression







adqselect on 1.000.000 random unsigned integers







PDF of Slope




Iteration Times







floydrivest on 1.000 random unsigned integers







PDF of Slope




Regression







floydrivest on 10.000 random unsigned integers







PDF of Slope




Regression







floydrivest on 100.000 random unsigned integers







PDF of Slope




Regression







floydrivest on 1.000.000 random unsigned integers







PDF of Slope




Iteration Times







kth on 1.000 random unsigned integers







PDF of Slope




Regression







kth on 10.000 random unsigned integers







PDF of Slope




Regression







kth on 100.000 random unsigned integers







PDF of Slope




Regression







kth on 1.000.000 random unsigned integers







PDF of Slope




Iteration Times







order_stat on 1.000 random unsigned integers







PDF of Slope




Regression







order_stat on 10.000 random unsigned integers







PDF of Slope




Regression







order_stat on 100.000 random unsigned integers







PDF of Slope




Regression







order_stat on 1.000.000 random unsigned integers







PDF of Slope




Iteration Times







pdqselect on 1.000 random unsigned integers







PDF of Slope




Regression







pdqselect on 10.000 random unsigned integers







PDF of Slope




Regression







pdqselect on 100.000 random unsigned integers







PDF of Slope




Regression







pdqselect on 1.000.000 random unsigned integers







PDF of Slope




Iteration Times








This report was generated by
Criterion.rs, a statistics-driven benchmarking
library in Rust.