Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions searches/binary_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,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
Expand Down