@@ -321,27 +321,24 @@ def exponential_search(sorted_collection: list[int], item: int) -> int:
321321 return - 1
322322 return last_result
323323
324- #binary search uses divide and conquer
325- #We will leverage the fact that our list is sorted
326- def binary_search2 (l ,target , low = None ,high = None ):
324+
325+ # binary search uses divide and conquer
326+ # We will leverage the fact that our list is sorted
327+ def binary_search2 (l , target , low = None , high = None ):
327328 if low is None :
328- low = 0
329+ low = 0
329330 if high is None :
330- high = len (l )- 1
331+ high = len (l ) - 1
331332 if high < low :
332333 return - 1
333-
334- midpoint = (low + high ) // 2
334+
335+ midpoint = (low + high ) // 2
335336 if l [midpoint ] == target :
336337 return midpoint
337- elif target < l [midpoint ]:
338- return binary_search (l ,target ,low ,midpoint - 1 )
338+ elif target < l [midpoint ]:
339+ return binary_search (l , target , low , midpoint - 1 )
339340 else :
340- return binary_search (l ,target ,midpoint + 1 ,high )
341-
342-
343-
344-
341+ return binary_search (l , target , midpoint + 1 , high )
345342
346343
347344searches = ( # Fastest to slowest...
@@ -381,6 +378,6 @@ def binary_search2(l,target, low=None,high=None):
381378 else :
382379 print (f"{ target } was found at position { result } of { collection } ." )
383380
384- #Testing the binary search 2
385- mylist = [1 ,2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 ]
386- print (binary_search (mylist ,10 ) )
381+ # Testing the binary search 2
382+ mylist = [1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 ]
383+ print (binary_search (mylist , 10 ))
0 commit comments