Advertisement
Guest User

Untitled

a guest
Mar 31st, 2015
218
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.52 KB | None | 0 0
  1. def BinarySearch(val, A, idx = None):
  2. """Recursive algorithm for performing binary search"""
  3. #idx: the rightmost index based on the numbering of the original array
  4.  
  5. n = len(A)
  6.  
  7. if idx == None:
  8. idx = (n - 1)
  9.  
  10. if n == 1:
  11. if A[0] == val:
  12. return idx
  13. else:
  14. return -1
  15. else:
  16. split = n//2
  17. if A[split] > val:
  18. idx -= (n - split)
  19. return BinarySearch(val, A[:n//2], idx)
  20. else:
  21. return BinarySearch(val, A[n//2:], idx)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement