Function algos::search::binary [−][src]
pub fn binary<T: Ord>(a: &[T], x: &T) -> Result<usize, usize>
Binary Search: Search for the value 'x' in an array. Returns the leftmost term if 'x' not find as Err.
Time complexity | Space complexity | |
---|---|---|
Best: | Ω(1) | |
Avrg: | θ(log(n)) | |
Worst: | O(log(n)) | O(1) |
#Examples
use algos::search; let v = [1, 3, 4, 8, 11, 17, 23]; let find = search::binary(&v, &11); assert_eq!(find, Ok(4)); let find2 = search::binary(&v, &19); assert_eq!(find2, Err(6));