(nums: &[i32], num: i32)
| 1 | // exponential_search.rs |
| 2 | |
| 3 | fn 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 | |
| 23 | fn exponential_search(nums: &[i32], target: i32) -> bool { |
| 24 | let size = nums.len(); |
no test coverage detected