如何对相同值的范围进行二进制搜索?

时间:2015-06-12 02:40:07

标签: python binary-search

我有一个排序的数字列表,我需要让它返回数字出现的索引范围。我的名单是:

daysSick = [0, 0, 0, 0, 1, 2, 3, 3, 3, 4, 5, 5, 5, 6, 6, 11, 15, 24]

如果我搜索0,我需要返回(0,3)。现在我只能找到一个数字的位置!我知道如何进行二进制搜索,但是我被困在如何让它从位置上下移动以找到其他相同的值!

low = 0
high = len(daysSick) - 1
while low <= high :
    mid = (low + high) // 2
    if value < daysSick[mid]:
        high = mid - 1
    elif value > list[mid]:
        low = mid + 1
    else:
        return mid

3 个答案:

答案 0 :(得分:4)

为什么不使用python's bisection routines

>>> daysSick = [0, 0, 0, 0, 1, 2, 3, 3, 3, 4, 5, 5, 5, 6, 6, 11, 15, 24]
>>> from bisect import bisect_left, bisect_right
>>> bisect_left(daysSick, 3)
6
>>> bisect_right(daysSick, 3)
9
>>> daysSick[6:9]
[3, 3, 3]

答案 1 :(得分:3)

我提供的解决方案比bisect库中的raw functions taken更快

解决方案

使用优化二进制搜索

def search(a, x):
    right = 0
    h = len(a)
    while right < h:
        m = (right+h)//2
        if x < a[m]: h = m
        else: 
            right = m+1
    # start binary search for left element only 
    # including elements from 0 to right-1 - much faster!
    left = 0
    h = right - 1
    while left < h:
        m = (left+h)//2
        if x > a[m]: left = m+1
        else: 
            h = m
    return left, right-1

search(daysSick, 5)
(10, 12)

search(daysSick, 2)
(5, 5)

比较与Bisect

  • 使用自定义二进制搜索...

    %timeit search(daysSick, 3)
    1000000 loops, best of 3: 1.23 µs per loop
    
  • 将源代码从bisect复制到python ...

    %timeit bisect_left(daysSick, 1), bisect_right(daysSick, 1)
    1000000 loops, best of 3: 1.77 µs per loop
    
  • 使用默认导入是最快的,因为我认为它可能会在幕后进行优化......

    from bisect import bisect_left, bisect_right
    %timeit bisect_left(daysSick, 1), bisect_right(daysSick, 1)
    1000000 loops, best of 3: 504 ns per loop
    

附加

没有分机。库但不是二进制搜索

daysSick = [0, 0, 0, 0, 1, 2, 3, 3, 3, 4, 5, 5, 5, 6, 6, 11, 15, 24]

# using a function
idxL = lambda val, lst:  [i for i,d in enumerate(lst) if d==val]

allVals = idxL(0,daysSick)
(0, 3)

答案 2 :(得分:2)

好的,这是另一种方法,通过尝试在已经减少的范围的一半上执行bisect_leftbisect_right之前先缩小范围。我编写此代码是因为我认为稍微比调用bisect_leftbisect_right更有效率,即使它具有相同的时间复杂度。

def binary_range_search(s, x):
    # First we will reduce the low..high range if possible
    # by using symmetric binary search to find an index pointing to x
    low, high = 0, len(s)
    while True:
        if low >= high:
            return None
        mid = (low + high) // 2
        mid_element = s[mid]
        if x == mid_element:
            break
        elif x < mid_element:
            high = mid
        else:
            low = mid + 1
    xindex = mid

    # Now we have found an index pointing to x called xindex
    # and potentially reduced the low..high range
    # now we can run bisect_left on low..xindex + 1

    lo, hi = low, xindex + 1
    while lo < hi:
        mid = (lo+hi)//2
        if x > s[mid]: lo = mid+1
        else: hi = mid
    first = lo

    # and also bisect_right on xindex..high

    lo, hi = xindex, high
    while lo < hi:
        mid = (lo+hi)//2
        if x < s[mid]: hi = mid
        else: lo = mid+1
    last = lo - 1

    return first, last

我认为时间复杂度是O(log n),就像琐碎的解决方案一样,但我相信无论如何这都会更有效率。我认为值得注意的是,您可以为大型数据集并行化bisect_leftbisect_right的第二部分,因为它们是不相互作用的独立操作。