Last active
December 12, 2018 07:41
-
-
Save RamonRomeroQro/c43f3eb438910cf687e8a3b73c2cdfdd to your computer and use it in GitHub Desktop.
Binary Search in Python
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(alist, item): | |
| first = 0 | |
| last = len(alist)-1 | |
| found = False | |
| while first<=last and not found: | |
| midpoint = (first + last)//2 | |
| if alist[midpoint] == item: | |
| found = True | |
| else: | |
| if item < alist[midpoint]: | |
| last = midpoint-1 | |
| else: | |
| first = midpoint+1 | |
| return found |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment