python实现二分查找和四种变形

# -*- coding: utf-8 -*-


def binary_search_rec(search_list, item):
    """二分查找, 递归"""
    lens = len(search_list)
    if lens > 0:
        mid = lens // 2
        if search_list[mid] == item:
            return True
        elif search_list[mid] > item:
            return binary_search_rec(search_list[:mid], item)
        else:
            return binary_search_rec(search_list[mid+1:], item)
    return False


def binary_search(search_list, item):
    """二分查找, 非递归"""
    lens = len(search_list)
    start = 0
    end = lens-1
    while start <= end:
        mid = (start + end) // 2
        if search_list[mid] == item:
            return True
        elif search_list[mid] > item:
            end = mid - 1
        else:
            start = mid + 1
    return False


def binary_search_firn(search_list, item):
    """二分查找, 查找第一个值等于给定值的元素"""
    lens = len(search_list)
    start = 0
    end = lens-1
    while start <= end:
        mid = (start + end) // 2
        if search_list[mid] == item:
            if mid == 0 or search_list[mid-1] != item:
                return mid
            else:
                end = mid - 1
        elif search_list[mid] > item:
            end = mid - 1
        else:
            start = mid + 1
    return False


def binary_search_lastn(search_list, item):
    """二分查找, 查找最后一个值等于给定值的元素"""
    lens = len(search_list)
    start = 0
    end = lens-1
    while start <= end:
        mid = (start + end) // 2
        if search_list[mid] == item:
            if mid == lens-1 or search_list[mid+1] != item:
                return mid
            else:
                start = mid + 1
        elif search_list[mid] > item:
            end = mid - 1
        else:
            start = mid + 1
    return False


def binary_search_fir(search_list, item):
    """二分查找, 查找第一个大于等于给定值的元素"""
    lens = len(search_list)
    start = 0
    end = lens-1
    while start <= end:
        mid = (start + end) // 2
        if search_list[mid] >= item:
            if mid == 0 or search_list[mid-1] < item:
                return mid
            end = mid - 1
        else:
            start = mid + 1
    return False


def binary_search_last(search_list, item):
    """二分查找, 查找最后一个小于等于给定值的元素 """
    lens = len(search_list)
    start = 0
    end = lens-1
    while start <= end:
        mid = (start + end) // 2
        if search_list[mid] <= item:
            if mid == lens - 1 or search_list[mid+1] > item:
                return mid
            start = mid + 1
        else:
            end = mid - 1
    return False


if __name__ == '__main__':
    li = [2, 5, 9, 11, 20, 26, 29, 32, 38, 41, 47]
    print(binary_search(li, 30))  # false
    print(binary_search(li, 29))  # ture
    print(binary_search_rec(li, 2))  # ture
    print(binary_search_rec(li, 10))  # false

    li1 = [2, 5, 5, 11, 20, 20, 20, 32, 38, 41, 47]

    print(binary_search(li1, 20))  # 5
    print(binary_search_firn(li1, 20))  # 4
    print(binary_search_lastn(li1, 20))  # 6

    print(binary_search_fir(li1, 1))  # 0
    print(binary_search_fir(li1, 21))  # 7
    print(binary_search_fir(li1, 40))  # 9

    print(binary_search_last(li1, 3))  # 0
    print(binary_search_last(li1, 12))  # 3
    print(binary_search_last(li1, 48))  # 10
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值