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: 6 months ago
JSON representation
Rust nth_element implementation that leverages Andrei Alexandrescu's Adaptive Quickselect algorithm.
- Host: GitHub
- URL: https://github.com/frjnn/adqselect
- Owner: frjnn
- License: mit
- Created: 2020-08-13T09:57:41.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2021-01-10T13:46:29.000Z (about 5 years ago)
- Last Synced: 2025-03-27T05:13:00.404Z (11 months ago)
- Topics: nthelement, order-statistics, quickselect, rust
- Language: Rust
- Homepage:
- Size: 1.08 MB
- Stars: 4
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# adqselect
[](https://opensource.org/licenses/MIT)
[](https://app.circleci.com/pipelines/github/frjnn/adqselect)
[](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
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
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
adqselect on 10.000 random unsigned integers
adqselect on 100.000 random unsigned integers
adqselect on 1.000.000 random unsigned integers
floydrivest on 1.000 random unsigned integers
floydrivest on 10.000 random unsigned integers
floydrivest on 100.000 random unsigned integers
floydrivest on 1.000.000 random unsigned integers
kth on 1.000 random unsigned integers
kth on 10.000 random unsigned integers
kth on 100.000 random unsigned integers
kth on 1.000.000 random unsigned integers
order_stat on 1.000 random unsigned integers
order_stat on 10.000 random unsigned integers
order_stat on 100.000 random unsigned integers
order_stat on 1.000.000 random unsigned integers
pdqselect on 1.000 random unsigned integers
pdqselect on 10.000 random unsigned integers
pdqselect on 100.000 random unsigned integers
pdqselect on 1.000.000 random unsigned integers