blob: 8f3f6a3fe35ffea711796e6f7c2a19f5add1342d [file] [log] [blame]
Guido van Rossum4acc25b2000-02-02 15:10:15 +00001"""Bisection algorithms."""
Guido van Rossum4e160981992-09-02 20:43:20 +00002
Tim Peters36cdad12000-12-29 02:06:45 +00003def insort_right(a, x, lo=0, hi=None):
4 """Insert item x in list a, and keep it sorted assuming a is sorted.
5
6 If x is already in a, insert it to the right of the rightmost x.
7
8 Optional args lo (default 0) and hi (default len(a)) bound the
9 slice of a to be searched.
10 """
11
Chillar Anand96be3402019-04-08 13:31:09 +053012 lo = bisect_right(a, x, lo, hi)
Guido van Rossum4acc25b2000-02-02 15:10:15 +000013 a.insert(lo, x)
Guido van Rossum4e160981992-09-02 20:43:20 +000014
Tim Peters36cdad12000-12-29 02:06:45 +000015def bisect_right(a, x, lo=0, hi=None):
16 """Return the index where to insert item x in list a, assuming a is sorted.
17
18 The return value i is such that all e in a[:i] have e <= x, and all e in
Guido van Rossumd8faa362007-04-27 19:54:29 +000019 a[i:] have e > x. So if x already appears in the list, a.insert(x) will
20 insert just after the rightmost x already there.
Tim Peters36cdad12000-12-29 02:06:45 +000021
22 Optional args lo (default 0) and hi (default len(a)) bound the
23 slice of a to be searched.
24 """
25
Georg Brandl2ee470f2008-07-16 12:55:28 +000026 if lo < 0:
27 raise ValueError('lo must be non-negative')
Guido van Rossum4acc25b2000-02-02 15:10:15 +000028 if hi is None:
29 hi = len(a)
30 while lo < hi:
Guido van Rossum54e54c62001-09-04 19:14:14 +000031 mid = (lo+hi)//2
Raymond Hettinger3c881992019-10-28 21:38:50 -070032 # Use __lt__ to match the logic in list.sort() and in heapq
Guido van Rossum4acc25b2000-02-02 15:10:15 +000033 if x < a[mid]: hi = mid
34 else: lo = mid+1
35 return lo
Tim Peters36cdad12000-12-29 02:06:45 +000036
Tim Peters36cdad12000-12-29 02:06:45 +000037def insort_left(a, x, lo=0, hi=None):
38 """Insert item x in list a, and keep it sorted assuming a is sorted.
39
40 If x is already in a, insert it to the left of the leftmost x.
41
42 Optional args lo (default 0) and hi (default len(a)) bound the
43 slice of a to be searched.
44 """
45
Chillar Anand96be3402019-04-08 13:31:09 +053046 lo = bisect_left(a, x, lo, hi)
Tim Peters36cdad12000-12-29 02:06:45 +000047 a.insert(lo, x)
48
49
50def bisect_left(a, x, lo=0, hi=None):
51 """Return the index where to insert item x in list a, assuming a is sorted.
52
53 The return value i is such that all e in a[:i] have e < x, and all e in
Guido van Rossumd8faa362007-04-27 19:54:29 +000054 a[i:] have e >= x. So if x already appears in the list, a.insert(x) will
55 insert just before the leftmost x already there.
Tim Peters36cdad12000-12-29 02:06:45 +000056
57 Optional args lo (default 0) and hi (default len(a)) bound the
58 slice of a to be searched.
59 """
60
Georg Brandl2ee470f2008-07-16 12:55:28 +000061 if lo < 0:
62 raise ValueError('lo must be non-negative')
Tim Peters36cdad12000-12-29 02:06:45 +000063 if hi is None:
64 hi = len(a)
65 while lo < hi:
Guido van Rossum54e54c62001-09-04 19:14:14 +000066 mid = (lo+hi)//2
Raymond Hettinger3c881992019-10-28 21:38:50 -070067 # Use __lt__ to match the logic in list.sort() and in heapq
Tim Peters36cdad12000-12-29 02:06:45 +000068 if a[mid] < x: lo = mid+1
69 else: hi = mid
70 return lo
Raymond Hettinger0c410272004-01-05 10:13:35 +000071
72# Overwrite above definitions with a fast C implementation
73try:
Raymond Hettinger9dbc6702009-03-31 17:51:51 +000074 from _bisect import *
Brett Cannoncd171c82013-07-04 17:43:24 -040075except ImportError:
Raymond Hettinger0c410272004-01-05 10:13:35 +000076 pass
Victor Stinner1018fad2016-11-24 23:31:59 +010077
78# Create aliases
79bisect = bisect_right
80insort = insort_right