为什么我无法使用二进制搜索从列表中获取最后一个数字?
我已经编写了一个代码来查找用户要求的数字的索引位置。除最后一个数字(在对列表进行排序之后)以外,在这种情况下为92,以下是代码,我将获得所有数字的答案。请指导这里有什么问题。
# Python program to implement binary search
list = [54, 67, 85, 33, 92, 74]
list.sort()
print(f'Given list is: {list}')
low = 0
upp = len(list)-1
mid = (low + upp) // 2
num = list.index(int(input('Enter number: ')))
while num != mid:
mid = (low + upp) // 2
if num < mid:
upp = mid
mid = (low + upp) // 2
else:
if num > mid:
low = mid
mid = (low + upp) // 2
print(f'found at {mid} index')
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您所描述的问题是,您使用的算法旨在将
upp
作为 exclusive 上限;通过将其初始化为len(list)-1
,您根本找不到最上面的索引。更改upp = len(list)-1
toupp = len(list)
和代码“ works”(它没有做任何有用的事情)。您的其他问题是:
num = list.index(int(intpation('enter number:')))
直接使您获得索引,并使用o(o(n))
搜索在引擎盖下,消除了二进制搜索的好处。您的其余代码甚至都不查看您的list
,您只是在做o(log n)
工作以查找MID
等于num
,当mid = num
将达到相同的结果时,不涉及搜索(no 附加搜索超出list.index
无论如何都在做)。list
,这意味着list
构造函数不可用;这是一个坏主意(即使在这里没有破坏任何东西)。list list
(在您的当前代码中,<代码> list.index 通过提高异常来调用该处理,但是如前所述,如果您使用list.index
,您将完全无法从二进制搜索中受益)。对于#1,您想接受没有
list.index
的输入:然后更改
num
和MID
之间的每个比较以比较num
和list [mid]
(或myList [mid]
或您用于list> List
)的任何更好的名称),所以num
始终是搜索 value ,中间
是当前索引正在搜索,list [mid]
是该索引的价值。您还需要改进搜索,因此当您要搜索的值中不存在list
(作为练习;在伪代码和Python中,在那里),而不是永远循环。Your described problem is that the algorithm you're using is intended to have
upp
as the exclusive upper bound; by initializing it tolen(list)-1
, you can't find the uppermost index at all. Changeupp = len(list)-1
toupp = len(list)
and the code "works" (it just doesn't do anything useful).Your other problems are:
num = list.index(int(input('Enter number: ')))
is getting you the index directly, withO(n)
search under the hood, eliminating the benefit of binary searching. The rest of your code isn't even looking at yourlist
, you're just doingO(log n)
work to find amid
equal tonum
, whenmid = num
would achieve the same result with no search involved (no additional search beyond whatlist.index
was doing anyway).list
, which means thelist
constructor is unavailable; this is a bad idea (even if it doesn't break anything here).list
(in your current code, thelist.index
call handles that by raising an exception, but as noted, if you uselist.index
, you gain no benefit from binary search at all).For #1, you wanted to accept the input without a
list.index
call:then change each comparison between
num
andmid
to comparenum
andlist[mid]
(ormylist[mid]
or whatever better name you use for thelist
), sonum
is always the value being searched for,mid
is the current index being searched, andlist[mid]
is the value at that index. You also need to improve your search so it stops when the value you're searching for doesn't exist in thelist
(left as exercise; there's thousands upon thousands of examples of hand-implemented binary search, in pseudocode and Python, out there), rather than looping forever.这是一个二进制搜索实现,可以帮助您查看自己出了问题的位置:
Here's a binary search implementation that may help you to see where you've gone wrong: