Python3 二分查找 bisect 模块

在计算机科学中,二分搜索(英语:binary search),也称折半搜索(英语:half-interval search)[1]、对数搜索(英语:logarithmic search)[2],是一种在有序数组中查找某一特定元素的搜索算法。搜索过程从数组的中间元素开始,如果中间元素正好是要查找的元素,则搜索过程结束;如果某一特定元素大于或者小于中间元素,则在数组大于或小于中间元素的那一半中查找,而且跟开始一样从中间元素开始比较。如果在某一步骤数组为空,则代表找不到。这种搜索算法每一次比较都使搜索范围缩小一半。

Python
import <span class="wp_keywordlink_affiliate"><a href="https://www.168seo.cn/tag/bisect" title="View all posts in bisect" target="_blank">bisect</a></span> l = [1, 3, 3, 6, 8, 12, 15] x = 3 x_insert_point = <span class="wp_keywordlink_affiliate"><a href="https://www.168seo.cn/tag/bisect" title="View all posts in bisect" target="_blank">bisect</a></span>.<span class="wp_keywordlink_affiliate"><a href="https://www.168seo.cn/tag/bisect" title="View all posts in bisect" target="_blank">bisect</a></span>_left(l,3) """在L中查找x,x存在时返回x左侧的位置,x不存在返回应该插入的位置..这是3存在于列表中,返回左侧位置1 """ print(x_insert_point) r_insert_point = bisect.bisect_right(l, x) """在L中查找x,x存在时返回x右侧的位置,x不存在返回应该插入的位置..这是3存在于列表中,返回右侧位置3 """ print(r_insert_point) x_insort_left = bisect.insort_left(l, x) # 将x插入到列表L中,x存在时插入在左侧 print(l) x_insort_rigth = bisect.insort_right(l, x) # 将x插入到列表L中,x存在时插入在右侧 print(l) # /usr/local/bin/<span class="wp_keywordlink_affiliate"><a href="https://www.168seo.cn/tag/python" title="View all posts in python" target="_blank">python</a></span>3 "/Users/songhao/Desktop/Python3 入门和进阶/Python file/d12/c2.py" # 1 # 3 # [1, 3, 3, 3, 6, 8, 12, 15] # [1, 3, 3, 3, 3, 6, 8, 12, 15] <strong>Bisect模块提供的函数有</strong>: (1)查找 ``` bisect.bisect_left(a,x, lo=0, hi=len(a)) : 查找在有序列表a中插入x的index。lo和hi用于指定列表的区间,默认是使用整个列表。 bisect.bisect_right(a,x, lo=0, hi=len(a)) bisect.bisect(a, x,lo=0, hi=len(a)) 这2个和bisect_left类似,但如果x已经存在,在其右边插入。] ``` (2)插入 ``` bisect.insort_left(a,x, lo=0, hi=len(a)) 在有序列表a中插入x。如果x已经存在,在其左边插入。返回值为index。 和a.insert(bisect.bisect_left(a,x, lo, hi), x) 的效果相同。 bisect.insort_right(a,x, lo=0, hi=len(a)) bisect.insort(a, x,lo=0, hi=len(a)) 和insort_left类似,但如果x已经存在,在其右边插入。 ``` 可以函数可以分2类,bisect*,用于查找index。Insort*用于实际插入。默认重复时从右边插入。实际常用的估计是insort。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import bisect
 
l = [ 1 , 3 , 3 , 6 , 8 , 12 , 15 ]
x = 3
 
x_insert_point = bisect . bisect_left ( l , 3 )
"""在L中查找x,x存在时返回x左侧的位置,x不存在返回应该插入的位置..这是3存在于列表中,返回左侧位置1  """
 
print ( x_insert_point )
 
r_insert_point = bisect . bisect_right ( l , x )
"""在L中查找x,x存在时返回x右侧的位置,x不存在返回应该插入的位置..这是3存在于列表中,返回右侧位置3
"""
print ( r_insert_point )
 
x_insort_left = bisect . insort_left ( l , x )    # 将x插入到列表L中,x存在时插入在左侧
print ( l )
 
x_insort_rigth = bisect . insort_right ( l , x )    # 将x插入到列表L中,x存在时插入在右侧
print ( l )
 
 
# /usr/local/bin/python3 "/Users/songhao/Desktop/Python3 入门和进阶/Python file/d12/c2.py"
# 1
# 3
# [1, 3, 3, 3, 6, 8, 12, 15]
# [1, 3, 3, 3, 3, 6, 8, 12, 15]
 
 
 
 
< strong > Bisect模块提供的函数有 < / strong >:
( 1 )查找
` ` `
bisect . bisect_left ( a , x , lo = 0 , hi = len ( a ) ) :
查找在有序列表 a中插入 x的 index。 lo和 hi用于指定列表的区间,默认是使用整个列表。
bisect . bisect_right ( a , x , lo = 0 , hi = len ( a ) )
bisect . bisect ( a , x , lo = 0 , hi = len ( a ) )
这 2个和 bisect _left类似,但如果 x已经存在,在其右边插入。 ]
` ` `
( 2)插入
` ` `
bisect . insort_left ( a , x , lo = 0 , hi = len ( a ) )
在有序列表 a中插入 x。如果 x已经存在,在其左边插入。返回值为 index。 和 a . insert ( bisect . bisect_left ( a , x , lo , hi ) , x ) 的效果相同。
bisect . insort_right ( a , x , lo = 0 , hi = len ( a ) )
bisect . insort ( a , x , lo = 0 , hi = len ( a ) )
和 insort _left类似,但如果 x已经存在,在其右边插入。
` ` `
可以函数可以分 2类, bisect *,用于查找 index。 Insort *用于实际插入。默认重复时从右边插入。实际常用的估计是 insort。

递归方式

Python
def binarySearch(lst, value,low,high): #low,high是lst的查找范围 if high < low: return -1 mid = (low + high)/2 if lst[mid] > value: return binarySearch(lst, value, low, mid-1) elif lst[mid] < value: return binarySearch(lst, value, mid+1, high) else: return mid
1
2
3
4
5
6
7
8
9
10
11
def binarySearch ( lst , value , low , high ) :            #low,high是lst的查找范围
     if high < low :
         return - 1
     mid = ( low + high ) / 2
     if lst [ mid ] > value :
         return binarySearch ( lst , value , low , mid - 1 )
     elif lst [ mid ] < value :
         return binarySearch ( lst , value , mid + 1 , high )
     else :
         return mid
 

采用循环

Python
def bsearch(l, value): lo, hi = 0, len(l)-1 while lo <= hi: mid = (lo + hi) / 2 if l[mid] < value: lo = mid + 1 elif value < l[mid]: hi = mid - 1 else: return mid return -1
1
2
3
4
5
6
7
8
9
10
11
12
def bsearch ( l , value ) :
     lo , hi = 0 , len ( l ) - 1
     while lo <= hi :
         mid = ( lo + hi ) / 2
         if l [ mid ] < value :
             lo = mid + 1
         elif value < l [ mid ] :
             hi = mid - 1
         else :
             return mid
     return - 1
 



  • zeropython 微信公众号 5868037 QQ号 5868037@qq.com QQ邮箱
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值