Python 基于最大值(最后一项)、最小值(第一项)和使用二进制搜索的搜索值,估计要搜索的项的位置

Python 基于最大值(最后一项)、最小值(第一项)和使用二进制搜索的搜索值,估计要搜索的项的位置,python,data-structures,binary-search,Python,Data Structures,Binary Search,我的代码: def binary_search(seq,item): """It uses non recursive method to search the item in the given seq. It returns the position of item if found, None otherwise""" left_index=0 right_index=len(seq)-1 while left_index <

我的代码:

    def binary_search(seq,item):
    """It uses non recursive method to search the item in the given seq. 
       It returns the position of item if found, None otherwise"""

    left_index=0
    right_index=len(seq)-1
    while left_index <= right_index:            #stop searching when left_index >   right_indext
    mid_index=(right_index + left_index)//2 #find the mid point
    if seq[mid_index]==item:
    return mid_index
    elif seq[mid_index]>item:
    right_index = mid_index -1          #if mid point ele > search ele, move right pointer
    else:  
    left_index = mid_index + 1        #if mid point ele < search ele, move left pointer
    return None
    a=[1,2,3,4,5,6,7,8,9]
    print(binary_search(a,6))
如何改进上面的代码

我想根据
最大值
(最后一项)、
最小值
(第一项)和
搜索值
,估计要搜索的
项的位置。我上面的代码正在使用
列表的中间部分进行搜索,这将需要更长的时间才能到达


有人能告诉我在哪里可以更改代码并进一步改进吗?

请提供更多信息。你的代码是做什么的?这会引起错误吗?输出是否与您期望的不同?怎么会呢?欢迎来到so。您似乎有一个特定的用例,并为我们设计了一个最小的可复制示例。别误会我,这是件好事!然而,为了给你的问题一个有教育意义的答案,有几件事我们仍然需要知道:列表总是(几乎)排序吗?是否知道列表元素之间的增量?如果这些问题的答案是“否”,那么我想不出一种方法来估计列表中某个元素的位置“估计”是什么意思?像二进制搜索这样的搜索算法的目的是找到准确的位置。如果您的意思是,如果数字是均匀分布的,您希望根据目标的位置进行插值,那么您编写的是插值搜索,而不是二进制搜索。是的,听起来你要找的是插值搜索。
Output: "5"