Iterative binary search in Python
2013-01-12
This is an iterative binary search algorithm written in python.
def binary_search(the_array, the_key, imin, imax):
while imax >= imin:
imid = imin + ((imax - imin) / 2)
if the_array[imid] < the_key:
imin = imid + 1
elif the_array[imid] > the_key:
imax = imid - 1
else:
return imid
return None
You might also be interested in the recursive algorithm for binary search in Python.