2
2
3
3
from bisect import bisect_left , bisect_right
4
4
5
+
5
6
def bisect_left_custom (sorted_collection , item , lo = 0 , hi = None ):
6
7
"""
7
8
Custom implementation of bisect_left.
@@ -17,6 +18,7 @@ def bisect_left_custom(sorted_collection, item, lo=0, hi=None):
17
18
hi = mid
18
19
return lo
19
20
21
+
20
22
def bisect_right_custom (sorted_collection , item , lo = 0 , hi = None ):
21
23
"""
22
24
Custom implementation of bisect_right.
@@ -32,18 +34,21 @@ def bisect_right_custom(sorted_collection, item, lo=0, hi=None):
32
34
hi = mid
33
35
return lo
34
36
37
+
35
38
def insort_left_custom (sorted_collection , item , lo = 0 , hi = None ):
36
39
"""
37
40
Inserts item into sorted_collection in sorted order (using bisect_left_custom).
38
41
"""
39
42
sorted_collection .insert (bisect_left_custom (sorted_collection , item , lo , hi ), item )
40
43
44
+
41
45
def insort_right_custom (sorted_collection , item , lo = 0 , hi = None ):
42
46
"""
43
47
Inserts item into sorted_collection in sorted order (using bisect_right_custom).
44
48
"""
45
49
sorted_collection .insert (bisect_right_custom (sorted_collection , item , lo , hi ), item )
46
50
51
+
47
52
def binary_search (sorted_collection , item ):
48
53
"""
49
54
Standard binary search implementation.
@@ -60,6 +65,7 @@ def binary_search(sorted_collection, item):
60
65
hi = mid - 1
61
66
return - 1
62
67
68
+
63
69
def binary_search_std_lib (sorted_collection , item ):
64
70
"""
65
71
Binary search using Python's standard library bisect module.
@@ -69,6 +75,7 @@ def binary_search_std_lib(sorted_collection, item):
69
75
return index
70
76
return - 1
71
77
78
+
72
79
def binary_search_by_recursion (sorted_collection , item , lo = 0 , hi = None ):
73
80
"""
74
81
Binary search using recursion.
@@ -85,6 +92,7 @@ def binary_search_by_recursion(sorted_collection, item, lo=0, hi=None):
85
92
else :
86
93
return binary_search_by_recursion (sorted_collection , item , mid + 1 , hi )
87
94
95
+
88
96
def exponential_search (sorted_collection , item ):
89
97
"""
90
98
Exponential search implementation.
@@ -95,7 +103,10 @@ def exponential_search(sorted_collection, item):
95
103
bound = 1
96
104
while bound < len (sorted_collection ) and sorted_collection [bound ] < item :
97
105
bound *= 2
98
- return binary_search_by_recursion (sorted_collection , item , bound // 2 , min (bound , len (sorted_collection ) - 1 ))
106
+ return binary_search_by_recursion (
107
+ sorted_collection , item , bound // 2 , min (bound , len (sorted_collection ) - 1 )
108
+ )
109
+
99
110
100
111
if __name__ == "__main__" :
101
112
import doctest
@@ -105,7 +116,12 @@ def exponential_search(sorted_collection, item):
105
116
doctest .testmod ()
106
117
107
118
# List of search functions to benchmark
108
- searches = [binary_search_std_lib , binary_search , exponential_search , binary_search_by_recursion ]
119
+ searches = [
120
+ binary_search_std_lib ,
121
+ binary_search ,
122
+ exponential_search ,
123
+ binary_search_by_recursion ,
124
+ ]
109
125
110
126
# Test and print results of searching for 10 in a sample list
111
127
for search in searches :
@@ -115,7 +131,12 @@ def exponential_search(sorted_collection, item):
115
131
setup = "collection = list(range(1000))"
116
132
# Benchmark each search function
117
133
for search in searches :
118
- time = timeit .timeit (f"{ search .__name__ } (collection, 500)" , setup = setup , number = 5000 , globals = globals ())
134
+ time = timeit .timeit (
135
+ f"{ search .__name__ } (collection, 500)" ,
136
+ setup = setup ,
137
+ number = 5000 ,
138
+ globals = globals (),
139
+ )
119
140
print (f"{ search .__name__ :>26} : { time :.6f} " )
120
141
121
142
# Interactive part: user inputs a list and a target number
0 commit comments