Interactive Learning Lab
Pick a core pattern, enter your own numbers, and step through each line in Rust, PHP, JavaScript, Python, or Go.
Halve the search window [low..high] on every comparison until target is found.
Start with low = 0 and high = 6. We are looking for target = 23.
1pub fn binary_search(nums: &[i32], target: i32) -> i32 {2 let (mut low, mut high) = (0isize, nums.len() as isize - 1);3 while low <= high {4 let mid = low + (high - low) / 2;5 let val = nums[mid as usize];6 if val == target {7 return mid as i32;8 } else if val < target {9 low = mid + 1;10 } else {11 high = mid - 1;12 }13 }14 -115}