<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<!--Converted with LaTeX2HTML 98.2 beta6 (August 14th, 1998)
original version by:  Nikos Drakos, CBLU, University of Leeds
* revised and updated by:  Marcus Hennecke, Ross Moore, Herb Swan
* with significant contributions from:
  Jens Lippmann, Marek Rouchal, Martin Wilck and others -->
<HTML>
<HEAD>
<TITLE>5.5 bisect --
Array bisection algorithm</TITLE>
<META NAME="description" CONTENT="5.5 bisect --
Array bisection algorithm">
<META NAME="keywords" CONTENT="lib">
<META NAME="resource-type" CONTENT="document">
<META NAME="distribution" CONTENT="global">
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
<LINK REL="STYLESHEET" HREF="lib.css">
<LINK REL="next" HREF="module-array.html">
<LINK REL="previous" HREF="module-random.html">
<LINK REL="up" HREF="misc.html">
<LINK REL="next" HREF="bisect-example.html">
</HEAD>
<BODY bgcolor="#ffffff">
<div class=navigation>
<table align=center width="100%" cellpadding=0 cellspacing=2>
<tr>
<td><A HREF="bisect-example.html"><img width=32 height=32 align=bottom border=0 alt="next"
 src="../icons/next.gif"></A></td>
<td><A HREF="misc.html"><img width=32 height=32 align=bottom border=0 alt="up"
 src="../icons/up.gif"></A></td>
<td><A HREF="module-random.html"><img width=32 height=32 align=bottom border=0 alt="previous"
 src="../icons/previous.gif"></A></td>
<td align=center bgcolor="#99CCFF" width="100%">
 <b class=title>Python Library Reference</b></td>
<td><A HREF="contents.html"><img width=32 height=32 align=bottom border=0 alt="contents"
 src="../icons/contents.gif"></A></td>
<td><a
 href="modindex.html"><img width=32 height=32 align=bottom border=0 alt="modules"
 src="../icons/modules.gif"></a></td>
<td><A HREF="genindex.html"><img width=32 height=32 align=bottom border=0 alt="index"
 src="../icons/index.gif"></A></td>
</tr></table><b class=navlabel>Next:</b> <span class=sectref><A HREF="bisect-example.html">5.5.1 Example</A></span>
<b class=navlabel>Up:</b> <span class=sectref><A HREF="misc.html">5. Miscellaneous Services</A></span>
<b class=navlabel>Previous:</b> <span class=sectref><A HREF="module-random.html">5.4 random  </A></span>
<br><hr></div>
<!--End of Navigation Panel-->

<H1><A NAME="SECTION007500000000000000000">
5.5 <tt class=module>bisect</tt> --
         Array bisection algorithm</A>
</H1>

<P>


<P>
This module provides support for maintaining a list in sorted order
without having to sort the list after each insertion.  For long lists
of items with expensive comparison operations, this can be an
improvement over the more common approach.  The module is called
<tt class=module>bisect</tt> because it uses a basic bisection algorithm to do its
work.  The source code may be most useful as a working example of the
algorithm (i.e., the boundary conditions are already right!).

<P>
The following functions are provided:

<P>
<dl><dt><b><a name="l2h-774"><tt class=function>bisect</tt></a></b> (<var>list, item</var><big>[</big><var>, lo</var><big>[</big><var>, hi</var><big>]</big><big>]</big>)
<dd>
Locate the proper insertion point for <var>item</var> in <var>list</var> to
maintain sorted order.  The parameters <var>lo</var> and <var>hi</var> may be
used to specify a subset of the list which should be considered.  The
return value is suitable for use as the first parameter to
<tt><var>list</var>.insert()</tt>.
</dl>

<P>
<dl><dt><b><a name="l2h-775"><tt class=function>insort</tt></a></b> (<var>list, item</var><big>[</big><var>, lo</var><big>[</big><var>, hi</var><big>]</big><big>]</big>)
<dd>
Insert <var>item</var> in <var>list</var> in sorted order.  This is equivalent
to <tt><var>list</var>.insert(bisect.bisect(<var>list</var>, <var>item</var>,
<var>lo</var>, <var>hi</var>), <var>item</var>)</tt>.
</dl>

<P>

<p><hr>
<!--Table of Child-Links-->


<UL>
<LI><A NAME="tex2html1700"
 HREF="bisect-example.html">5.5.1 Example</A>
</UL>
<!--End of Table of Child-Links-->
<p>
<div class=navigation><hr><table align=center width="100%" cellpadding=0 cellspacing=2>
<tr>
<td><A HREF="bisect-example.html"><img width=32 height=32 align=bottom border=0 alt="next"
 src="../icons/next.gif"></A></td>
<td><A HREF="misc.html"><img width=32 height=32 align=bottom border=0 alt="up"
 src="../icons/up.gif"></A></td>
<td><A HREF="module-random.html"><img width=32 height=32 align=bottom border=0 alt="previous"
 src="../icons/previous.gif"></A></td>
<td align=center bgcolor="#99CCFF" width="100%">
 <b class=title>Python Library Reference</b></td>
<td><A HREF="contents.html"><img width=32 height=32 align=bottom border=0 alt="contents"
 src="../icons/contents.gif"></A></td>
<td><a
 href="modindex.html"><img width=32 height=32 align=bottom border=0 alt="modules"
 src="../icons/modules.gif"></a></td>
<td><A HREF="genindex.html"><img width=32 height=32 align=bottom border=0 alt="index"
 src="../icons/index.gif"></A></td>
</tr></table><b class=navlabel>Next:</b> <span class=sectref><A HREF="bisect-example.html">5.5.1 Example</A></span>
<b class=navlabel>Up:</b> <span class=sectref><A HREF="misc.html">5. Miscellaneous Services</A></span>
<b class=navlabel>Previous:</b> <span class=sectref><A HREF="module-random.html">5.4 random  </A></span>
</div>
<!--End of Navigation Panel-->
<ADDRESS>
<hr>Send comments on this document to <a href="mailto:python-docs@python.org">python-docs@python.org</a>.
</ADDRESS>
</BODY>
</HTML>
