Algo
A small crate of commonly used sorting algorithms for any generic type that implements PartialOrd and Copy.
The crate can be found here: Crate
Sorting
- merge sort
- quick sort
- insertion sort
- bubble sort
Compare
- Find the longest common subsequence of two strings
- Find the longest common substring of two strings
Search
- Find a value or it's index through binary search
Usage
[dependencies]
rs_algo = "0.1"
Example
extern crate rs_algo;
use rs_algo::sort::*;
use rs_algo::search::binary;
use rs_algo::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];
merge::sort(&mut a);
quick::sort(&mut b);
insertion::sort(&mut c);
bubble::sort(&mut d);
let sequence = LCSubsequence::new_subsequence("leighxxxft".to_string(), "right".to_string());
assert_eq!(sequence.subsequence_len, 4);
assert_eq!(sequence.get_longest_subsequence(), Some("ight".to_string()));
let substring = LCSubstring::new_substring("!!!!Hello WorldXXXXX".to_string(), "XX Hello World@cvcvcvc".to_string());
assert_eq!(substring.substring_len, 11);
assert_eq!(substring.get_longest_substring(), Some("Hello World".to_string()));
match binary::search(99, &a) {
Some(value) => println!("our array has value {}", value),
None => println!("our array dosen't have value 99"),
}
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"),
}
}
License
MIT