This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: |