forked from TheAlgorithms/Python
- Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexponential_search.py
113 lines (88 loc) · 3.61 KB
/
exponential_search.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#!/usr/bin/env python3
"""
Pure Python implementation of exponential search algorithm
For more information, see the Wikipedia page:
https://en.wikipedia.org/wiki/Exponential_search
For doctests run the following command:
python3 -m doctest -v exponential_search.py
For manual testing run:
python3 exponential_search.py
"""
from __future__ importannotations
defbinary_search_by_recursion(
sorted_collection: list[int], item: int, left: int=0, right: int=-1
) ->int:
"""Pure implementation of binary search algorithm in Python using recursion
Be careful: the collection must be ascending sorted otherwise, the result will be
unpredictable.
:param sorted_collection: some ascending sorted collection with comparable items
:param item: item value to search
:param left: starting index for the search
:param right: ending index for the search
:return: index of the found item or -1 if the item is not found
Examples:
>>> binary_search_by_recursion([0, 5, 7, 10, 15], 0, 0, 4)
0
>>> binary_search_by_recursion([0, 5, 7, 10, 15], 15, 0, 4)
4
>>> binary_search_by_recursion([0, 5, 7, 10, 15], 5, 0, 4)
1
>>> binary_search_by_recursion([0, 5, 7, 10, 15], 6, 0, 4)
-1
"""
ifright<0:
right=len(sorted_collection) -1
iflist(sorted_collection) !=sorted(sorted_collection):
raiseValueError("sorted_collection must be sorted in ascending order")
ifright<left:
return-1
midpoint=left+ (right-left) //2
ifsorted_collection[midpoint] ==item:
returnmidpoint
elifsorted_collection[midpoint] >item:
returnbinary_search_by_recursion(sorted_collection, item, left, midpoint-1)
else:
returnbinary_search_by_recursion(sorted_collection, item, midpoint+1, right)
defexponential_search(sorted_collection: list[int], item: int) ->int:
"""
Pure implementation of an exponential search algorithm in Python.
For more information, refer to:
https://en.wikipedia.org/wiki/Exponential_search
Be careful: the collection must be ascending sorted, otherwise the result will be
unpredictable.
:param sorted_collection: some ascending sorted collection with comparable items
:param item: item value to search
:return: index of the found item or -1 if the item is not found
The time complexity of this algorithm is O(log i) where i is the index of the item.
Examples:
>>> exponential_search([0, 5, 7, 10, 15], 0)
0
>>> exponential_search([0, 5, 7, 10, 15], 15)
4
>>> exponential_search([0, 5, 7, 10, 15], 5)
1
>>> exponential_search([0, 5, 7, 10, 15], 6)
-1
"""
iflist(sorted_collection) !=sorted(sorted_collection):
raiseValueError("sorted_collection must be sorted in ascending order")
ifsorted_collection[0] ==item:
return0
bound=1
whilebound<len(sorted_collection) andsorted_collection[bound] <item:
bound*=2
left=bound//2
right=min(bound, len(sorted_collection) -1)
returnbinary_search_by_recursion(sorted_collection, item, left, right)
if__name__=="__main__":
importdoctest
doctest.testmod()
# Manual testing
user_input=input("Enter numbers separated by commas: ").strip()
collection=sorted(int(item) foriteminuser_input.split(","))
target=int(input("Enter a number to search for: "))
result=exponential_search(sorted_collection=collection, item=target)
ifresult==-1:
print(f"{target} was not found in {collection}.")
else:
print(f"{target} was found at index {result} in {collection}.")