MCPcopy Create free account
hub / github.com/douchuan/algorithm / search

Function search

src/search/binary.rs:5–22  ·  view source on GitHub ↗
(arr: &[K], k: K)

Source from the content-addressed store, hash-verified

3use std::cmp::Ordering;
4
5pub fn search<K>(arr: &[K], k: K) -> Option<usize>
6where
7 K: Ord,
8{
9 let mut left = 0;
10 let mut right = arr.len();
11
12 while left < right {
13 let mid = (left + right) >> 1;
14 match arr[mid].cmp(&k) {
15 Ordering::Less => left = mid + 1,
16 Ordering::Equal => return Some(mid),
17 Ordering::Greater => right = mid,
18 }
19 }
20
21 None
22}

Callers

nothing calls this directly

Calls 2

lenMethod · 0.45
cmpMethod · 0.45

Tested by

no test coverage detected