Skip to content

Commit 28d6266

Browse files
committed
Fix busca_binaria.rb
1 parent e56d5bd commit 28d6266

File tree

2 files changed

+18
-20
lines changed

2 files changed

+18
-20
lines changed

src/ruby/BuscaBinaria.rb

Lines changed: 0 additions & 20 deletions
This file was deleted.

src/ruby/busca_binaria.rb

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# frozen_string_literal: true
2+
3+
def busca_binaria(valor, vetor, esquerda, direita)
4+
meio = ((esquerda + direita) / 2).floor
5+
6+
return -1 unless esquerda <= direita
7+
8+
if valor > vetor[meio]
9+
busca_binaria(valor, vetor, meio + 1, direita)
10+
elsif valor < vetor[meio]
11+
busca_binaria(valor, vetor, esquerda, meio - 1)
12+
else
13+
meio
14+
end
15+
end
16+
17+
vetor = [0, 1, 3, 5, 6, 7, 8, 9, 10, 11, 12]
18+
print busca_binaria(12, vetor, 0, vetor.length)

0 commit comments

Comments
 (0)