Skip to content

Commit c7b744b

Browse files
authored
Merge pull request kelvins#255 from Ananyasingh2002/hacktober
Updated file busca_binaria.rb
2 parents 7678a36 + 6c1c180 commit c7b744b

File tree

1 file changed

+10
-11
lines changed

1 file changed

+10
-11
lines changed

src/ruby/busca_binaria.rb

+10-11
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,17 @@
11
# frozen_string_literal: true
2+
def binary_search(value, array, left, right)
3+
middle = ((left + right) / 2).truncate
24

3-
def busca_binaria(valor, vetor, esquerda, direita)
4-
meio = ((esquerda + direita) / 2).floor
5+
return -1 unless left <= right
56

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)
7+
if value > array[middle]
8+
binary_search(value, array, middle + 1, right)
9+
elsif value < array[middle]
10+
binary_search(value, array, left, middle - 1)
1211
else
13-
meio
12+
middle
1413
end
1514
end
1615

17-
vetor = [0, 1, 3, 5, 6, 7, 8, 9, 10, 11, 12]
18-
print busca_binaria(12, vetor, 0, vetor.length)
16+
array = [0, 1, 3, 5, 6, 7, 8, 9, 10, 11, 12]
17+
puts binary_search(12, array, 0, array.length)

0 commit comments

Comments
 (0)