Staging
v0.5.1
https://github.com/python/cpython
Revision 90f0e07a5b149869460663e2734eb1186fc56900 authored by Guido van Rossum on 30 January 1995, 12:53:06 UTC, committed by Guido van Rossum on 30 January 1995, 12:53:06 UTC
1 parent 42a5124
Raw File
Tip revision: 90f0e07a5b149869460663e2734eb1186fc56900 authored by Guido van Rossum on 30 January 1995, 12:53:06 UTC
fix glaring bug in get_magic
Tip revision: 90f0e07
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