Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # //binary search algo
- # // arr --> array of the elements given
- # // n--> number to be found
- # // left --> 0
- # // right --> len(arr-1)
- # from array import array
- def binSearch(arr,n):
- # arr = array("i" ,arr)
- return (arr, n , 0 , len(arr-1))
- def binSearchDriver(arra , n, left , right):
- if left>right:
- return -1
- middle = (left-right)//2
- match= arra[middle]
- if match == n:
- print (f"Found a match at: "+ middle)
- elif n <match:
- return binSearchDriver(arra , n, left , (middle-1))
- else:
- return binSearchDriver(arra , n , (middle+1) , right)
- 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