A small crate of commonly used sorting algorithms for any generic type that implements PartialOrd and Copy. The crate can be found here: Crate
rust
[dependencies]
rs_algo = "^0.2"
```rust use rsalgo::math; use rsalgo::sort::*; use rsalgo::search::binary; use rsalgo::compare::{LCSubsequence, LCSubstring};
fn main() { let mut a = vec![117, 1, 3, 99, 10, 7, 17, 2, 11, -6, 4, 9, 10, 7, 2, 11, -5, 4, 9, 7, 2, 11, -5, 4, 9, 8]; let mut b = vec![117, 1, 3, 99, 10, 7, 17, 2, 11, -6, 4, 9, 10, 7, 2, 11, -5, 4, 9, 7, 2, 11, -5, 4, 9, 8]; let mut c = vec![117, 1, 3, 99, 10, 7, 17, 2, 11, -6, 4, 9, 10, 7, 2, 11, -5, 4, 9, 7, 2, 11, -5, 4, 9, 8]; let mut d = vec![117, 1, 3, 99, 10, 7, 17, 2, 11, -6, 4, 9, 10, 7, 2, 11, -5, 4, 9, 7, 2, 11, -5, 4, 9, 8];
// Get a sorted array without changing the original let sortedbubble = bubble::sort(&a); let sortedinsertion = insertion::sort(&b); let sortedmerge = merge::sort(&c); let sortedquick = quick::sort(&d);
// This will sort the vector passed in, changing the original vector order merge::sortmut(&mut a); quick::sortmut(&mut b); insertion::sortmut(&mut c); bubble::sortmut(&mut d);
// get a new longest common sequence object let sequence = LCSubsequence::newsubsequence("leighxxxft".tostring(), "right".tostring()); asserteq!(sequence.subsequencelen, 4); asserteq!(sequence.getlongestsubsequence(), Some("ight".to_string()));
// get a new longest common substring let substring = LCSubstring::newsubstring("!!!!Hello WorldXXXXX".tostring(), "XX Hello World@cvcvcvc".tostring()); asserteq!(substring.substringlen, 11); asserteq!(substring.getlongestsubstring(), Some("Hello World".to_string()));
// do a binary search on array 'a' to see if value 99 is in the array match binary::search(99, &a) { Some(value) => println!("our array has value {}", value), None => println!("our array dosen't have value 99"), }
// do a binary search on array 'a' to get the index. Note: with binary search, this may not be the first occurance match binary::index_of(99, &a) { Some(index) => println!("index of 99 is {}", index), None => println!("no index of 99, guess it's not in there"), }
// common math functions let divisor = math::gcd(30, 21); let factor = math::factors(9124);
asserteq!(Ok(3), divisor); asserteq!(Some(vec![2, 4, 2281, 4562]), factor); } ```
MIT License Copyright (c) <2018-2020> Joe Berria