Skip to content

Improve documentation for binary search algorithm #12702

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
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
58 changes: 29 additions & 29 deletions searches/binary_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,40 +178,40 @@
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
# Binary Search Algorithm
# Binary Search is an efficient algorithm for finding an item from a sorted list of items.

Check failure on line 182 in searches/binary_search.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (E501)

searches/binary_search.py:182:89: E501 Line too long (90 > 88)
# It works by repeatedly dividing in half the portion of the list that could contain the item,

Check failure on line 183 in searches/binary_search.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (E501)

searches/binary_search.py:183:89: E501 Line too long (94 > 88)
# until you've narrowed the possible locations to just one.

Be careful collection must be ascending sorted otherwise, the result will be
unpredictable

:param sorted_collection: some ascending sorted collection with comparable items
:param item: item value to search
:return: index of the found item or -1 if the item is not found
def binary_search(arr, x):
"""
Perform a binary search to find the element `x` in the sorted list `arr`.

Examples:
>>> binary_search([0, 5, 7, 10, 15], 0)
0
>>> binary_search([0, 5, 7, 10, 15], 15)
4
>>> binary_search([0, 5, 7, 10, 15], 5)
1
>>> binary_search([0, 5, 7, 10, 15], 6)
-1
Parameters:
arr (list): A sorted list of elements
x (int or float): The element to search for in the list

Returns:
int: The index of the element `x` in the list `arr`. If `x` is not found, returns -1.

Check failure on line 196 in searches/binary_search.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (E501)

searches/binary_search.py:196:89: E501 Line too long (89 > 88)
"""
if list(sorted_collection) != sorted(sorted_collection):
raise ValueError("sorted_collection must be sorted in ascending order")
left = 0
right = len(sorted_collection) - 1

while left <= right:
midpoint = left + (right - left) // 2
current_item = sorted_collection[midpoint]
if current_item == item:
return midpoint
elif item < current_item:
right = midpoint - 1
low = 0
high = len(arr) - 1

while low <= high:
mid = (low + high) // 2

# If x is found at the mid index
if arr[mid] == x:
return mid
# If x is smaller than mid, search in the left half
elif arr[mid] > x:
high = mid - 1
# If x is larger than mid, search in the right half
else:
left = midpoint + 1
low = mid + 1

# Return -1 if element is not present in the list
return -1


Expand Down
Loading