This post explains how to implement binary search in a very simple way. In addition to binary search, it also introduces how to implement binary insertion (both considering left insertion, right insertion). This article is a study note so it might have incomplete info.
Binary search is so powerful for its simpleness. We can make it O(n) to O(log n) compare to linear search. Unfortunately, we rarely use Binary search at leetcode or coding interviews.
(Cited from wikimedia)
However, as for Binary insertion(where we don't look if there are any targets in the source, but we look where to insert the target), we frequently use and this will be a very powerful tool.
There is a lot of questions for coding interviews which we can't solve by O(n**2). In these cases, if are using a linear way to find something, we can replace it with binary insertion. Thus, we can make the time complexity O(n*long n) which will be able to pass.
Actually, we can use the built-in function which is so easy to use. However, to understand deeply, and just in case to be asked to implement, I note my codes.
True
if there is a target in the source, otherwise, return False
.Here is the code.
def binarySearch(source, target):
lo = 0
hi = len(source)
while lo < hi:
mid = (lo + hi) // 2
if source[mid] == target:
return True
if source[mid] < target:
lo = mid + 1
else:
hi = mid - 1
return False
lo
larger than mid. (mid+1
)hi
is too large, so make the hi
small than mid. (mid-1
)Here is the code.
def binaryInsertionLeft(source, target):
lo = 0
hi = len(source)
while lo < hi:
mid = (lo + hi) // 2
if source[mid] < target:
lo = mid + 1
else:
hi = mid
return lo
source[mid] == target
and source[mid] > target
hi = mid
, not hi = mid-1
and return lo
Here is the code.
def binaryInsertionRight(source, target):
lo = 0
hi = len(source)
while lo < hi:
mid = (lo + hi) // 2
if source[mid] <= target:
lo = mid + 1
else:
hi = mid
return lo
source[mid] == target
and source[mid] < target
We can use the built-in function, which is very easy to use. As for python, we can use bisect
modules. Here is the internal implementation.
How to use is as follow,
import bisect # for leetcode, we don't even need this
source = [1,2,2,2,2,2,2,2,3]
target = 2
print(bisect.bisect_left(source, target)) #1
print(bisect.bisect_right(source, target)) #9