Loading RustViz Engine...
Search sorted slice without integer overflow in midpoint calculation.
This problem is currently in blueprint template mode. Once the concept foundation is finalized, live in-browser WebAssembly compilation and test assertion runners will be enabled here.
pub fn binary_search(nums: &[i32], target: i32) -> Option<usize> {
let mut low = 0;
let mut high = nums.len();
while low < high {
let mid = low + (high - low) / 2;
match nums[mid].cmp(&target) {
std::cmp::Ordering::Equal => return Some(mid),
std::cmp::Ordering::Less => low = mid + 1,
std::cmp::Ordering::Greater => high = mid,
}
}
None
}