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