#自适应 #第 n 个元素 #有序统计 #选择 #快速选择

adqselect

一个轻量级的crate,通过使用Andrei Alexandrescu的自适应快速选择算法来实现nth_element。

4个版本

0.1.3 2021年1月10日
0.1.2 2021年1月10日
0.1.1 2020年8月13日
0.1.0 2020年8月13日

#1005 in 算法

每月 39 次下载
fnntw 中使用

MIT 许可证

1.5MB
339 代码行

adqselect

License: MIT frjnn codecov

一个轻量级的crate,为Rust带来了一个利用Andrei Alexandrescu的nth_element实现,该实现利用了自适应快速选择算法。也可在crates.io上找到。

安装

确保你的Cargo.toml看起来像这样

[dependencies]
adqselect = "0.1.3"

使用

将crate引入作用域

extern crate adqselect;

use adqselect::nth_element;

然后在vector上简单地调用nth_element

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);

此实现还处理满足PartialEqPartialOrd特性的泛型数据类型。

实现

链接到Andrei Alexandrescu的原始论文:快速确定性选择

性能

该算法基于改进的Median of Medians版本,并保证线性确定性时间复杂度。

基准测试

以下是一些与其他crate的基准测试: floydrivestorder-statkthpdqselect

结果

小提琴图

Violin Plot

此图表显示了函数/参数与迭代时间之间的关系。阴影区域的厚度表示给定函数/参数的测量值可能持续特定时间的概率。

折线图

Line Chart

此图表显示了随着输入(或输入大小)的增加,每个函数的平均测量时间。

adqselect在1.000个随机无符号整数上的结果

PDF of Slope Regression

adqselect在10.000个随机无符号整数上的结果

PDF of Slope Regression

adqselect在100.000个随机无符号整数上的结果

PDF of Slope Regression

adqselect在1.000.000个随机无符号整数上的结果

PDF of Slope Iteration Times

floydrivest在1.000个随机无符号整数上的结果

PDF of Slope Regression

floydrivest在10.000个随机无符号整数上的结果

PDF of Slope Regression

floydrivest在100.000个随机无符号整数上的结果

PDF of Slope Regression

floydrivest在1.000.000个随机无符号整数上的结果

PDF of Slope Iteration Times

kth在1.000个随机无符号整数上的结果

PDF of Slope Regression

kth在10.000个随机无符号整数上的结果

PDF of Slope Regression

kth在100.000个随机无符号整数上的结果

PDF of Slope Regression

kth在1.000.000个随机无符号整数上的结果

PDF of Slope Iteration Times

order_stat在1.000个随机无符号整数上的结果

PDF of Slope Regression

order_stat在10.000个随机无符号整数上的结果

PDF of Slope Regression

order_stat在100.000个随机无符号整数上的结果

PDF of Slope Regression

order_stat在1.000.000个随机无符号整数上的结果

PDF of Slope Iteration Times

pdqselect在1.000个随机无符号整数上的结果

PDF of Slope Regression

pdqselect在10.000个随机无符号整数上的结果

PDF of Slope Regression

在100,000个随机无符号整数上运行pdqselect

PDF of Slope Regression

在1,000,000个随机无符号整数上运行pdqselect

PDF of Slope Iteration Times

本报告由Criterion.rs生成,这是一个基于统计的Rust基准测试库。

无运行时依赖