zuixime0515

    二分算法采用分而治之的思想,算法思路比较简单,便直接附上一端代码

def binarysearch(ll,x):
    length = len(ll)
    height = length-1
    ini = 0
    
    while ini <= height:
        mid = (ini + height) // 2
        findx = ll[mid]
        
        if findx == x:
            return mid
        if findx < x:
            # 中间数比待找值小
            ini = mid + 1
        if findx > x:
            height = mid - 1
    
    return None

al = [1,2,3,4,5,6,7]
print("is 3 : " + str(binarysearch(al,3)))
print("is 9 : " + str(binarysearch(al,9)))

    上述代码便是一个数组二分查找算法的具实现。

分类:

技术点:

相关文章:

  • 2021-06-26
  • 2021-12-20
  • 2022-12-23
  • 2022-12-23
  • 2021-12-10
  • 2021-05-17
  • 2021-12-15
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2021-12-10
  • 2021-12-10
  • 2021-12-10
相关资源
相似解决方案