Algorithms Library for Rust
A comprehensive, zero-dependency collection of common algorithms for Rust. The crate is published on crates.io and documented on docs.rs.
Installation
Add the crate to your Cargo.toml:
[]
= "2.0.0"
Quick Start
use ;
use HashMap;
let sorted = quick_sort;
assert_eq!;
let idx = binary_search.unwrap;
assert_eq!;
let mut graph: = new;
graph.insert;
graph.insert;
graph.insert;
graph.insert;
assert_eq!;
let mut weighted: = new;
weighted.insert;
weighted.insert;
weighted.insert;
weighted.insert;
let = dijkstra.unwrap;
assert_eq!;
Features
- Zero runtime dependencies: Uses only the Rust standard library
- Comprehensive coverage: Sorting, searching, graphs, dynamic programming, strings, greedy, and divide-and-conquer
- Generic APIs: Strongly-typed with trait bounds
- Well tested: Integration tests for common and edge cases
- Clear errors: Methods return
Result<T, Error>where appropriate
Supported Algorithms
Sorting
quick_sort: quick sortmerge_sort: stable merge sortheap_sort: binary max-heap sortradix_sort: LSD radix sort for non-negative integersnative_sort: standard library sort wrapper
Searching
binary_search: classic binary searchinterpolation_search: interpolation search for numeric datajump_search: jump search with block sizesqrt(n)
Graphs
bfs: breadth-first searchdfs: depth-first searchdijkstra: Dijkstra shortest paths (non-negative weights)a_star: A* shortest path with heuristicbellman_ford: shortest paths with negative-weight cycle detection
Dynamic Programming
knapsack_01: 0/1 knapsack maximum valuelongest_common_subsequence: LCS lengthedit_distance: Levenshtein distance
String Algorithms
kmp_search: Knuth-Morris-Pratt pattern matchingrabin_karp_search: rolling-hash pattern matchingboyer_moore_search: Boyer-Moore with bad-character and good-suffix rules; reports overlapping matches (byte offsets)
Greedy
activity_selection: maximum compatible activitiesfractional_knapsack: fractional knapsack maximum valuehuffman_coding: optimal prefix codes
Divide and Conquer
max_subarray: maximum subarray sumcount_inversions: inversion count via merge sortfast_power: exponentiation by squaring (returnsErrfor0to a negative power)
Development
License
MIT License. See LICENSE for details.