Skip to content
Open
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions DIRECTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -1323,6 +1323,7 @@

## [Searches](searches)
* [Binary Search](searches/binary_search.py)
* [Binary Search Recursion](searches/binary_search_recursion.py)
* [Binary Tree Traversal](searches/binary_tree_traversal.py)
* [Double Linear Search](searches/double_linear_search.py)
* [Double Linear Search Recursion](searches/double_linear_search_recursion.py)
Expand Down
62 changes: 62 additions & 0 deletions searches/binary_search_recursion.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
"""
Pure Python implementation of Recursive Binary Search.

Binary Search is a divide-and-conquer algorithm that works on sorted lists.
"""

from __future__ import annotations

from collections.abc import Sequence
from typing import TypeVar

T = TypeVar("T")


def binary_search_recursive(
arr: Sequence[T],
target: T,
left: int = 0,
right: int | None = None,
) -> int:

Check failure on line 20 in searches/binary_search_recursion.py

View workflow job for this annotation

GitHub Actions / ruff

ruff (UP047)

searches/binary_search_recursion.py:15:5: UP047 Generic function `binary_search_recursive` should use type parameters help: Use type parameters
"""
Perform recursive binary search on a sorted sequence.

:param arr: A sorted sequence of comparable elements
:param target: The element to search for
:param left: Left boundary of the search interval
:param right: Right boundary of the search interval
:return: Index of target if found, otherwise -1

>>> binary_search_recursive([1, 2, 3, 4, 5], 3)
2
>>> binary_search_recursive([1, 2, 3, 4, 5], 1)
0
>>> binary_search_recursive([1, 2, 3, 4, 5], 5)
4
>>> binary_search_recursive([1, 2, 3, 4, 5], 6)
-1
>>> binary_search_recursive([], 10)
-1
>>> binary_search_recursive([2, 4, 6, 8], 6)
2
"""

if right is None:
right = len(arr) - 1

if left > right:
return -1

mid = left + (right - left) // 2

if arr[mid] == target:
return mid
if arr[mid] > target:
return binary_search_recursive(arr, target, left, mid - 1)
return binary_search_recursive(arr, target, mid + 1, right)


if __name__ == "__main__":
import doctest

doctest.testmod()
Loading