1 个不稳定版本
0.1.1 | 2023年4月27日 |
---|
#22 in #斐波那契
10KB
144 行
这个库实现了比二分查找算法更快的算法,是为了纪念Nisar先生,BMI学院于4月7日计划发布,并于2023年4月27日发布,并于4月28日公开。模块库可以作为crate在Rust中导入,如下所示,在您的Cargo.toml
文件中包含此片段:
[dependencies]
AitSar = "0.1.1"
用法
要使用此库及其功能,请在您的main程序中使用此片段
use AitSar::search::{ternary, hash, interpolation, fibonacci};
use AitSar::table::{hash};
说明
Nisar先生于4月6日在一次在线课程中宣称,二分查找算法通过将数组分为一半是速度最快的算法,这是剑桥大学支持的观点。Aitzaz立即指出这是错误的;Nisar先生告诉他应该写一本书,用我自己发现的这个实现来写,但他知道这些快速算法已经存在。所以他就创造了这个!
Ternary search algorithm: It is a divide and conquer algorithm used to find the maximum or minimum value in a unimodal function. It is faster than binary
search in this specific scenario.
Hash table: A hash table is a data structure that uses a hash function to map keys to values. It allows for constant time lookup and insertion, which can be
faster than binary search for certain types of data.
Interpolation search: This algorithm is similar to binary search, but it uses interpolation to guess where the target element might be. It can be faster
than binary search for certain types of data that are evenly distributed.
Exponential search: This algorithm works by finding a range in which the target element might be, and then using binary search within that range. It can be
faster than binary search for very large arrays.
Fibonacci search: This algorithm is similar to exponential search, but it uses Fibonacci numbers to determine the range to search. It can be faster than
binary search for very large arrays, but it requires precomputing the Fibonacci sequence.
示例
fn main() {
let arr = vec![1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
let x = 6;
let result = ternary::ternary_search(&arr, 0, arr.len() - 1, x);
if let Some(index) = result {
println!("Found {} at index {}", x, index);
} else {
println!("{} not found in array", x);
}
}
无运行时依赖
~0–3MB
~66K SLoC