Python 在这个算法中循环的结尾是什么
循环的终点是什么 没有中断,条件仍然为真Python 在这个算法中循环的结尾是什么,python,algorithm,while-loop,break,Python,Algorithm,While Loop,Break,循环的终点是什么 没有中断,条件仍然为真 def binary_search(list, target): ''' This function will return one of two things: 1 - index position of the target 2 - None ''' first = 0 last = len(list) - 1 while first <= last: middl
def binary_search(list, target):
'''
This function will return one of two things:
1 - index position of the target
2 - None
'''
first = 0
last = len(list) - 1
while first <= last:
middle = (first + last) // 2
if list[middle] == target:
return middle # Return the index position of the target
elif list[middle] < target:
first = middle + 1
elif list[middle] > target:
last = middle - 1
return None
def二进制搜索(列表,目标):
'''
此函数将返回以下两项之一:
1-目标的索引位置
2-无
'''
第一个=0
last=len(列表)-1
第一个目标是:
最后=中间-1
一无所获
当列表[middle]==target
时,返回中间值
def binary_search(list, target):
first = 0
last = len(list) - 1
while first <= last:
middle = (first + last) // 2
if list[middle] == target:
print("stopping...")
return middle # Return the index position of the target
elif list[middle] < target:
first = middle + 1
elif list[middle] > target:
last = middle - 1
return None
binary_search([1, 2, 3, 4, 5], 3) # >>> stopping...
def二进制搜索(列表,目标):
第一个=0
last=len(列表)-1
第一个目标是:
最后=中间-1
一无所获
二进制搜索([1,2,3,4,5],3)#>>>停止。。。
返回将取消该功能。在这种情况下,这将发生在return中间
return
语句完成此循环。但只有当存在一个等于target
的元素时,才可以使用调试器一步一步地检查代码,这样就没有问题了。这是二进制搜索算法。它将结束,因为在某一点上,您将返回middle,结束函数,或者last将变为条件仍然为True
?first
和last
都可以在循环期间修改,因此while
可能会退出。