Binary Search Code Template Deep Dive
Let’s take a look at a easy problem on Leetcode 704. Binary Search. Besides the brute-force O(n) solution, it’s not hard to get the O(log(n)) solution from the constrains unique
and sorted in ascending order
. Binary search is one of the most basic algorithms we are using, but most people couldn’t get the right code.
Based on the open/close intervals, there are two different templates for Binary Search code:
Left-closed, Right-closed [left, right]
Two tips if you chose this one:
- Use
left <= right
for the loop condition checking - When narrowing dow to a sub-range, use
left = middle + 1
orright = middle - 1
1 | func search(nums []int, target int) int { |
Golang Playbook: https://play.golang.org/p/g_mimUUYz2H
Left-closed, Right-opened [left, right)
Samething above, two similar tips for this template:
- Use
left < right
for the loop condition checking, it’s because 0-based array list, the right open interval is invalid index. - When narrowing down to a sub-range, use
left = middle + 1
andright = middle
to keep the consistance of the intervals
1 | func search(nums []int, target int) int { |
Golang Playbook: https://play.golang.org/p/tkUkoNElKSV
Another important thing to keep in mind is that the range overflow. You may notice that when we calculate the new sub ranges above, we are using middle := left + (right-left)/2
instead of middle := (left + right)/2
. So what’s the difference between those two? Mathmatically there’s no difference, but in computer world, the later one postentially can cause an overflow issue when the range of the array is too large. left + right
could be larger than the biggest int
.