我无法让这种二进制排序正常工作。lower_bound
我在其他地方遇到语法错误。我知道这很明显,但我正处于办公桌的转折点。有人可以帮我让它清晰运行吗,我知道这是基本的东西,但我对此很陌生。
def main():
sortNames()
binarySearch()
def sortNames():
global names
names = ["Chris Rich", "Ava Fischer", "Bob White", "Danielle Porter", "Gordon Pike", "Hannah Beauregard", "Matt Hoyle", "Ross Harrison", "Sasha Ricci", "Xavier Adams"]
names.sort()
def binarySearch():
global names, found, nameSearch, lower_bound, middle_pos, upper_bound
nameSearch = string(input("What name are you looking for?")
lower_bound = 0
upper_bound = len(names)-1
found = False
while lower_bound <= upper_bound and not found:
middle_pos = (lower_bound+upper_bound) // 2
if name_list[middle_pos] < nameSearch:
lower_bound = middle_pos + 1
elif name_list[middle_pos] > nameSearch:
upper_bound = middle_pos - 1
else:
found = True
if found:
print("The name is at position", middle_pos)
else:
print("The name was not in the list.")
main()