binary search by Python3

Posted 左百工

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了binary search by Python3相关的知识,希望对你有一定的参考价值。

 1 def binary_search(number, array=[]):
 2     """ binary search. """
 3     low = 0
 4     high = len(array) - 1
 5     while low <= high:
 6         mid = (low + high) // 2
 7         guess = array[mid]
 8         if guess == number:
 9             return mid
10         elif guess < number:
11             low = mid + 1
12         else: #guess > number
13             high = mid - 1
14     return None

二分查找法 O(logn)

以上是关于binary search by Python3的主要内容,如果未能解决你的问题,请参考以下文章

109. Convert Sorted List to Binary Search Tree

475. Heaters (start binary search, appplication for binary search)

[Leetcode] Binary search tree --Binary Search Tree Iterator

binary search

[Lintcode]95. Validate Binary Search Tree/[Leetcode]98. Validate Binary Search Tree

[LeetCode] 173. Binary Search Tree Iterator_Medium_tag: Binary Search Tree