-
-
Notifications
You must be signed in to change notification settings - Fork 46.6k
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
Changes from 1 commit
4808d62
ec85644
e3d13d3
e38e626
aa1eee1
afce723
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
# Python program to perform Exponential Search | ||
from bisect import bisect_left | ||
|
||
# Binary search function | ||
def binary_search(arr, left, right, 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): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please provide return type hint for the function: As there is no test file in this pull request nor any test function or class in the file Please provide type hint for the parameter: Please provide type hint for the parameter: |
||
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") |
There was a problem hiding this comment.
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 functionbinary_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