From a60efb5a40d5680350f05faad7be43c3554dd370 Mon Sep 17 00:00:00 2001 From: sharanabasava-05 Date: Thu, 21 May 2026 17:34:46 +0530 Subject: [PATCH 1/2] added time complexity to binary_search.py docstring --- searches/binary_search.py | 26 ++++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) diff --git a/searches/binary_search.py b/searches/binary_search.py index bec87b3c5aec..522f3099d05f 100644 --- a/searches/binary_search.py +++ b/searches/binary_search.py @@ -176,7 +176,6 @@ def insort_right( """ sorted_collection.insert(bisect_right(sorted_collection, item, lo, hi), item) - def binary_search(sorted_collection: list[int], item: int) -> int: """Pure implementation of a binary search algorithm in Python @@ -187,6 +186,29 @@ def binary_search(sorted_collection: list[int], item: int) -> int: :param item: item value to search :return: index of the found item or -1 if the item is not found + Time Complexity: + Best Case: O(1) + The item is found at the middle index in the first comparison. + + Average Case: O(log n) + Search space is reduced by half in each iteration. + + Worst Case: O(log n) + The item is at the end or not present in the collection. + + Space Complexity: O(1) + - Uses constant extra memory. + Args: + sorted_collection (list[int]): + Ascending sorted collection of comparable items. + + item (int): + Item value to search. + Returns: + int: + Index of the found item, + or -1 if the item is not found. + Examples: >>> binary_search([0, 5, 7, 10, 15], 0) 0 @@ -211,7 +233,7 @@ def binary_search(sorted_collection: list[int], item: int) -> int: right = midpoint - 1 else: left = midpoint + 1 - return -1 + return -1 def binary_search_std_lib(sorted_collection: list[int], item: int) -> int: From a1882da10fae18fcb41474d610f57e701c7164c5 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 21 May 2026 12:08:33 +0000 Subject: [PATCH 2/2] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- searches/binary_search.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/searches/binary_search.py b/searches/binary_search.py index 522f3099d05f..eb6ffda1b6d1 100644 --- a/searches/binary_search.py +++ b/searches/binary_search.py @@ -176,6 +176,7 @@ def insort_right( """ sorted_collection.insert(bisect_right(sorted_collection, item, lo, hi), item) + def binary_search(sorted_collection: list[int], item: int) -> int: """Pure implementation of a binary search algorithm in Python @@ -233,7 +234,7 @@ def binary_search(sorted_collection: list[int], item: int) -> int: right = midpoint - 1 else: left = midpoint + 1 - return -1 + return -1 def binary_search_std_lib(sorted_collection: list[int], item: int) -> int: