算法图解书籍第一章算法简介读书笔记

  1. 二分查找算法的复杂度最多为: \log _{2}n   对数是幂运算的逆运算
  2. 仅当列表是有序的时候,二分查找才管用.
  3. 算法的速度指的并非时间,而是操作数的增速
  • 二分查找法python版本
  • def binary_search(list,  item):
        low = 0
        high = len(list)-1
        while low <= high:
            # mid = int((low + high)/2)
            mid = (low + high)//2
            guess = list[mid]
            if guess == item:
                return mid
            if guess > item:
                high = mid -1
            else:
                low = mid + 1
        return None
    my_list = [1, 3, 5, 7, 9]
    print(binary_search(my_list, 3))
    print(binary_search(my_list, -1))

     

你可能感兴趣的:(python,算法,算法图解笔记,算法,python)