22
33from bisect import bisect_left , bisect_right
44
5+
56def bisect_left_custom (sorted_collection , item , lo = 0 , hi = None ):
67 """
78 Custom implementation of bisect_left.
@@ -17,6 +18,7 @@ def bisect_left_custom(sorted_collection, item, lo=0, hi=None):
1718 hi = mid
1819 return lo
1920
21+
2022def bisect_right_custom (sorted_collection , item , lo = 0 , hi = None ):
2123 """
2224 Custom implementation of bisect_right.
@@ -32,18 +34,21 @@ def bisect_right_custom(sorted_collection, item, lo=0, hi=None):
3234 hi = mid
3335 return lo
3436
37+
3538def insort_left_custom (sorted_collection , item , lo = 0 , hi = None ):
3639 """
3740 Inserts item into sorted_collection in sorted order (using bisect_left_custom).
3841 """
3942 sorted_collection .insert (bisect_left_custom (sorted_collection , item , lo , hi ), item )
4043
44+
4145def insort_right_custom (sorted_collection , item , lo = 0 , hi = None ):
4246 """
4347 Inserts item into sorted_collection in sorted order (using bisect_right_custom).
4448 """
4549 sorted_collection .insert (bisect_right_custom (sorted_collection , item , lo , hi ), item )
4650
51+
4752def binary_search (sorted_collection , item ):
4853 """
4954 Standard binary search implementation.
@@ -60,6 +65,7 @@ def binary_search(sorted_collection, item):
6065 hi = mid - 1
6166 return - 1
6267
68+
6369def binary_search_std_lib (sorted_collection , item ):
6470 """
6571 Binary search using Python's standard library bisect module.
@@ -69,6 +75,7 @@ def binary_search_std_lib(sorted_collection, item):
6975 return index
7076 return - 1
7177
78+
7279def binary_search_by_recursion (sorted_collection , item , lo = 0 , hi = None ):
7380 """
7481 Binary search using recursion.
@@ -85,6 +92,7 @@ def binary_search_by_recursion(sorted_collection, item, lo=0, hi=None):
8592 else :
8693 return binary_search_by_recursion (sorted_collection , item , mid + 1 , hi )
8794
95+
8896def exponential_search (sorted_collection , item ):
8997 """
9098 Exponential search implementation.
@@ -95,7 +103,10 @@ def exponential_search(sorted_collection, item):
95103 bound = 1
96104 while bound < len (sorted_collection ) and sorted_collection [bound ] < item :
97105 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+
99110
100111if __name__ == "__main__" :
101112 import doctest
@@ -105,7 +116,12 @@ def exponential_search(sorted_collection, item):
105116 doctest .testmod ()
106117
107118 # 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+ ]
109125
110126 # Test and print results of searching for 10 in a sample list
111127 for search in searches :
@@ -115,7 +131,12 @@ def exponential_search(sorted_collection, item):
115131 setup = "collection = list(range(1000))"
116132 # Benchmark each search function
117133 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+ )
119140 print (f"{ search .__name__ :>26} : { time :.6f} " )
120141
121142 # Interactive part: user inputs a list and a target number
0 commit comments