Skip to content

Instantly share code, notes, and snippets.

@jadechip
Last active September 6, 2019 12:58
Show Gist options
  • Save jadechip/f5b36792736c0639cea22beb2ab192ad to your computer and use it in GitHub Desktop.
Save jadechip/f5b36792736c0639cea22beb2ab192ad to your computer and use it in GitHub Desktop.
Programming challenges: Binary Search
def binarySearch(array, target):
# Array has to be sorted in order to check surrounding elements
left = 0
right = len(array) - 1
while left <= right:
mid = (left + right)
guess = array[mid]
if guess == target:
return mid
if guess > target:
right = mid - 1 # Set new range starting from midpoint
else:
left = mid + 1 # Set new range from midpoint
return -1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment