added demo in main for complete algos. started dijkstras
This commit is contained in:
parent
f895efce44
commit
fc92909aec
10 changed files with 262 additions and 15 deletions
|
|
@ -1,49 +1,62 @@
|
|||
#[cfg (test)]
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::binary_search;
|
||||
|
||||
#[test]
|
||||
fn binary_search_primegen_test() {
|
||||
let haystack = [1, 3, 4, 69, 71, 81, 90, 99, 420, 1337, 69420];
|
||||
assert_eq!(binary_search(&haystack, 69), Some((true, 3)));
|
||||
assert_eq!(binary_search(&haystack, 1336), Some((false, 0)));
|
||||
assert_eq!(binary_search(&haystack, 69420), Some((true, 10)));
|
||||
assert_eq!(binary_search(&haystack, 69421), Some((false, 0)));
|
||||
assert_eq!(binary_search(&haystack, 1), Some((true, 0)));
|
||||
assert_eq!(binary_search(&haystack, 0), Some((false, 0)));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_binary_search_found() {
|
||||
let haystack = [1, 3, 5, 7, 9];
|
||||
assert_eq!(binary_search(&haystack, 5), Some(true));
|
||||
assert_eq!(binary_search(&haystack, 5), Some((true, 2)));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_binary_search_not_found() {
|
||||
let haystack = [1, 3, 5, 7, 9];
|
||||
assert_eq!(binary_search(&haystack, 4), Some(false));
|
||||
assert_eq!(binary_search(&haystack, 4), Some((false, 0)));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_binary_search_empty() {
|
||||
let haystack: [usize; 0] = [];
|
||||
assert_eq!(binary_search(&haystack, 4), Some(false));
|
||||
assert_eq!(binary_search(&haystack, 4), Some((false, 0)));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_binary_search_single_element_found() {
|
||||
let haystack = [5];
|
||||
assert_eq!(binary_search(&haystack, 5), Some(true));
|
||||
assert_eq!(binary_search(&haystack, 5), Some((true, 0)));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_binary_search_single_element_not_found() {
|
||||
let haystack = [5];
|
||||
assert_eq!(binary_search(&haystack, 3), Some(false));
|
||||
assert_eq!(binary_search(&haystack, 3), Some((false, 0)));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_binary_search_multiple_elements_not_found() {
|
||||
let haystack = [1, 2, 3, 4, 5];
|
||||
assert_eq!(binary_search(&haystack, 6), Some(false));
|
||||
assert_eq!(binary_search(&haystack, 6), Some((false, 0)));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_binary_search_multiple_elements_found() {
|
||||
let haystack = [1, 2, 3, 4, 5];
|
||||
assert_eq!(binary_search(&haystack, 3), Some(true));
|
||||
assert_eq!(binary_search(&haystack, 3), Some((true, 2)));
|
||||
}
|
||||
}
|
||||
fn binary_search(haystack: &[usize], needle: usize) -> Option<bool> {
|
||||
|
||||
pub fn binary_search(haystack: &[usize], needle: usize) -> Option<(bool, usize)> {
|
||||
let mut low = 0;
|
||||
let mut high = haystack.len();
|
||||
|
||||
|
|
@ -51,7 +64,7 @@ fn binary_search(haystack: &[usize], needle: usize) -> Option<bool> {
|
|||
let midpoint = low + (high - low) / 2;
|
||||
let value = haystack[midpoint];
|
||||
if value == needle {
|
||||
return Some(true);
|
||||
return Some((true, midpoint));
|
||||
} else if value > needle {
|
||||
high = midpoint;
|
||||
} else {
|
||||
|
|
@ -59,5 +72,5 @@ fn binary_search(haystack: &[usize], needle: usize) -> Option<bool> {
|
|||
}
|
||||
}
|
||||
|
||||
Some(false)
|
||||
Some((false, 0)) // Return 0 as index if value is not found
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue