二分搜索,lower_bound,upper_bound

二分搜索,lower_bound,upper_bound

二分搜索

写法1

def binary_search(nums, target):
    l = 0
    r = len(nums)

    while l < r:
        m = (l + r) // 2
        if nums[m] == target:
            return m
        elif nums[m] < target:
            l = m + 1
        else:
            r = m

    return -1

写法2

def binary_search2(nums, target):
    l = 0
    r = len(nums) - 1

    while l <= r:
        m = (l + r) // 2
        if nums[m] == target:
            return m
        elif nums[m] > target:
            r = m-1
        else:
            l = m+1

    return -1

递归

# [begin, end]
def binary_search_rec(nums, begin, end, target):
    if begin > end:
        return -1

    m = (begin + end) // 2
    if nums[m] == target:
        return m
    elif nums[m] > target:
        return binary_search_rec(nums, begin, m-1, target)
    else:
        return binary_search_rec(nums, m+1, end, target)

lower_bound

# first position >= target
def lower_bound2(nums, target):
    begin = 0
    end = len(nums)

    while begin < end:
        mid = (begin + end) // 2
        if nums[mid] < target:
            begin = mid + 1
        else:
            end = mid

    return begin

upper_bound

# first position > target
def upper_bound2(nums, target):
    begin = 0
    end = len(nums)

    while begin < end:
        mid = (begin + end) // 2
        if nums[mid] <= target:
            begin = mid + 1
        else:
            end = mid

    return begin

测试

def test_binary_search(num, maxnum, times):
    nums = []
    for _ in range(num):
        nums.append(random.randint(0, maxnum))

    nums.sort()
    for _ in range(times):
        a = random.randint(0, maxnum)
        i = nums.index(a) if a in nums else -1
        j = binary_search(nums, a)
        if i != j and nums[j] != a:
            print(a, i, j)
            raise Exception('Error search')

        k = binary_search2(nums, a)
        if k != j and nums[k] != a:
            print(a, j, k)
            raise Exception('Error search2')

        l = binary_search_rec(nums, 0, len(nums)-1, a)
        if l != k and nums[l] != a:
            print(a, k, l)
            raise Exception('Error search_rec')


def test_lower_bound(num, maxnum, times):
    nums = []
    for _ in range(num):
        nums.append(random.randint(0, maxnum))

    nums.sort()
    for _ in range(times):
        a = random.randint(0, maxnum)
        i = lower_bound(nums, a)
        j = bisect.bisect_left(nums, a)
        k = lower_bound2(nums, a)
        if i != j:
            raise Exception('Error lower bound')

        if k != j:
            raise Exception('Error lower bound2')


def test_upper_bound(num, maxnum, times):
    nums = []
    for _ in range(num):
        nums.append(random.randint(0, maxnum))

    nums.sort()
    for _ in range(times):
        a = random.randint(0, maxnum)
        i = upper_bound(nums, a)
        j = bisect.bisect_right(nums, a)
        k = upper_bound2(nums, a)
        if i != j:
            raise Exception('Error upper bound')

        if k != j:
            raise Exception('Error upper bound2')


if __name__ == '__main__':
    test_binary_search(10000, 15000, 10000)
    test_lower_bound(10000, 1500, 10000)
    test_upper_bound(10000, 1500, 10000)

    test_lower_bound(10000, 15000, 10000)
    test_upper_bound(10000, 15000, 10000)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值