Skip to content

Create exponential_search.py #7624

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

Closed
wants to merge 1 commit into from
Closed
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
78 changes: 78 additions & 0 deletions searches/exponential_search.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
"""
This is pure Python implementation of exponential search algorithm
For doctests run following command:
python3 -m doctest -v linear_search.py
For manual testing run:
python3 linear_search.py
"""

# A recursive binary search function returns location of search_element in given array if present, otherwise -1
def binary_search( array: list, left: int, right: int, search_element: int) -> int:
"""A pure Python implementation of binary search algorithm
:param array: a collection with comparable items (items are sorted)
:param left: leftmost boundary of the array
:param right: rightmost boundary of the array
:param search_element: the element we are looking for in the array
:return: index of found item or -1 if item is not found
Examples:
>>> binary_search([0, 5, 7, 10, 15], 0, 4, 0)
0
>>> binary_search([0, 5, 7, 10, 15], 0, 4, 15)
4
>>> binary_search([0, 5, 7, 10, 15], 0, 4, 5)
1
>>> binary_search([0, 5, 7, 10, 15], 0, 4, 6)
-1
"""
if right >= left:
middle = left + ( right-left ) // 2

# If the element is present at the middle itself
if array[middle] == search_element:
return middle

# If the element is smaller than mid, then it can only be present in the left subarray
if array[middle] > search_element:
return binary_search(array, left,
middle - 1, search_element)

# Else he element can only be present in the right
return binary_search(array, middle + 1, right, search_element)

# We reach here if the search_element is not present
return -1

# Returns the position of first occurrence of search_element in array
def exponential_search(array: list, no_of_items: int, search_element: int) -> int:
"""A pure Python implementation of exponential search algorithm
:param array: a collection with comparable items (items are sorted)
:param no_of_items: number of items in the array
:param search_element: the element we are looking for in the array
:return: index of found item or -1 if item is not found
Examples:
>>> exponential_search([0, 5, 7, 10, 15], 5, 0)
0
>>> exponential_search([0, 5, 7, 10, 15], 5, 15)
4
>>> exponential_search([0, 5, 7, 10, 15], 5, 5)
1
>>> exponential_search([0, 5, 7, 10, 15], 5, 6)
-1
"""

# IF search_element is present at first location itself
if array[0] == search_element:
return 0

index = 1
while index < no_of_items and array[index] <= search_element:
index = index * 2

# Call binary search for searching the element in the found range
return binary_search( array, index // 2,
min(index, no_of_items-1), search_element)


if __name__ == "__main__":
import doctest
doctest.testmod()