Advertisement
Guest User

Untitled

a guest
Nov 18th, 2019
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.71 KB | None | 0 0
  1. def b_binaria_i(l, buscado):
  2.     inicio = 0
  3.     fin = len(l) - 1
  4.     while inicio <= fin:
  5.         medio = (inicio + fin) // 2
  6.         if l[medio] == buscado:
  7.             return True
  8.         if l[medio] > buscado:
  9.             fin = medio - 1
  10.         else:
  11.             inicio = medio + 1
  12.     return False
  13.  
  14. def b_binaria_r(l, buscado, inicio, fin):
  15.     medio = (inicio + fin) // 2
  16.     if inicio <= fin:
  17.         if l[medio] == buscado:
  18.             return True
  19.         if l[medio] > buscado:
  20.             return b_binaria_r(l, buscado, inicio, medio - 1)
  21.         return b_binaria_r(l, buscado, medio + 1, fin)
  22.     return False
  23.  
  24. lista = [1, 4, 7, 8, 9, 10, 11, 15, 20, 23]
  25. print(b_binaria_r(lista, 2, 0, 9))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement