Created
August 8, 2022 12:17
-
-
Save odysseus0/d33702f279ae5ed64ab8723f5c198122 to your computer and use it in GitHub Desktop.
[Binary Search] Classical Binary Search #python #algo
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(array, target): | |
| """ | |
| input: int[] array, int target | |
| return: int | |
| """ | |
| l = 0 | |
| r = len(array) | |
| while l < r: | |
| m = l + (r - l) // 2 | |
| if array[m] == target: | |
| return m | |
| elif target < array[m]: | |
| r = m | |
| else: | |
| l = m + 1 | |
| return -1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment