Created
October 21, 2017 15:59
-
-
Save astro313/fb91eb9c5866936abf31ef8ca7eecd84 to your computer and use it in GitHub Desktop.
Binary search function
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
| import math | |
| def binary_search(array, search): | |
| m = 0 | |
| i = 0 | |
| z = len(array) - 1 | |
| while i<= z: | |
| m = math.floor(i + ((z - i) / 2)) | |
| if array[m] == search: | |
| return m | |
| elif array[m] < search: | |
| i = m + 1 | |
| elif array[m] > search: | |
| z = m - 1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment