Staging
v0.5.1
https://github.com/python/cpython
Revision c25296a91656edd01df7c9f7e25955f119c6bb09 authored by Fred Drake on 14 October 2000, 05:41:17 UTC, committed by Fred Drake on 14 October 2000, 05:41:17 UTC
Convert from a howto to a manual, so we can *have* chapters!
Comment out the macconsole module documentation; Think C seems to have
disappeared.
1 parent c7babad
Raw File
Tip revision: c25296a91656edd01df7c9f7e25955f119c6bb09 authored by Fred Drake on 14 October 2000, 05:41:17 UTC
Update to include all the new chapters & sections.
Tip revision: c25296a
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