1
"""Bisection algorithms."""
3
def insort_right(a, x, lo=0, hi=None):
4
"""Insert item x in list a, and keep it sorted assuming a is sorted.
6
If x is already in a, insert it to the right of the rightmost x.
8
Optional args lo (default 0) and hi (default len(a)) bound the
9
slice of a to be searched.
16
if x < a[mid]: hi = mid
20
insort = insort_right # backward compatibility
22
def bisect_right(a, x, lo=0, hi=None):
23
"""Return the index where to insert item x in list a, assuming a is sorted.
25
The return value i is such that all e in a[:i] have e <= x, and all e in
26
a[i:] have e > x. So if x already appears in the list, i points just
27
beyond the rightmost x already there.
29
Optional args lo (default 0) and hi (default len(a)) bound the
30
slice of a to be searched.
37
if x < a[mid]: hi = mid
41
bisect = bisect_right # backward compatibility
43
def insort_left(a, x, lo=0, hi=None):
44
"""Insert item x in list a, and keep it sorted assuming a is sorted.
46
If x is already in a, insert it to the left of the leftmost x.
48
Optional args lo (default 0) and hi (default len(a)) bound the
49
slice of a to be searched.
56
if a[mid] < x: lo = mid+1
61
def bisect_left(a, x, lo=0, hi=None):
62
"""Return the index where to insert item x in list a, assuming a is sorted.
64
The return value i is such that all e in a[:i] have e < x, and all e in
65
a[i:] have e >= x. So if x already appears in the list, i points just
66
before the leftmost x already there.
68
Optional args lo (default 0) and hi (default len(a)) bound the
69
slice of a to be searched.
76
if a[mid] < x: lo = mid+1
80
# Overwrite above definitions with a fast C implementation
82
from _bisect import bisect_right, bisect_left, insort_left, insort_right, insort, bisect