Staging
v0.5.1
https://github.com/python/cpython
Raw File
Tip revision: a222759312e0c8da11e4a0c5f48c5ad9d841f5ab authored by cvs2svn on 22 June 2001, 06:43:01 UTC
This commit was manufactured by cvs2svn to create tag 'r201'.
Tip revision: a222759
bisect.py
"""Bisection algorithms."""


def insort(a, x, lo=0, hi=None):
    """Insert item x in list a, and keep it sorted assuming a is sorted."""
    if hi is None:
        hi = len(a)
    while lo < hi:
        mid = (lo+hi)/2
        if x < a[mid]: hi = mid
        else: lo = mid+1
    a.insert(lo, x)


def bisect(a, x, lo=0, hi=None):
    """Find the index where to insert item x in list a, assuming a is sorted."""
    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
back to top