【问题标题】:Ternary Search just like Binary search but dividing items by 3三元搜索就像二分搜索一样,但将项目除以 3
【发布时间】:2013-03-12 02:26:01
【问题描述】:

下面有一个名为“test”的函数。我的程序无法通过测试功能。

这是我的三元搜索代码。三元搜索类似于二分搜索,但不是将所有元素除以二,而是将它们除以三。

为了使用三元搜索,我使用 index2 作为 1/3 项目的分隔符。 index1 是 2/3 项的分隔符。

您只需将“高”和“低”分配给 index1 或 index2。这使您可以将列表分为三个部分。 High 和 Low 用于查找您应该搜索的划分列表的哪一部分。然后这个过程不断重复,直到high和low的值接近。

seq 是列表中的项目,即。 [1,2,3,4,5...] 列表中的项目是有序的。

key 是我正在寻找的值

并且ternary_search返回键的索引或接近键的数字的索引

玩得开心! 干杯!

def ternary_search(seq,key):
    length = len(seq)
    left = 0
    right = length
    index = 0
    x = True
    while x and left <= right:
        #focal = (high + low) //3

        if left == right:
            #check similarity between values and key
            return index
        else:
            if right - left > 0:
                index1 = ((right+2*(left))//3)
                index2 = ((2*(right)+left)//3)
                if left == right:
                    x = False
                    return (index1+index2)
                if seq[index1] == key:
                    x = False
                    return index1
                if seq[index2]== key:
                    x = False
                    return index2
                if key<seq[index1]:
                        right = index1 - 1
                else:
                    if key > seq[index1] and key <seq[index2]:
                        right = index2 - 1
                        left = index1 - 1
                    if key > seq[index2]:
                        left = index2+1

    return index

def test():
    seq = []
    for i in range(1,1001):
        seq.append(i)
    for j in range(1,1001):
        is_pass = (ternary_search(seq,j)==j-1)
        assert is_pass == True, "fail the test when key is %d"%j
    if is_pass == True:
        print("=========== Congratulations! Your have finished exercise 2! ============")
if __name__ == '__main__':
    test()

【问题讨论】:

  • 如果列表中有两个项目,它可以工作,但如果有三个或更多,我会出错
  • 显示输入示例,以及您得到的错误。
  • def test() 如果你运行这个程序,它会自动给你输入和错误。它在页面的底部
  • 这是一个相当老的问题,但是阅读答案后,我看到很多人声称三元搜索很可能比二分搜索慢,而且确实如此直观。事实证明,事实并非如此。因为数组的长度是 2 的幂是非常常见的,并且由于 CPU 将地址映射到缓存行的方式,二进制搜索算法实际上是 CPU 缓存的病态案例,正如这篇很棒的博客文章所解释的那样:@987654321 @

标签: python python-3.x binary-search ternary-search


【解决方案1】:

你的错误是:

if left == right:
#check similarity between values and key
return index 

基本上现在当你的上下值(右和左)相遇时,它们将返回存储在变量索引中的值,但是在你的函数体中你永远不会改变索引的值,所以它总是返回 0。一使您的代码工作的方法是,一旦您知道 left==right 检查并查看 value==key 是否存在,则返回左侧或右侧,因为两者都必须是该值的索引。我用你的代码做了这个,它通过了测试功能。顺便说一句,实验室 8 祝你好运。

【讨论】:

  • 用户1789376。非常感谢您一直在努力寻找问题。
【解决方案2】:
def ternary_search(seq,key):
  length = len(seq)
  left = 0
  right = length
  index = 0
  x = True
  while x and left <= right:
    #focal = (high + low) //3
    if left == right:
        #check similarity between values and key
        return left 

    elif right - left > 0:
        index1 = ((right+2*(left))//3)
        index2 = ((2*(right)+left)//3)
        if seq[index1] == key:
            return index1
        elif seq[index2] == key:
            return index2
        else:
            if key<seq[index1]:
                    right = index1 - 1
            elif key > seq[index1] and key <seq[index2]:
                    right = index2 - 1
                    left = index1 - 1
            elif key > seq[index2]:
                    left = index2+1

return index

顺便说一下,实验室 8 祝你好运。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-26
    • 1970-01-01
    相关资源
    最近更新 更多