|
1 | 1 | """
|
2 |
| -This is pure python implementation of binary search algorithm |
| 2 | +This is pure python implementation of binary search algorithms |
3 | 3 |
|
4 | 4 | For doctests run following command:
|
5 | 5 | python -m doctest -v binary_search.py
|
|
12 | 12 | import bisect
|
13 | 13 |
|
14 | 14 |
|
| 15 | +def bisect_left(sorted_collection, item, lo=0, hi=None): |
| 16 | + """ |
| 17 | + Locates the first element in a sorted array that is larger or equal to a given value. |
| 18 | +
|
| 19 | + It has the same interface as https://docs.python.org/3/library/bisect.html#bisect.bisect_left . |
| 20 | +
|
| 21 | + :param sorted_collection: some ascending sorted collection with comparable items |
| 22 | + :param item: item to bisect |
| 23 | + :param lo: lowest index to consider (as in sorted_collection[lo:hi]) |
| 24 | + :param hi: past the highest index to consider (as in sorted_collection[lo:hi]) |
| 25 | + :return: index i such that all values in sorted_collection[lo:i] are < item and all values in sorted_collection[i:hi] are >= item. |
| 26 | +
|
| 27 | + Examples: |
| 28 | + >>> bisect_left([0, 5, 7, 10, 15], 0) |
| 29 | + 0 |
| 30 | +
|
| 31 | + >>> bisect_left([0, 5, 7, 10, 15], 6) |
| 32 | + 2 |
| 33 | +
|
| 34 | + >>> bisect_left([0, 5, 7, 10, 15], 20) |
| 35 | + 5 |
| 36 | +
|
| 37 | + >>> bisect_left([0, 5, 7, 10, 15], 15, 1, 3) |
| 38 | + 3 |
| 39 | +
|
| 40 | + >>> bisect_left([0, 5, 7, 10, 15], 6, 2) |
| 41 | + 2 |
| 42 | + """ |
| 43 | + if hi is None: |
| 44 | + hi = len(sorted_collection) |
| 45 | + |
| 46 | + while lo < hi: |
| 47 | + mid = (lo + hi) // 2 |
| 48 | + if sorted_collection[mid] < item: |
| 49 | + lo = mid + 1 |
| 50 | + else: |
| 51 | + hi = mid |
| 52 | + |
| 53 | + return lo |
| 54 | + |
| 55 | + |
| 56 | +def bisect_right(sorted_collection, item, lo=0, hi=None): |
| 57 | + """ |
| 58 | + Locates the first element in a sorted array that is larger than a given value. |
| 59 | +
|
| 60 | + It has the same interface as https://docs.python.org/3/library/bisect.html#bisect.bisect_right . |
| 61 | +
|
| 62 | + :param sorted_collection: some ascending sorted collection with comparable items |
| 63 | + :param item: item to bisect |
| 64 | + :param lo: lowest index to consider (as in sorted_collection[lo:hi]) |
| 65 | + :param hi: past the highest index to consider (as in sorted_collection[lo:hi]) |
| 66 | + :return: index i such that all values in sorted_collection[lo:i] are <= item and all values in sorted_collection[i:hi] are > item. |
| 67 | +
|
| 68 | + Examples: |
| 69 | + >>> bisect_right([0, 5, 7, 10, 15], 0) |
| 70 | + 1 |
| 71 | +
|
| 72 | + >>> bisect_right([0, 5, 7, 10, 15], 15) |
| 73 | + 5 |
| 74 | +
|
| 75 | + >>> bisect_right([0, 5, 7, 10, 15], 6) |
| 76 | + 2 |
| 77 | +
|
| 78 | + >>> bisect_right([0, 5, 7, 10, 15], 15, 1, 3) |
| 79 | + 3 |
| 80 | +
|
| 81 | + >>> bisect_right([0, 5, 7, 10, 15], 6, 2) |
| 82 | + 2 |
| 83 | + """ |
| 84 | + if hi is None: |
| 85 | + hi = len(sorted_collection) |
| 86 | + |
| 87 | + while lo < hi: |
| 88 | + mid = (lo + hi) // 2 |
| 89 | + if sorted_collection[mid] <= item: |
| 90 | + lo = mid + 1 |
| 91 | + else: |
| 92 | + hi = mid |
| 93 | + |
| 94 | + return lo |
| 95 | + |
| 96 | + |
| 97 | +def insort_left(sorted_collection, item, lo=0, hi=None): |
| 98 | + """ |
| 99 | + Inserts a given value into a sorted array before other values with the same value. |
| 100 | +
|
| 101 | + It has the same interface as https://docs.python.org/3/library/bisect.html#bisect.insort_left . |
| 102 | +
|
| 103 | + :param sorted_collection: some ascending sorted collection with comparable items |
| 104 | + :param item: item to insert |
| 105 | + :param lo: lowest index to consider (as in sorted_collection[lo:hi]) |
| 106 | + :param hi: past the highest index to consider (as in sorted_collection[lo:hi]) |
| 107 | +
|
| 108 | + Examples: |
| 109 | + >>> sorted_collection = [0, 5, 7, 10, 15] |
| 110 | + >>> insort_left(sorted_collection, 6) |
| 111 | + >>> sorted_collection |
| 112 | + [0, 5, 6, 7, 10, 15] |
| 113 | +
|
| 114 | + >>> sorted_collection = [(0, 0), (5, 5), (7, 7), (10, 10), (15, 15)] |
| 115 | + >>> item = (5, 5) |
| 116 | + >>> insort_left(sorted_collection, item) |
| 117 | + >>> sorted_collection |
| 118 | + [(0, 0), (5, 5), (5, 5), (7, 7), (10, 10), (15, 15)] |
| 119 | + >>> item is sorted_collection[1] |
| 120 | + True |
| 121 | + >>> item is sorted_collection[2] |
| 122 | + False |
| 123 | +
|
| 124 | + >>> sorted_collection = [0, 5, 7, 10, 15] |
| 125 | + >>> insort_left(sorted_collection, 20) |
| 126 | + >>> sorted_collection |
| 127 | + [0, 5, 7, 10, 15, 20] |
| 128 | +
|
| 129 | + >>> sorted_collection = [0, 5, 7, 10, 15] |
| 130 | + >>> insort_left(sorted_collection, 15, 1, 3) |
| 131 | + >>> sorted_collection |
| 132 | + [0, 5, 7, 15, 10, 15] |
| 133 | + """ |
| 134 | + sorted_collection.insert(bisect_left(sorted_collection, item, lo, hi), item) |
| 135 | + |
| 136 | + |
| 137 | +def insort_right(sorted_collection, item, lo=0, hi=None): |
| 138 | + """ |
| 139 | + Inserts a given value into a sorted array after other values with the same value. |
| 140 | +
|
| 141 | + It has the same interface as https://docs.python.org/3/library/bisect.html#bisect.insort_right . |
| 142 | +
|
| 143 | + :param sorted_collection: some ascending sorted collection with comparable items |
| 144 | + :param item: item to insert |
| 145 | + :param lo: lowest index to consider (as in sorted_collection[lo:hi]) |
| 146 | + :param hi: past the highest index to consider (as in sorted_collection[lo:hi]) |
| 147 | +
|
| 148 | + Examples: |
| 149 | + >>> sorted_collection = [0, 5, 7, 10, 15] |
| 150 | + >>> insort_right(sorted_collection, 6) |
| 151 | + >>> sorted_collection |
| 152 | + [0, 5, 6, 7, 10, 15] |
| 153 | +
|
| 154 | + >>> sorted_collection = [(0, 0), (5, 5), (7, 7), (10, 10), (15, 15)] |
| 155 | + >>> item = (5, 5) |
| 156 | + >>> insort_right(sorted_collection, item) |
| 157 | + >>> sorted_collection |
| 158 | + [(0, 0), (5, 5), (5, 5), (7, 7), (10, 10), (15, 15)] |
| 159 | + >>> item is sorted_collection[1] |
| 160 | + False |
| 161 | + >>> item is sorted_collection[2] |
| 162 | + True |
| 163 | +
|
| 164 | + >>> sorted_collection = [0, 5, 7, 10, 15] |
| 165 | + >>> insort_right(sorted_collection, 20) |
| 166 | + >>> sorted_collection |
| 167 | + [0, 5, 7, 10, 15, 20] |
| 168 | +
|
| 169 | + >>> sorted_collection = [0, 5, 7, 10, 15] |
| 170 | + >>> insort_right(sorted_collection, 15, 1, 3) |
| 171 | + >>> sorted_collection |
| 172 | + [0, 5, 7, 15, 10, 15] |
| 173 | + """ |
| 174 | + sorted_collection.insert(bisect_right(sorted_collection, item, lo, hi), item) |
| 175 | + |
| 176 | + |
15 | 177 | def binary_search(sorted_collection, item):
|
16 | 178 | """Pure implementation of binary search algorithm in Python
|
17 | 179 |
|
|
0 commit comments