Binary Search
Pseudocode
import math
primes = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97]
def doSearch(array, target_value):
min_value = 0
max_value = len(array) - 1
while min_value <= max_value:
guess = int(math.floor((max_value + min_value) / 2))
if array[guess] == target_value:
return guess
elif array[guess] < target_value:
min_value = guess + 1
else:
max_value = guess - 1
return -1;
target = 73
print(doSearch(primes, target))Last updated