Skip to content

Implemented Exponential Search with binary search for improved perfor… #11666

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

Merged
merged 6 commits into from
Oct 5, 2024
42 changes: 42 additions & 0 deletions searches/Exponential_Search.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# Python program to perform Exponential Search

Check failure on line 1 in searches/Exponential_Search.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (N999)

searches/Exponential_Search.py:1:1: N999 Invalid module name: 'Exponential_Search'
from bisect import bisect_left

Check failure on line 2 in searches/Exponential_Search.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (F401)

searches/Exponential_Search.py:2:20: F401 `bisect.bisect_left` imported but unused

# Binary search function

Check failure on line 4 in searches/Exponential_Search.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (I001)

searches/Exponential_Search.py:2:1: I001 Import block is un-sorted or un-formatted
def binary_search(arr, left, right, target):

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please provide return type hint for the function: binary_search. If the function does not return a value, please provide the type hint as: def function() -> None:

As there is no test file in this pull request nor any test function or class in the file searches/Exponential_Search.py, please provide doctest for the function binary_search

Please provide type hint for the parameter: arr

Please provide type hint for the parameter: left

Please provide type hint for the parameter: right

Please provide type hint for the parameter: target

while left <= right:
mid = left + (right - left) // 2
if arr[mid] == target:
return mid
elif arr[mid] < target:
left = mid + 1
else:
right = mid - 1
return -1

# Exponential search function
def exponential_search(arr, target):

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please provide return type hint for the function: exponential_search. If the function does not return a value, please provide the type hint as: def function() -> None:

As there is no test file in this pull request nor any test function or class in the file searches/Exponential_Search.py, please provide doctest for the function exponential_search

Please provide type hint for the parameter: arr

Please provide type hint for the parameter: target

n = len(arr)

# If the target is at the first position
if arr[0] == target:
return 0

# Find the range for binary search by repeatedly doubling the index
i = 1
while i < n and arr[i] <= target:
i *= 2

# Perform binary search within the found range
return binary_search(arr, i // 2, min(i, n - 1), target)

# Example usage
if __name__ == "__main__":
arr = [2, 3, 4, 10, 40, 50, 60, 70, 80, 90, 100]
target = 10

result = exponential_search(arr, target)

if result != -1:
print(f"Element found at index {result}")
else:
print("Element not found in the array")
Loading