blob: 61b470baa42a17fd2c4520e0e243d61b452dbb4c [file] [log] [blame]
Georg Brandl116aa622007-08-15 14:28:22 +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>
Christian Heimes5b5e81c2007-12-31 16:14:33 +00007.. example based on the PyModules FAQ entry by Aaron Watters <arw@pythonpros.com>
Georg Brandl116aa622007-08-15 14:28:22 +00008
9This module provides support for maintaining a list in sorted order without
10having to sort the list after each insertion. For long lists of items with
11expensive comparison operations, this can be an improvement over the more common
12approach. The module is called :mod:`bisect` because it uses a basic bisection
13algorithm to do its work. The source code may be most useful as a working
14example of the algorithm (the boundary conditions are already right!).
15
16The following functions are provided:
17
18
Georg Brandl0d8f0732009-04-05 22:20:44 +000019.. function:: bisect_left(a, x, lo=0, hi=len(a))
Georg Brandl116aa622007-08-15 14:28:22 +000020
Georg Brandl0d8f0732009-04-05 22:20:44 +000021 Locate the proper insertion point for *x* in *a* to maintain sorted order.
22 The parameters *lo* and *hi* may be used to specify a subset of the list
23 which should be considered; by default the entire list is used. If *x* is
24 already present in *a*, the insertion point will be before (to the left of)
25 any existing entries. The return value is suitable for use as the first
26 parameter to ``list.insert()``. This assumes that *a* is already sorted.
Georg Brandl116aa622007-08-15 14:28:22 +000027
Georg Brandl116aa622007-08-15 14:28:22 +000028
Georg Brandl0d8f0732009-04-05 22:20:44 +000029.. function:: bisect_right(a, x, lo=0, hi=len(a))
30 bisect(a, x, lo=0, hi=len(a))
Georg Brandl116aa622007-08-15 14:28:22 +000031
Georg Brandl0d8f0732009-04-05 22:20:44 +000032 Similar to :func:`bisect_left`, but returns an insertion point which comes
33 after (to the right of) any existing entries of *x* in *a*.
Georg Brandl116aa622007-08-15 14:28:22 +000034
Georg Brandl116aa622007-08-15 14:28:22 +000035
Georg Brandl0d8f0732009-04-05 22:20:44 +000036.. function:: insort_left(a, x, lo=0, hi=len(a))
Georg Brandl116aa622007-08-15 14:28:22 +000037
Georg Brandl0d8f0732009-04-05 22:20:44 +000038 Insert *x* in *a* in sorted order. This is equivalent to
39 ``a.insert(bisect.bisect_left(a, x, lo, hi), x)``. This assumes that *a* is
40 already sorted.
Georg Brandl116aa622007-08-15 14:28:22 +000041
42
Georg Brandl0d8f0732009-04-05 22:20:44 +000043.. function:: insort_right(a, x, lo=0, hi=len(a))
44 insort(a, x, lo=0, hi=len(a))
Georg Brandl116aa622007-08-15 14:28:22 +000045
Georg Brandl0d8f0732009-04-05 22:20:44 +000046 Similar to :func:`insort_left`, but inserting *x* in *a* after any existing
47 entries of *x*.
Georg Brandl116aa622007-08-15 14:28:22 +000048
49
50Examples
51--------
52
53.. _bisect-example:
54
55The :func:`bisect` function is generally useful for categorizing numeric data.
56This example uses :func:`bisect` to look up a letter grade for an exam total
57(say) based on a set of ordered numeric breakpoints: 85 and up is an 'A', 75..84
Christian Heimesfe337bf2008-03-23 21:54:12 +000058is a 'B', etc.
Georg Brandl116aa622007-08-15 14:28:22 +000059
60 >>> grades = "FEDCBA"
61 >>> breakpoints = [30, 44, 66, 75, 85]
62 >>> from bisect import bisect
63 >>> def grade(total):
64 ... return grades[bisect(breakpoints, total)]
65 ...
66 >>> grade(66)
67 'C'
68 >>> map(grade, [33, 99, 77, 44, 12, 88])
69 ['E', 'A', 'B', 'D', 'F', 'A']
70
71