Skip to content

Commit

Permalink
Merge pull request wangzheng0822#108 from jerryderry/bsearch-variant-…
Browse files Browse the repository at this point in the history
…python

Binary search variants in python
  • Loading branch information
wangzheng0822 authored Oct 26, 2018
2 parents 771fe9c + 58aa8bc commit c2e136f
Showing 1 changed file with 77 additions and 0 deletions.
77 changes: 77 additions & 0 deletions python/16_bsearch/bsearch_variants.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
"""
Author: Wenru
"""

from typing import List

def bsearch_left(nums: List[int], target: int) -> int:
"""Binary search of the index of the first element
equal to a given target in the ascending sorted array.
If not found, return -1.
"""
low, high = 0, len(nums) - 1
while low <= high:
mid = low + (high - low) // 2
if nums[mid] < target:
low = mid + 1
else:
high = mid - 1
return low if nums[low] == target else -1

def bsearch_right(nums: List[int], target: int) -> int:
"""Binary search of the index of the last element
equal to a given target in the ascending sorted array.
If not found, return -1.
"""
low, high = 0, len(nums) - 1
while low <= high:
mid = low + (high - low) // 2
if nums[mid] <= target:
low = mid + 1
else:
high = mid - 1
return high if nums[high] == target else -1


def bsearch_left_not_less(nums: List[int], target: int) -> int:
"""Binary search of the index of the first element
not less than a given target in the ascending sorted array.
If not found, return -1.
"""
low, high = 0, len(nums) - 1
while low <= high:
mid = low + (high - low) // 2
if nums[mid] < target:
low = mid + 1
else:
high = mid - 1
return low if low < len(nums) else -1

def bsearch_right_not_greater(nums: List[int], target: int) -> int:
"""Binary search of the index of the last element
not greater than a given target in the ascending sorted array.
If not found, return -1.
"""
low, high = 0, len(nums) - 1
while low <= high:
mid = low + (high - low) // 2
if nums[mid] <= target:
low = mid + 1
else:
high = mid - 1
return high if high > 0 else -1

if __name__ == "__main__":
import bisect

a = [0, 1, 1, 2, 3, 4, 5, 6, 6, 7, 8, 8, 10, 10, 10]
b = [11, 12, 12, 13, 14, 14, 15, 15]
print(bisect.bisect_left(a, 10) == bsearch_left(a, 10))
print(bisect.bisect_right(a, 10))
print(bisect.bisect_right(a, 6)-1 == bsearch_right(a, 6))
print(bisect.bisect_right(b, 14)-1 == bsearch_right(b, 14))

print(bsearch_left_not_less(a, 11))
print(bsearch_right_not_greater(b, 12))
print(bsearch_right_not_greater(b, 10))
print(bsearch_right_not_greater(b, 17))

0 comments on commit c2e136f

Please sign in to comment.