Binary search patterns
Binary Search: Patterns to understand algorithm design
Patterns
Interviewing bar is getting higher and higher for software engineers. Binary search is a topic that is elegant and has a lot of room for customization. Just some notes as I learn a bit of Binary search
Template:
- Initialize the search space (start and end)
- Find middle of the search space
- Discard/select one half based on the specific condition, choice of loop break condition is important.
- Update the search space (this also depends on the condition and choice of start and end)
- Break the loop once the search space is narrowed down to a single element
Template code
If we choose left and right based on the size of the search space
`class Solution: def search(self, nums: List[int], target: int) -> int: left,right = 0,len(nums)
while left < right:
mid = left + (right - left)//2
if nums[mid] >= target:
right = mid
else:
left = mid+1
if nums[left] == target:
return left
else:
return -1`
Or if we choose left and right as first and last index
`class Solution: def search(self, nums: List[int], target: int) -> int: left,right = 0,len(nums)-1
while left <= right:
mid = left + (right - left)//2
if nums[mid] >= target:
right = mid-1
else:
left = mid+1
if left < len(nums) and nums[left] == target:
return left
else:
return -1`
Standard library support for Binary Search (Always ask the interviewer if the question is primarily about Binary search)
Python Bisect Module
- Python bisect module has four methods that can help us skip the implemnetation of Binary search and use it out of the box.
- bisect.bisect_left(a, x, lo=0, hi=len(a), *, key=None), locate left-most insertion point for x in a, lo and hi can be used to indicate the search subset, ignore the key for now
- bisect.bisect_right(a, x, lo=0, hi=len(a), *, key=None), locate right-most insertion point for x in a, lo and hi can be used to indicate the search subset, ignore the key for now
- bisect.insort_left(a, x, lo=0, hi=len(a), *, key=None), Insert after finding the correct leftmost place for x in a
- bisect.insort_right(a, x, lo=0, hi=len(a), *, key=None), Insert after finding the correct rightmost place for x in a
- bisect.insort(a, x, lo=0, hi=len(a), *, key=None), essentially same as insort_right
Java Arrays.binarysearch(array,key)
##Follow-up questions
- How would you search an unbounded array?
- Why only binary search, why not a ternary search, i.e divide the array into three parts?