Staging
v0.5.1
https://github.com/python/cpython
Raw File
Tip revision: e2aaa9dd61632e48da08372dfb7f3365ed6d663b authored by cvs2svn on 14 February 1995, 00:58:59 UTC
This commit was manufactured by cvs2svn to create tag 'r12beta3'.
Tip revision: e2aaa9d
bisect.py
# Bisection algorithms


# Insert item x in list a, and keep it sorted assuming a is sorted

def insort(a, x):
        lo, hi = 0, len(a)
        while lo < hi:
		mid = (lo+hi)/2
		if x < a[mid]: hi = mid
		else: lo = mid+1
	a.insert(lo, x)


# Find the index where to insert item x in list a, assuming a is sorted

def bisect(a, x):
        lo, hi = 0, len(a)
        while lo < hi:
		mid = (lo+hi)/2
		if x < a[mid]: hi = mid
		else: lo = mid+1
	return lo
back to top