Advertisement
A_God

BinSearch

May 25th, 2022
228
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.74 KB | None | 0 0
  1. # //binary search algo
  2. # // arr --> array of the elements given
  3. # // n--> number to be found
  4. # // left --> 0
  5. # // right --> len(arr-1)
  6.  
  7. # from array import array
  8. def binSearch(arr,n):
  9.    
  10.     # arr = array("i" ,arr)
  11.     return (arr, n , 0 , len(arr-1))
  12.    
  13. def binSearchDriver(arra , n, left , right):
  14.     if left>right:
  15.         return -1
  16.     middle = (left-right)//2
  17.     match= arra[middle]
  18.     if match == n:
  19.         print (f"Found a match at: "+ middle)
  20.     elif n <match:
  21.         return binSearchDriver(arra , n, left , (middle-1))
  22.     else:
  23.         return binSearchDriver(arra , n , (middle+1) , right)
  24.        
  25.        
  26.        
  27. binSearch([5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 65, 70, 75, 80, 85, 90, 95] ,70)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement