Created
January 2, 2022 16:15
-
-
Save mertbozkir/82e8c095b0d1f1371b93241053f54c77 to your computer and use it in GitHub Desktop.
Binary Search algorithm O(log n) runtime complexity
This file contains 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 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