blob: 64a362eb93152cf3ebfcdb28df49a86cdfd94969 [file] [log] [blame]
Georg Brandl8ec7f652007-08-15 14:28:01 +00001:mod:`bisect` --- Array bisection algorithm
2===========================================
3
4.. module:: bisect
5 :synopsis: Array bisection algorithms for binary searching.
6.. sectionauthor:: Fred L. Drake, Jr. <fdrake@acm.org>
Raymond Hettinger54f824f2010-09-01 19:42:36 +00007.. sectionauthor:: Raymond Hettinger <python at rcn.com>
Georg Brandlb19be572007-12-29 10:57:00 +00008.. example based on the PyModules FAQ entry by Aaron Watters <arw@pythonpros.com>
Georg Brandl8ec7f652007-08-15 14:28:01 +00009
Éric Araujo29a0b572011-08-19 02:14:03 +020010.. versionadded:: 2.1
11
12**Source code:** :source:`Lib/bisect.py`
13
14--------------
15
Georg Brandl8ec7f652007-08-15 14:28:01 +000016This module provides support for maintaining a list in sorted order without
17having to sort the list after each insertion. For long lists of items with
18expensive comparison operations, this can be an improvement over the more common
19approach. The module is called :mod:`bisect` because it uses a basic bisection
20algorithm to do its work. The source code may be most useful as a working
21example of the algorithm (the boundary conditions are already right!).
22
23The following functions are provided:
24
25
Raymond Hettinger54f824f2010-09-01 19:42:36 +000026.. function:: bisect_left(a, x, lo=0, hi=len(a))
Georg Brandl8ec7f652007-08-15 14:28:01 +000027
Raymond Hettinger54f824f2010-09-01 19:42:36 +000028 Locate the insertion point for *x* in *a* to maintain sorted order.
29 The parameters *lo* and *hi* may be used to specify a subset of the list
30 which should be considered; by default the entire list is used. If *x* is
31 already present in *a*, the insertion point will be before (to the left of)
32 any existing entries. The return value is suitable for use as the first
33 parameter to ``list.insert()`` assuming that *a* is already sorted.
Georg Brandl8ec7f652007-08-15 14:28:01 +000034
Raymond Hettinger54f824f2010-09-01 19:42:36 +000035 The returned insertion point *i* partitions the array *a* into two halves so
36 that ``all(val < x for val in a[lo:i])`` for the left side and
37 ``all(val >= x for val in a[i:hi])`` for the right side.
Georg Brandl8ec7f652007-08-15 14:28:01 +000038
Raymond Hettinger54f824f2010-09-01 19:42:36 +000039.. function:: bisect_right(a, x, lo=0, hi=len(a))
40 bisect(a, x, lo=0, hi=len(a))
Georg Brandl8ec7f652007-08-15 14:28:01 +000041
Raymond Hettinger54f824f2010-09-01 19:42:36 +000042 Similar to :func:`bisect_left`, but returns an insertion point which comes
43 after (to the right of) any existing entries of *x* in *a*.
Georg Brandl8ec7f652007-08-15 14:28:01 +000044
Raymond Hettinger54f824f2010-09-01 19:42:36 +000045 The returned insertion point *i* partitions the array *a* into two halves so
46 that ``all(val <= x for val in a[lo:i])`` for the left side and
47 ``all(val > x for val in a[i:hi])`` for the right side.
Georg Brandl8ec7f652007-08-15 14:28:01 +000048
Raymond Hettinger54f824f2010-09-01 19:42:36 +000049.. function:: insort_left(a, x, lo=0, hi=len(a))
Georg Brandl8ec7f652007-08-15 14:28:01 +000050
Raymond Hettinger54f824f2010-09-01 19:42:36 +000051 Insert *x* in *a* in sorted order. This is equivalent to
52 ``a.insert(bisect.bisect_left(a, x, lo, hi), x)`` assuming that *a* is
53 already sorted. Keep in mind that the O(log n) search is dominated by
54 the slow O(n) insertion step.
Georg Brandl8ec7f652007-08-15 14:28:01 +000055
Raymond Hettinger54f824f2010-09-01 19:42:36 +000056.. function:: insort_right(a, x, lo=0, hi=len(a))
Raymond Hettinger47ed1c12010-08-07 21:55:06 +000057 insort(a, x, lo=0, hi=len(a))
Georg Brandl8ec7f652007-08-15 14:28:01 +000058
Raymond Hettinger54f824f2010-09-01 19:42:36 +000059 Similar to :func:`insort_left`, but inserting *x* in *a* after any existing
60 entries of *x*.
Georg Brandl8ec7f652007-08-15 14:28:01 +000061
Raymond Hettinger54f824f2010-09-01 19:42:36 +000062.. seealso::
63
64 `SortedCollection recipe
65 <http://code.activestate.com/recipes/577197-sortedcollection/>`_ that uses
66 bisect to build a full-featured collection class with straight-forward search
67 methods and support for a key-function. The keys are precomputed to save
68 unnecessary calls to the key function during searches.
69
Georg Brandl8ec7f652007-08-15 14:28:01 +000070
Raymond Hettinger47ed1c12010-08-07 21:55:06 +000071Searching Sorted Lists
72----------------------
Georg Brandl8ec7f652007-08-15 14:28:01 +000073
Raymond Hettinger54f824f2010-09-01 19:42:36 +000074The above :func:`bisect` functions are useful for finding insertion points but
75can be tricky or awkward to use for common searching tasks. The following five
Raymond Hettinger47ed1c12010-08-07 21:55:06 +000076functions show how to transform them into the standard lookups for sorted
77lists::
Georg Brandl8ec7f652007-08-15 14:28:01 +000078
Raymond Hettinger54f824f2010-09-01 19:42:36 +000079 def index(a, x):
80 'Locate the leftmost value exactly equal to x'
81 i = bisect_left(a, x)
82 if i != len(a) and a[i] == x:
83 return i
84 raise ValueError
Georg Brandl8ec7f652007-08-15 14:28:01 +000085
Raymond Hettinger54f824f2010-09-01 19:42:36 +000086 def find_lt(a, x):
87 'Find rightmost value less than x'
88 i = bisect_left(a, x)
89 if i:
90 return a[i-1]
91 raise ValueError
92
93 def find_le(a, x):
94 'Find rightmost value less than or equal to x'
95 i = bisect_right(a, x)
96 if i:
97 return a[i-1]
98 raise ValueError
99
100 def find_gt(a, x):
101 'Find leftmost value greater than x'
102 i = bisect_right(a, x)
103 if i != len(a):
Raymond Hettinger47ed1c12010-08-07 21:55:06 +0000104 return a[i]
Raymond Hettinger54f824f2010-09-01 19:42:36 +0000105 raise ValueError
Georg Brandl8ec7f652007-08-15 14:28:01 +0000106
Raymond Hettinger54f824f2010-09-01 19:42:36 +0000107 def find_ge(a, x):
108 'Find leftmost item greater than or equal to x'
109 i = bisect_left(a, x)
110 if i != len(a):
Raymond Hettinger47ed1c12010-08-07 21:55:06 +0000111 return a[i]
Raymond Hettinger54f824f2010-09-01 19:42:36 +0000112 raise ValueError
Raymond Hettinger47ed1c12010-08-07 21:55:06 +0000113
Raymond Hettinger47ed1c12010-08-07 21:55:06 +0000114
115Other Examples
116--------------
Georg Brandl8ec7f652007-08-15 14:28:01 +0000117
118.. _bisect-example:
119
Raymond Hettinger54f824f2010-09-01 19:42:36 +0000120The :func:`bisect` function can be useful for numeric table lookups. This
121example uses :func:`bisect` to look up a letter grade for an exam score (say)
122based on a set of ordered numeric breakpoints: 90 and up is an 'A', 80 to 89 is
123a 'B', and so on::
Georg Brandl8ec7f652007-08-15 14:28:01 +0000124
Raymond Hettinger54f824f2010-09-01 19:42:36 +0000125 >>> def grade(score, breakpoints=[60, 70, 80, 90], grades='FDCBA'):
Raymond Hettinger9aa13952012-04-27 09:55:31 -0700126 i = bisect(breakpoints, score)
127 return grades[i]
128
Raymond Hettinger54f824f2010-09-01 19:42:36 +0000129 >>> [grade(score) for score in [33, 99, 77, 70, 89, 90, 100]]
130 ['F', 'A', 'C', 'C', 'B', 'A', 'A']
Georg Brandl8ec7f652007-08-15 14:28:01 +0000131
Raymond Hettinger87be88c2009-06-11 22:04:00 +0000132Unlike the :func:`sorted` function, it does not make sense for the :func:`bisect`
133functions to have *key* or *reversed* arguments because that would lead to an
Georg Brandl09302282010-10-06 09:32:48 +0000134inefficient design (successive calls to bisect functions would not "remember"
Raymond Hettinger87be88c2009-06-11 22:04:00 +0000135all of the previous key lookups).
Georg Brandl8ec7f652007-08-15 14:28:01 +0000136
Raymond Hettinger87be88c2009-06-11 22:04:00 +0000137Instead, it is better to search a list of precomputed keys to find the index
138of the record in question::
139
140 >>> data = [('red', 5), ('blue', 1), ('yellow', 8), ('black', 0)]
Raymond Hettinger2742e7e2009-06-11 22:08:10 +0000141 >>> data.sort(key=lambda r: r[1])
142 >>> keys = [r[1] for r in data] # precomputed list of keys
Raymond Hettinger87be88c2009-06-11 22:04:00 +0000143 >>> data[bisect_left(keys, 0)]
144 ('black', 0)
145 >>> data[bisect_left(keys, 1)]
146 ('blue', 1)
147 >>> data[bisect_left(keys, 5)]
148 ('red', 5)
149 >>> data[bisect_left(keys, 8)]
150 ('yellow', 8)
Raymond Hettinger47ed1c12010-08-07 21:55:06 +0000151