Skip to content

Instantly share code, notes, and snippets.

View moulya-somasundara's full-sized avatar
👀
May the forks be with you

Moulya moulya-somasundara

👀
May the forks be with you
  • Hoboken
View GitHub Profile
def binarySearch (list, l, r, target):
# Check base case
if r >= l:
mid = l + (r - l)/2
# If element is present at the middle itself
if arr[mid] == target:
return mid
def binarySearch(self, nums: List[int], target: int) -> int:
left, right = 0, len(nums) - 1
#The list should be sorted
while left <= right:
middle = (left + right) // 2
#If the element is equal to the middle element
if nums[middle] == target:
return middle
#If the element is less than the middle element, ignore the right half of the list
else: