python
def binary_search(lst, x):
lst.sort()
if len(lst) >= 1:
pivot = (len(lst)-1) // 2
if lst[pivot] == x:
return True
elif lst[pivot] > x:
return binary_search(lst[:pivot], x)
elif lst[pivot] < x:
return binary_search(lst[pivot+1:], x)
return False
def binary_search2(lst, x):
lst.sort()
head = 0
tail = len(lst)-1
while head <= tail:
pivot = (head + tail) // 2
if lst[pivot]>x:
tail = pivot - 1
elif lst[pivot]<x:
head = pivot + 1
elif lst[pivot] == x:
return True
return False
if __name__ == '__main__':
lst = [5, 3, 1, 8, 9, 10]
print(binary_search(lst, 3))
print(binary_search(lst, 100))
print(binary_search2(lst, 8))
print(binary_search2(lst, 100))
True
False
True
False