dongzaobei0942 2015-11-30 13:32
浏览 83

去二进制搜索错误

Suppose you have two use cases:

a := [] int {2, 2, 3, 4}
i := sort.Search(len(a), func(pos int) bool { return a[pos] == 2})
fmt.Printf("%v -> %v 
", a, i)

b := [] int {1, 2, 2, 3, 4}
j := sort.Search(len(b), func(pos int) bool { return b[pos] == 2})
fmt.Printf("%v -> %v 
", b, j)

Answer for those are:

[2 2 3 4] -> 4
[1 2 2 3 4] -> 1

I suppose it must be 1 in both cases, no? Does anyone knows why?

  • 写回答

1条回答 默认 最新

  • donglanzhan7151 2015-11-30 13:46
    关注

    sort.Search() returns the smallest index i in [0, n) at which f(i) is true.

    Since slice indices are 0-based, you should expect a return of 0 for the first and 1 for the second. It (seemingly) works for the second.

    Quoting from the doc:

    ...assuming that on the range [0, n), f(i) == true implies f(i+1) == true. That is, Search requires that f is false for some (possibly empty) prefix of the input range [0, n) and then true for the (possibly empty) remainder; Search returns the first true index. If there is no such index, Search returns n.

    Your function does not meet the criteria sort.Search() expects. Your function is not to tell if the element at the specified index is the one you're looking for, but rather to tell:

    • if the element at the requested index is equal to or is greater than (return true)
    • or if the element at the specified index is less than the searched one (return false).

    (A binary search could not be carried out if you would only tell when they are equal but not when they are greater or less.)

    So simply use >= comparison instead of ==. Correct usage:

    a := []int{2, 2, 3, 4}
    i := sort.Search(len(a), func(pos int) bool { return a[pos] >= 2 })
    fmt.Printf("%v -> %v 
    ", a, i)
    
    b := []int{1, 2, 2, 3, 4}
    j := sort.Search(len(b), func(pos int) bool { return b[pos] >= 2 })
    fmt.Printf("%v -> %v 
    ", b, j)
    

    Output (try it on the <kbd>Go Playground</kbd>):

    [2 2 3 4] -> 0 
    [1 2 2 3 4] -> 1 
    

    Note: also from the doc:

    For instance, given a slice data sorted in ascending order, the call

    Search(len(data), func(i int) bool { return data[i] >= 23 })
    

    returns the smallest index i such that data[i] >= 23. If the caller wants to find whether 23 is in the slice, it must test data[i] == 23 separately.

    Easier alternative: sort.SearchInts()

    If you want to search for an int value in an int slice ([]int), simply use sort.SearchInts():

    a := []int{2, 2, 3, 4}
    i := sort.SearchInts(a, 2)
    fmt.Printf("%v -> %v 
    ", a, i)
    
    b := []int{1, 2, 2, 3, 4}
    j := sort.SearchInts(b, 2)
    fmt.Printf("%v -> %v 
    ", b, j)
    

    Output (try it on the <kbd>Go Playground</kbd>):

    [2 2 3 4] -> 0 
    [1 2 2 3 4] -> 1 
    
    评论

报告相同问题?

悬赏问题

  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c