Skip to content

Instantly share code, notes, and snippets.

@SeanPlusPlus
Created March 9, 2017 22:26
Show Gist options
  • Save SeanPlusPlus/40cdfacf794d34b0270995ac1891b15c to your computer and use it in GitHub Desktop.
Save SeanPlusPlus/40cdfacf794d34b0270995ac1891b15c to your computer and use it in GitHub Desktop.
binary search in python
#!/usr/bin/env python
import time
from bisect import bisect_left
def binary_search(a, x):
# implement binary_search
return
def main():
a1 = [12, 16, 24, 37, 102, 109]
a2 = [1, 4, 7, 8, 9, 16]
a3 = [400, 401, 402, 403, 404, 405]
x = 16
print binary_search(a1, x)
print binary_search(a2, x)
print binary_search(a3, x)
if __name__ == '__main__':
start_time = time.time()
main()
print('--- %s seconds ---' % '%.2f' % (time.time() - start_time) )
# takes about 13 seconds to run
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment