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 1 commit
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
Prev Previous commit
[pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
  • Loading branch information
pre-commit-ci[bot] committed May 1, 2025
commit 58a27b791c3e5d163902f7879fbb7c03fcabd008
2 changes: 2 additions & 0 deletions searches/binary_search.py
Original file line number Diff line number Diff line change
@@ -179,10 +179,11 @@


# 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

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

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.


def binary_search(arr, x):
"""
Perform a binary search to find the element `x` in the sorted list `arr`.
@@ -192,7 +193,7 @@
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

GitHub Actions / ruff

Ruff (E501)

searches/binary_search.py:196:89: E501 Line too long (89 > 88)
"""
low = 0
high = len(arr) - 1
@@ -213,6 +214,7 @@
# Return -1 if element is not present in the list
return -1


def binary_search_std_lib(sorted_collection: list[int], item: int) -> int:
"""Pure implementation of a binary search algorithm in Python using stdlib

Loading