Advertisement
nher1625

binary_recursive_search

Sep 29th, 2015
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.73 KB | None | 0 0
  1. import random
  2.        
  3. def recursive_binary_search(search_space):
  4.     n = len(search_space)
  5.     a, b = search_space[n/2:], search_space[:n/2]
  6.     print(a,b)
  7.  
  8.     if (sum(a) > sum(b)):
  9.         if n <= 2:
  10.             x, y = search_space[0], search_space[1]
  11.             return x if x > y else y
  12.         else:
  13.             return recursive_binary_search(a)
  14.     elif (sum(b) > sum(a)):
  15.         if n <= 2:
  16.             x, y = search_space[0], search_space[1]
  17.             return x if x > y else y
  18.         else:
  19.             return recursive_binary_search(b)
  20.     else:
  21.         return search_space[round(n/2)]
  22.    
  23. random_int_array = [random.randint(0,1000) for x in range(100)]    
  24.    
  25. print(recursive_binary_search(random_int_array))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement