Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/343.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 二进制搜索递归实现_Python_Recursion - Fatal编程技术网

Python 二进制搜索递归实现

Python 二进制搜索递归实现,python,recursion,Python,Recursion,我正在尝试创建二进制搜索的递归版本的实现。这就是我目前所拥有的。有人能帮忙吗?我不知道该怎么完成 def binarySearch(searchList, numberSought, low, high): if high < low: return False midpoint = (low + high)//2 print ("high is index", high) print ("low is index", low) pr

我正在尝试创建二进制搜索的递归版本的实现。这就是我目前所拥有的。有人能帮忙吗?我不知道该怎么完成

def binarySearch(searchList, numberSought, low, high):
    if high < low:
        return False
    midpoint = (low + high)//2
    print ("high is index", high)
    print ("low is index", low)
    print ("midpoint is index", midpoint)
    if searchList[midpoint] == numberSought:
        return True
    elif ...

    else:
        ...

mylist = [2, 4, 7, 13, 21, 22, 27, 31, 41, 77, 97, 144, 168]
first = 0
last = len(mylist) - 1
candidate = int(input("Does our list contain the following number? "))
print ("It is ",binarySearch(mylist,candidate,first,last), "that our list contains", candidate)
def binarySearch(searchList、numbershow、low、high):
如果高<低:
返回错误
中点=(低+高)//2
打印(“高即索引”,高)
打印(“低即索引”,低)
打印(“中点即索引”,中点)
如果搜索列表[中点]==数字搜索:
返回真值
埃利夫。。。
其他:
...
mylist=[2,4,7,13,21,22,27,31,41,77,97,144,168]
第一个=0
last=len(mylist)-1
candidate=int(输入(“我们的列表是否包含以下数字?”)
打印(“它是”,二进制搜索(mylist,candidate,first,last),“我们的列表包含的”,candidate)

下一步是填写以下空白:

    if searchList[midpoint] == numberSought:
        return True
    elif searchList[midpoint] < numberSought:
        pass # somehow search left of midpoint here
    else: # must have > numberSought
        pass # somehow search right of midpoint here
如果搜索列表[中点]==数字搜索:
返回真值
elif搜索列表[中点]<数字搜索:
传球#在中点左侧搜索
else:#必须有>数字搜索
通过#在这里搜索中点右侧

这有帮助吗?

为什么不看看Python中非递归但规范化实现的源代码呢?当然,您必须将while循环转换为递归。

您可以使用此递归程序。。执行二进制搜索

>>>def BS(list,key,min,max):
    if max<min:
        return None
    else:
        mid=(min+(max-min)/2)
    if list[mid]>key:
        return BS(list,keyey,min,mid-1)
    elif list[mid]<key:
        return BS(list,key,mid+1,max)
    else:
        return mid

>>> min = 0
>>> list = [2, 4, 7, 13, 21, 22, 27, 31, 41, 77, 97, 144, 168]
>>> max = len(list)-1
>>> key = 21
>>> BS(list,key,min,max)
def BS(列表、键、最小值、最大值): 如果maxkey: 返回BS(列表、密钥、最小值、中间值1) elif列表[mid]>>min=0 >>>列表=[2,4,7,13,21,22,27,31,41,77,97,144,168] >>>最大值=长度(列表)-1 >>>键=21 >>>BS(列表、键、最小值、最大值) 维基说:
二进制搜索或半间隔搜索算法查找指定输入值(搜索“键”)在按键值排序的数组中的位置。[1][2]在每个步骤中,该算法将搜索键值与数组中间元素的键值进行比较。如果键匹配,则找到匹配的元素并返回其索引或位置。否则,如果搜索键小于中间元素的键,则算法将在中间元素左侧的子数组上重复其操作,如果搜索键较大,则在右侧的子数组上重复其操作。如果要搜索的剩余数组为空,则在数组中找不到密钥,并返回一个特殊的“未找到”指示。

那么这将是关闭挖掘的正确来源?如果lo<0:13,则提升值错误(“lo必须为非负”)14如果hi为无:15 hi=len(a)16,而lopass语句都应该被另一个对
binarySearch
的调用所取代。。。