在这里,我尝试在Python(3.6.1)中实现插值搜索。我的逻辑似乎是正确的,但输出总是给我假,除非我把列表的第一个索引作为搜索键。 代码如下:
def NSearch(l,n,s):
beg = 0
end = (n-1)
while (beg<=end) and (s>=l[beg]) and (s<=l[end]):
p = beg + int(((float(end - beg) / ( l[end] - l[beg])) * ( s - l[beg])))
if (l[p]==s):
return True
if (l[p]<=s):
beg = p + 1
else:
end = p - 1
return False
liost = [23, 76, 17, -87, 56]
#x = int(input())
x = 76
print(NSearch(liost,len(liost),x))
读取:How-to-debug-small-programs/
我在while上设置了一个断点,并仔细查看了示例的值。你知道吗
然后我查了查interpolation search。你知道吗
然后我看了一下你的意见:
并将其固定为:
现在,如果找到它-添加了几个其他值来检查和瞧:工程。你知道吗
旁注:
相关问题 更多 >
编程相关推荐