mirror of
https://github.com/autistic-symposium/master-algorithms-py.git
synced 2025-04-29 12:16:14 -04:00
Update README.md
This commit is contained in:
parent
7b32dbb0d7
commit
a684d7989e
@ -14,9 +14,24 @@
|
|||||||
* `while left < right`, with `left = mid + 1` and `right = mid`, and `left` is returned
|
* `while left < right`, with `left = mid + 1` and `right = mid`, and `left` is returned
|
||||||
* `while left + 1 < right`, with `left = mid` and `right = mid`, and `left` and `right` are returned
|
* `while left + 1 < right`, with `left = mid` and `right = mid`, and `left` and `right` are returned
|
||||||
|
|
||||||
|
* in python, `bisect.bisect_left()` returns the index at which the `val` should be inserted in the sorted array.
|
||||||
|
|
||||||
<br>
|
<br>
|
||||||
|
|
||||||
|
```python
|
||||||
|
from bisect import bisect_left
|
||||||
|
|
||||||
|
def binary_search(array, val, low=0, high=None):
|
||||||
|
|
||||||
|
high = high or len(array)
|
||||||
|
position = bisect_left(array, val, low, high)
|
||||||
|
|
||||||
|
if position != high and array[position] == value:
|
||||||
|
return position
|
||||||
|
|
||||||
|
return -1
|
||||||
|
```
|
||||||
|
|
||||||
----
|
----
|
||||||
|
|
||||||
### iterative
|
### iterative
|
||||||
|
Loading…
x
Reference in New Issue
Block a user