码迷,mamicode.com
首页 > 编程语言 > 详细

binary search by Python3

时间:2017-11-28 15:43:23      阅读:147      评论:0      收藏:0      [点我收藏+]

标签:size   arch   pytho   binary   div   二分   span   log   color   

 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

标签:size   arch   pytho   binary   div   二分   span   log   color   

原文地址:http://www.cnblogs.com/zuosy/p/7909834.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!