Skip to content

Commit f786d61

Browse files
committed
Pesquisa Binaria em Python
1 parent b0df2d5 commit f786d61

1 file changed

Lines changed: 19 additions & 0 deletions

File tree

algorithms/binary_search

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
def pesquisa_binaria(lista, item):
2+
baixo = 0
3+
alto = len(lista) - 1
4+
5+
while baixo <= alto:
6+
meio = (baixo + alto) // 2
7+
chute = lista[meio]
8+
if chute == item:
9+
return meio
10+
if chute > item:
11+
alto = meio - 1
12+
else:
13+
baixo = meio + 1
14+
15+
return None
16+
17+
minha_lista = [1, 3, 5, 7, 9]
18+
print(pesquisa_binaria(minha_lista, 3))
19+
print(pesquisa_binaria(minha_lista, -1))

0 commit comments

Comments
 (0)