python的算法:二分法查找(2)--bisect模块
2017-07-04 09:25
597 查看
Python 有一个
先看一个最简单的用法:
两个的结果,都是3
结果分别是1,3
bisect的模块不复杂,现在,我们先看bisect_left这个函数:
这里,假定的,就是,a是一个有序的数组。
假设,我们有这么一个数组:
类似的,不多做描述。
bisect模块,用于维护有序列表。
bisect模块实现了一个算法用于插入元素到有序列表。在一些情况下,这比反复排序列表或构造一个大的列表再排序的效率更高。Bisect 是二分法的意思,这里使用二分法来排序,它会将一个元素插入到一个有序列表的合适位置,这使得不需要每次调用 sort 的方式维护有序列表。
先看一个最简单的用法:
import bisect l=[1,3,3,6,8,12,15] x=4 left_insert_point=bisect.bisect_left(l,x) right_insert_point=bisect.bisect_right(l,x) print(left_insert_point) print(right_insert_point)
两个的结果,都是3
import bisect l=[1,3,3,6,8,12,15] x=3 left_insert_point=bisect.bisect_left(l,x) right_insert_point=bisect.bisect_right(l,x) print(left_insert_point) print(right_insert_point)
结果分别是1,3
bisect的模块不复杂,现在,我们先看bisect_left这个函数:
def bisect_left(a, x, lo=0, hi=None): """Return the index where to insert item x in list a, assuming a is sorted. The return value i is such that all e in a[:i] have e < x, and all e in a[i:] have e >= x. So if x already appears in the list, a.insert(x) will insert just before the leftmost x already there. Optional args lo (default 0) and hi (default len(a)) bound the slice of a to be searched. """ if lo < 0: raise ValueError('lo must be non-negative') if hi is None: hi = len(a) while lo < hi: mid = (lo+hi)//2 if a[mid] < x: lo = mid+1 else: hi = mid return lo
这里,假定的,就是,a是一个有序的数组。
假设,我们有这么一个数组:
p=[1,4,3,3,6,8,12,15,0,9,3,28] 这实际上,p不是有序的e。 根据bisect_left的算法,每次都是二分。如果查找4的插入位置,当找到3的时候,就停止找了。所以,返回是4. 再看bisect_right:
def bisect_right(a, x, lo=0, hi=None): """Return the index where to insert item x in list a, assuming a is sorted. The return value i is such that all e in a[:i] have e <= x, and all e in a[i:] have e > x. So if x already appears in the list, a.insert(x) will insert just after the rightmost x already there. Optional args lo (default 0) and hi (default len(a)) bound the slice of a to be searched. """ if lo < 0: raise ValueError('lo must be non-negative') if hi is None: hi = len(a) while lo < hi: mid = (lo+hi)//2 if x < a[mid]: hi = mid else: lo = mid+1 return lo
类似的,不多做描述。
相关文章推荐
- python的算法:二分法查找(1)
- 基础算法——查找(二分法)(python)
- Python有序查找算法之二分法实例分析
- Python实现二分法算法实例
- [算法][二分法查找]
- 查找算法之二分法查找(JAVA)
- Java学习之数组1(1.数组的声明;2.元素为引用数据类型的数组;3.关于main方法里的String[] args;4.数组排序;5.数3退1 数组算法,(用数组模拟链表);6数组查找之二分法;7数组的拷贝)
- C基础算法之二分法查找
- python快速查找算法应用实例
- 基本算法之二分法查找
- 【基础算法】二分法查找
- python 实现二分法查找
- 二分法查找(Python版)
- 二分法查找数字--算法分析和源码
- java学习-算法2--二分法查找
- python数据结构与算法28 二分查找
- [php] 数据结构&算法(PHP描述) 查找&&二分法查找
- Python 二分法查找
- 基本算法简单实现-二分法查找、合并排序、冒泡排序、插入排序、选择排序、快速排序
- C++ 二分法查找操作 算法(4)