Binary Search algorithm. Done in python.
def findWordinLibraryBinarySearch(usrInput):
usrInput = usrInput.lower()
if any(char.isdigit() for char in usrInput):
raise Exception('no numbers allowed')
library = libraryBuilder()
words = charPermutations(usrInput)
foundWords = {}
xtime = time.time()
for word in words:
first = int(0) # Making sure its integers.
last = int(len(library)) # Making sure its integers.
found = False
while first <= last and not found:
midpoint = (first + last) / 2
if library[midpoint] == word:
found = True
else:
if word < library[midpoint]:
last = midpoint - 1
else:
first = midpoint + 1
if found:
foundWords[midpoint] = library[midpoint]
xtime = time.time() - xtime
foundWords['time'] = xtime
return foundWords