From a684d7989e80c018bc0dba0a0e92c0293d854365 Mon Sep 17 00:00:00 2001 From: bt3gl <138340846+bt3gl-cryptographer@users.noreply.github.com> Date: Tue, 8 Aug 2023 17:17:16 -0700 Subject: [PATCH] Update README.md --- searching/README.md | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/searching/README.md b/searching/README.md index cce5a68..bdeaccd 100644 --- a/searching/README.md +++ b/searching/README.md @@ -14,9 +14,24 @@ * `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 +* in python, `bisect.bisect_left()` returns the index at which the `val` should be inserted in the sorted array.
+```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