Jun 27, 2024
def linear_search(list, target):
for i in range(len(list)):
if list[i] == target:
return i
return None
def binary_search(list, target):
first = 0
last = len(list) - 1
while first <= last:
midpoint = (first + last) // 2
if list[midpoint] == target:
return midpoint
elif list[midpoint] < target:
first = midpoint + 1
else:
last = midpoint - 1
return None
def recursive_binary_search(list, target):
if len(list) == 0:
return False
else:
midpoint = len(list) // 2
if list[midpoint] == target:
return True
elif list[midpoint] < target:
return recursive_binary_search(list[midpoint+1:], target)
else:
return recursive_binary_search(list[:midpoint], target)