-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathbinary_search.rs
More file actions
53 lines (42 loc) · 1.1 KB
/
binary_search.rs
File metadata and controls
53 lines (42 loc) · 1.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
use std::cmp::Ordering;
#[warn(dead_code)]
fn binary_search<T>(list: Vec<T>, item: T) -> Option<usize>
where
T: std::cmp::Ord,
{
if list.is_empty() {
return None;
};
let (mut low, mut high) = (0, list.len() - 1);
while low < high {
let mid = (low + high) / 2;
match list[mid].cmp(&item) {
Ordering::Greater => high = mid,
Ordering::Less => low = mid + 1,
Ordering::Equal => return Some(mid),
}
}
None
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn returns_none_if_items_is_empty() {
let items = vec![];
let result = binary_search(items, 1);
assert_eq!(result, None);
}
#[test]
fn doesnt_finds_if_collection_is_not_sorted() {
let items = vec![1024, 32, 16, 512, 256, 64, 128];
let result = binary_search(items, 1024);
assert_eq!(result, None);
}
#[test]
fn finds_index_of_element() {
let items = vec![16, 32, 64, 128, 256, 512, 1024];
let result = binary_search(items, 256);
assert_eq!(result, Some(4));
}
}