Skip to content

Commit ff05412

Browse files
authored
Adiciona Busca por interpolação, Minimo e maximo recursivo, Minimo e maximo iterativo
1 parent ef9d3ac commit ff05412

File tree

4 files changed

+90
-0
lines changed

4 files changed

+90
-0
lines changed

src/ruby/interpolation_search.rb

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# Algoritmo de Busca por interpolação em Ruby
2+
# Carlos Alves
3+
# https://github.com/EuCarlos
4+
5+
def interpolation_search(value, data)
6+
low = 0
7+
high = data.length - 1
8+
mid = ((data.length - 1) / 2).floor + low
9+
10+
while low < mid do
11+
if data[mid] == value
12+
return "Elemento encontrado na #{high}° posição do array ordenado."
13+
elsif value > data[mid]
14+
low = mid
15+
high = high
16+
mid = ((high - low) / 2).floor + low
17+
else
18+
low = low
19+
high = mid
20+
mid = ((high - low) / 2).floor + low
21+
end
22+
end
23+
end
24+
25+
def main
26+
array_1 = [37, 53, 1, 43, 11, 49, 32, 55, 40, 47, 25, 4].sort
27+
puts interpolation_search(43, array_1) # 8° posição do array ordenado
28+
29+
array_2 = [23, 9, 5, 78, 123, 5444, 54535, 64, 3, 12, 14, 15].sort
30+
puts interpolation_search(5444, array_2) # 11° posição do array ordenado
31+
end
32+
33+
main()

src/ruby/max_recursivo.rb

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# Algoritmo de Máximo Recursivo em Ruby
2+
# Carlos Alves
3+
# https://github.com/EuCarlos
4+
5+
def max_recursivo(vetor, maximo, indice)
6+
maximo = vetor[indice] if vetor[indice] > maximo
7+
8+
maximo = max_recursivo(vetor, maximo, indice + 1) if indice < vetor.length - 1
9+
10+
return maximo
11+
end
12+
13+
lista = [19, 32, 43, 58, 12, 28, 98, 19, 12, 10]
14+
print lista
15+
16+
puts "\nMaximo recursivo: #{max_recursivo(lista, lista[0], 0)}"

src/ruby/min_max_iterativo.rb

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Algoritmo de Mínimo Recursivo em Ruby
2+
# Carlos Alves
3+
# https://github.com/EuCarlos
4+
5+
def min_max_array(vetor)
6+
minimo = vetor[0]
7+
maximo = vetor[0]
8+
9+
vetor_size = vetor.length - 1
10+
for i in 1..vetor_size
11+
if vetor[i] < minimo
12+
minimo = vetor[i]
13+
elsif vetor[i] > maximo
14+
maximo = vetor[i]
15+
end
16+
end
17+
18+
puts "Minimo: #{minimo}"
19+
puts "Maximo: #{maximo}"
20+
end
21+
22+
lista = [2, 94, 83, 10, 0, 2, 48, 1, 24]
23+
min_max_array(lista)

src/ruby/min_max_recursivo.rb

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Mínimo e Máximo recursivo em Ruby
2+
# Carlos Alves
3+
# https://github.com/EuCarlos
4+
5+
def recursive_min_and_max(arr, min, max, index)
6+
min = arr[index] if arr[index] < min
7+
max = arr[index] if arr[index] > max
8+
9+
if index < arr.length - 1
10+
recursive_min_and_max(arr, min, max, index + 1)
11+
else
12+
puts "Mínimo: #{min}"
13+
puts "Máximo: #{max}"
14+
end
15+
end
16+
17+
list = [37, 53, 1, 43, 11, 49, 32, 55, 40, 47, 25, 4]
18+
recursive_min_and_max(list, list[0], list[0], 0)

0 commit comments

Comments
 (0)