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

Function exponential_search

code/chapter05/exponential_search.rs:23–39  ·  view source on GitHub ↗
(nums: &[i32], target: i32)

Source from the content-addressed store, hash-verified

21}
22
23fn exponential_search(nums: &[i32], target: i32) -> bool {
24 let size = nums.len();
25 if size == 0 {
26 return false;
27 }
28
29 // 逐步找到上界
30 let mut high = 1usize;
31 while high < size && nums[high] < target {
32 high <<= 1;
33 }
34
35 // 上界的一半一定可以作为下界
36 let low = high >> 1;
37
38 binary_search(&nums[low..size.min(high+1)], target)
39}
40
41fn main() {
42 let nums = [1,9,10,15,16,17,19,23,27,28,29,30,32,35];

Callers 1

mainFunction · 0.70

Calls 3

binary_searchFunction · 0.70
lenMethod · 0.45
minMethod · 0.45

Tested by

no test coverage detected