MCPcopy Create free account
hub / github.com/QMHTMY/RustBook / binary_search

Function binary_search

code/chapter05/exponential_search.rs:3–21  ·  view source on GitHub ↗
(nums: &[i32], num: i32)

Source from the content-addressed store, hash-verified

1// exponential_search.rs
2
3fn binary_search(nums: &[i32], num: i32) -> bool {
4 let mut low = 0;
5 let mut high = nums.len() - 1;
6 let mut found = false;
7
8 // 注意是 <= 不是 <
9 while low <= high && !found {
10 let mid: usize = (low + high) >> 1;
11 if num == nums[mid] {
12 found = true;
13 } else if num < nums[mid] {
14 high = mid - 1;
15 } else {
16 low = mid + 1;
17 }
18 }
19
20 found
21}
22
23fn exponential_search(nums: &[i32], target: i32) -> bool {
24 let size = nums.len();

Callers 1

exponential_searchFunction · 0.70

Calls 1

lenMethod · 0.45

Tested by

no test coverage detected