Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 1 | """Bisection algorithms.""" |
Guido van Rossum | 4e16098 | 1992-09-02 20:43:20 +0000 | [diff] [blame] | 2 | |
Tim Peters | 36cdad1 | 2000-12-29 02:06:45 +0000 | [diff] [blame] | 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. |
| 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 Anand | 96be340 | 2019-04-08 13:31:09 +0530 | [diff] [blame] | 12 | lo = bisect_right(a, x, lo, hi) |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 13 | a.insert(lo, x) |
Guido van Rossum | 4e16098 | 1992-09-02 20:43:20 +0000 | [diff] [blame] | 14 | |
Tim Peters | 36cdad1 | 2000-12-29 02:06:45 +0000 | [diff] [blame] | 15 | def 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 Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 19 | 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 Peters | 36cdad1 | 2000-12-29 02:06:45 +0000 | [diff] [blame] | 21 | |
| 22 | Optional args lo (default 0) and hi (default len(a)) bound the |
| 23 | slice of a to be searched. |
| 24 | """ |
| 25 | |
Georg Brandl | 2ee470f | 2008-07-16 12:55:28 +0000 | [diff] [blame] | 26 | if lo < 0: |
| 27 | raise ValueError('lo must be non-negative') |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 28 | if hi is None: |
| 29 | hi = len(a) |
| 30 | while lo < hi: |
Guido van Rossum | 54e54c6 | 2001-09-04 19:14:14 +0000 | [diff] [blame] | 31 | mid = (lo+hi)//2 |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 32 | if x < a[mid]: hi = mid |
| 33 | else: lo = mid+1 |
| 34 | return lo |
Tim Peters | 36cdad1 | 2000-12-29 02:06:45 +0000 | [diff] [blame] | 35 | |
Tim Peters | 36cdad1 | 2000-12-29 02:06:45 +0000 | [diff] [blame] | 36 | def insort_left(a, x, lo=0, hi=None): |
| 37 | """Insert item x in list a, and keep it sorted assuming a is sorted. |
| 38 | |
| 39 | If x is already in a, insert it to the left of the leftmost x. |
| 40 | |
| 41 | Optional args lo (default 0) and hi (default len(a)) bound the |
| 42 | slice of a to be searched. |
| 43 | """ |
| 44 | |
Chillar Anand | 96be340 | 2019-04-08 13:31:09 +0530 | [diff] [blame] | 45 | lo = bisect_left(a, x, lo, hi) |
Tim Peters | 36cdad1 | 2000-12-29 02:06:45 +0000 | [diff] [blame] | 46 | a.insert(lo, x) |
| 47 | |
| 48 | |
| 49 | def bisect_left(a, x, lo=0, hi=None): |
| 50 | """Return the index where to insert item x in list a, assuming a is sorted. |
| 51 | |
| 52 | The return value i is such that all e in a[:i] have e < x, and all e in |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 53 | a[i:] have e >= x. So if x already appears in the list, a.insert(x) will |
| 54 | insert just before the leftmost x already there. |
Tim Peters | 36cdad1 | 2000-12-29 02:06:45 +0000 | [diff] [blame] | 55 | |
| 56 | Optional args lo (default 0) and hi (default len(a)) bound the |
| 57 | slice of a to be searched. |
| 58 | """ |
| 59 | |
Georg Brandl | 2ee470f | 2008-07-16 12:55:28 +0000 | [diff] [blame] | 60 | if lo < 0: |
| 61 | raise ValueError('lo must be non-negative') |
Tim Peters | 36cdad1 | 2000-12-29 02:06:45 +0000 | [diff] [blame] | 62 | if hi is None: |
| 63 | hi = len(a) |
| 64 | while lo < hi: |
Guido van Rossum | 54e54c6 | 2001-09-04 19:14:14 +0000 | [diff] [blame] | 65 | mid = (lo+hi)//2 |
Tim Peters | 36cdad1 | 2000-12-29 02:06:45 +0000 | [diff] [blame] | 66 | if a[mid] < x: lo = mid+1 |
| 67 | else: hi = mid |
| 68 | return lo |
Raymond Hettinger | 0c41027 | 2004-01-05 10:13:35 +0000 | [diff] [blame] | 69 | |
| 70 | # Overwrite above definitions with a fast C implementation |
| 71 | try: |
Raymond Hettinger | 9dbc670 | 2009-03-31 17:51:51 +0000 | [diff] [blame] | 72 | from _bisect import * |
Brett Cannon | cd171c8 | 2013-07-04 17:43:24 -0400 | [diff] [blame] | 73 | except ImportError: |
Raymond Hettinger | 0c41027 | 2004-01-05 10:13:35 +0000 | [diff] [blame] | 74 | pass |
Victor Stinner | 1018fad | 2016-11-24 23:31:59 +0100 | [diff] [blame] | 75 | |
| 76 | # Create aliases |
| 77 | bisect = bisect_right |
| 78 | insort = insort_right |