Skip to content

Instantly share code, notes, and snippets.

@javi830810
Created October 15, 2015 19:19
Show Gist options
  • Select an option

  • Save javi830810/336ed64a6646e61b71b4 to your computer and use it in GitHub Desktop.

Select an option

Save javi830810/336ed64a6646e61b71b4 to your computer and use it in GitHub Desktop.
arr = [1,2,3, 4, 5, 6]
#In Place
def binary_search(elem, arr, init=None, end=None):
if not init and not end:
init = 0
end = len(arr)
if end < init or init >= len(arr):
return False
if init == end:
return arr[init] == elem
mid_point = (init+end)/2
median = arr[mid_point]
if elem == median:
return True
elif elem < median:
return binary_search(elem, arr, init, mid_point)
else:
return binary_search(elem, arr, mid_point+1, end)
# Memory Cost
def binary_search_2(elem, arr):
mid_point = len(arr)/2
if not arr:
return False
elif mid_point == 0:
return arr[0] == elem
elif elem < arr[mid_point]:
return binary_search_2(elem, arr[0:mid_point])
else:
return binary_search_2(elem, arr[mid_point:len(arr)])
print binary_search(5, arr)
print binary_search_2(5, arr)
# for x in arr:
# print str(binary_search(x, arr)) + ' == ' + 'True'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment