Skip to content
61 changes: 61 additions & 0 deletions searches/binary_search_recursion.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
"""
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

Check failure on line 10 in searches/binary_search_recursion.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (I001)

searches/binary_search_recursion.py:7:1: I001 Import block is un-sorted or un-formatted
T = TypeVar("T")


def binary_search_recursive(
arr: Sequence[T],
target: T,
left: int = 0,
right: int | None = None,
) -> int:
"""
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