Skip to content

Instantly share code, notes, and snippets.

@mertbozkir
Created January 2, 2022 16:15
Show Gist options
  • Save mertbozkir/82e8c095b0d1f1371b93241053f54c77 to your computer and use it in GitHub Desktop.
Save mertbozkir/82e8c095b0d1f1371b93241053f54c77 to your computer and use it in GitHub Desktop.
Binary Search algorithm O(log n) runtime complexity
def search(nums, target) -> int:
high = len(nums) - 1
low, mid = 0, 0
while low <= high:
mid = (high + low)// 2
if nums[mid] < target:
low = mid + 1
elif nums[mid] > target:
high = mid - 1
else:
return mid
return -1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment