Skip to content
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

Added Recursive Binary Search Algorithm #335

Open
wants to merge 1 commit 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
62 changes: 62 additions & 0 deletions binary_search_recursive.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
"""Recursive approach of binary search.
Ref : https://en.wikipedia.org/wiki/Binary_search_algorithm

Time Complexity : O(log n)
Space Complexity : O(1)
"""


def find_index(nums: list, k: int, left: int, right: int) -> int:
"""_summary_
Args:
nums (list): list of numbers
k (int): number to be find from the list
left (int): lower bound
right (int): upper bound
Returns:
int: index of k in list or -1 if k is not found in list
Examples
--------
>>> find_index([1,4,5,6,7],6,0,5)
3
>>> find_index([1,4,5,6,7],2,0,5)
-1
"""
# Base case
if left >= right:
return -1

else:
mid = (left + right) // 2

if nums[mid] == k:
return mid
elif nums[mid] > k:
return find_index(nums, k, left, mid)
else:
return find_index(nums, k, mid + 1, right)


def binary_search(nums: list, k: int, n: int) -> int:
"""This function uses divide & conquer paradigm to find the number in a sorted list
and return its index.
Args:
nums (list): list of numbers
k (int): number to be find from the list
n (int): size of list
Returns:
int: index of k or -1 if k is not present in list
Examples
--------
>>> binary_search([1,4,5,6,7],6,5)
3
>>> binary_search([1,4,5,6,7],3,5)
-1
"""
left = 0
right = n

return find_index(nums, k, left, right)


print(binary_search([1, 4, 5, 6, 7], 6, 5))