Skip to content

Commit 5d56964

Browse files
authored
Merge pull request kelvins#145 from EuCarlos/main
Adicionado algoritmos e estrutura de dados em Ruby
2 parents 4382a50 + 2420a57 commit 5d56964

11 files changed

+338
-20
lines changed

README.md

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,8 @@ Com o objetivo de alcançar uma abrangência maior e encorajar novas pessoas a c
9292
</a>
9393
</td>
9494
<td> <!-- Ruby -->
95-
<a href="./CONTRIBUTING.md">
96-
<img align="center" height="25" src="https://cdn.jsdelivr.net/gh/devicons/devicon/icons/github/github-original.svg" />
95+
<a href="./src/ruby/algoritmo_dijkstra.rb">
96+
<img align="center" height="25" src="https://cdn.jsdelivr.net/gh/devicons/devicon/icons/ruby/ruby-original.svg" />
9797
</a>
9898
</td>
9999
<td> <!-- JavaScript -->
@@ -498,8 +498,8 @@ Com o objetivo de alcançar uma abrangência maior e encorajar novas pessoas a c
498498
</a>
499499
</td>
500500
<td> <!-- Ruby -->
501-
<a href="./CONTRIBUTING.md">
502-
<img align="center" height="25" src="https://cdn.jsdelivr.net/gh/devicons/devicon/icons/github/github-original.svg" />
501+
<a href="./src/ruby/interpolation_search.rb">
502+
<img align="center" height="25" src="https://cdn.jsdelivr.net/gh/devicons/devicon/icons/ruby/ruby-original.svg" />
503503
</a>
504504
</td>
505505
<td> <!-- JavaScript -->
@@ -846,8 +846,8 @@ Com o objetivo de alcançar uma abrangência maior e encorajar novas pessoas a c
846846
</a>
847847
</td>
848848
<td> <!-- Ruby -->
849-
<a href="./CONTRIBUTING.md">
850-
<img align="center" height="25" src="https://cdn.jsdelivr.net/gh/devicons/devicon/icons/github/github-original.svg" />
849+
<a href="./src/ruby/factorial.rb">
850+
<img align="center" height="25" src="https://cdn.jsdelivr.net/gh/devicons/devicon/icons/ruby/ruby-original.svg" />
851851
</a>
852852
</td>
853853
<td> <!-- JavaScript -->
@@ -1020,8 +1020,8 @@ Com o objetivo de alcançar uma abrangência maior e encorajar novas pessoas a c
10201020
</a>
10211021
</td>
10221022
<td> <!-- Ruby -->
1023-
<a href="./CONTRIBUTING.md">
1024-
<img align="center" height="25" src="https://cdn.jsdelivr.net/gh/devicons/devicon/icons/github/github-original.svg" />
1023+
<a href="./src/ruby/max_recursivo.rb">
1024+
<img align="center" height="25" src="https://cdn.jsdelivr.net/gh/devicons/devicon/icons/ruby/ruby-original.svg" />
10251025
</a>
10261026
</td>
10271027
<td> <!-- JavaScript -->
@@ -1078,8 +1078,8 @@ Com o objetivo de alcançar uma abrangência maior e encorajar novas pessoas a c
10781078
</a>
10791079
</td>
10801080
<td> <!-- Ruby -->
1081-
<a href="./CONTRIBUTING.md">
1082-
<img align="center" height="25" src="https://cdn.jsdelivr.net/gh/devicons/devicon/icons/github/github-original.svg" />
1081+
<a href="./src/ruby/min_max_iterativo.rb">
1082+
<img align="center" height="25" src="https://cdn.jsdelivr.net/gh/devicons/devicon/icons/ruby/ruby-original.svg" />
10831083
</a>
10841084
</td>
10851085
<td> <!-- JavaScript -->
@@ -1136,8 +1136,8 @@ Com o objetivo de alcançar uma abrangência maior e encorajar novas pessoas a c
11361136
</a>
11371137
</td>
11381138
<td> <!-- Ruby -->
1139-
<a href="./CONTRIBUTING.md">
1140-
<img align="center" height="25" src="https://cdn.jsdelivr.net/gh/devicons/devicon/icons/github/github-original.svg" />
1139+
<a href="./src/ruby/min_max_recursivo.rb">
1140+
<img align="center" height="25" src="https://cdn.jsdelivr.net/gh/devicons/devicon/icons/ruby/ruby-original.svg" />
11411141
</a>
11421142
</td>
11431143
<td> <!-- JavaScript -->
@@ -1718,8 +1718,8 @@ Com o objetivo de alcançar uma abrangência maior e encorajar novas pessoas a c
17181718
</a>
17191719
</td>
17201720
<td> <!-- Ruby -->
1721-
<a href="./CONTRIBUTING.md">
1722-
<img align="center" height="25" src="https://cdn.jsdelivr.net/gh/devicons/devicon/icons/github/github-original.svg" />
1721+
<a href="./src/ruby/deque.rb">
1722+
<img align="center" height="25" src="https://cdn.jsdelivr.net/gh/devicons/devicon/icons/ruby/ruby-original.svg" />
17231723
</a>
17241724
</td>
17251725
<td> <!-- JavaScript -->
@@ -2706,8 +2706,8 @@ Com o objetivo de alcançar uma abrangência maior e encorajar novas pessoas a c
27062706
</a>
27072707
</td>
27082708
<td> <!-- Ruby -->
2709-
<a href="./CONTRIBUTING.md">
2710-
<img align="center" height="25" src="https://cdn.jsdelivr.net/gh/devicons/devicon/icons/github/github-original.svg" />
2709+
<a href="./src/ruby/comb_sort.rb">
2710+
<img align="center" height="25" src="https://cdn.jsdelivr.net/gh/devicons/devicon/icons/ruby/ruby-original.svg" />
27112711
</a>
27122712
</td>
27132713
<td> <!-- JavaScript -->
@@ -2822,8 +2822,8 @@ Com o objetivo de alcançar uma abrangência maior e encorajar novas pessoas a c
28222822
</a>
28232823
</td>
28242824
<td> <!-- Ruby -->
2825-
<a href="./CONTRIBUTING.md">
2826-
<img align="center" height="25" src="https://cdn.jsdelivr.net/gh/devicons/devicon/icons/github/github-original.svg" />
2825+
<a href="./src/ruby/gnome_sort.rb">
2826+
<img align="center" height="25" src="https://cdn.jsdelivr.net/gh/devicons/devicon/icons/ruby/ruby-original.svg" />
28272827
</a>
28282828
</td>
28292829
<td> <!-- JavaScript -->
@@ -3578,8 +3578,8 @@ Com o objetivo de alcançar uma abrangência maior e encorajar novas pessoas a c
35783578
</a>
35793579
</td>
35803580
<td> <!-- Ruby -->
3581-
<a href="./CONTRIBUTING.md">
3582-
<img align="center" height="25" src="https://cdn.jsdelivr.net/gh/devicons/devicon/icons/github/github-original.svg" />
3581+
<a href="./src/ruby/calculate_pi.rb">
3582+
<img align="center" height="25" src="https://cdn.jsdelivr.net/gh/devicons/devicon/icons/ruby/ruby-original.svg" />
35833583
</a>
35843584
</td>
35853585
<td> <!-- JavaScript -->

src/ruby/algoritmo_dijkstra.rb

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# Algoritmo de Dijkstra em Ruby
2+
# Carlos Alves
3+
# https://github.com/EuCarlos
4+
5+
def dijkstra(matrix, n)
6+
visited = Array.new(n)
7+
8+
for i in 1..n-1
9+
min, min_value = nil, nil
10+
11+
for j in 1..n-1
12+
if !visited[j] && (!min_value || matrix[j][0] < min_value)
13+
min = j
14+
min_value = matrix[j][0]
15+
end
16+
end
17+
18+
visited[min] = true
19+
20+
for j in 1..n-1
21+
if matrix[min][0] + matrix[min][j] < matrix[j][0]
22+
matrix[j][0] = matrix[min][0] + matrix[min][j]
23+
end
24+
end
25+
end
26+
end
27+
28+
def main
29+
n_vertices = 5
30+
31+
matrix = []
32+
ignore = 4294967295
33+
34+
matrix.push([0, 6, 10, ignore, ignore])
35+
matrix.push([6, 0, ignore, 2, ignore])
36+
matrix.push([10, ignore, 0, 1, 3])
37+
matrix.push([ignore, 2, 1, 0, 8])
38+
matrix.push([ignore, ignore, 3, 8, 0])
39+
40+
matrix.to_a.each {|row| puts row.inspect}
41+
42+
dijkstra(matrix, n_vertices)
43+
44+
puts "============================================================"
45+
puts "Total caminho mais curto do vertice 0 ao 4: #{matrix[4][0]}"
46+
puts "============================================================"
47+
48+
matrix.to_a.each {|row| puts row.inspect}
49+
end
50+
51+
main()

src/ruby/calculate_pi.rb

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Calculo do PI (Leibniz) em Ruby
2+
# Carlos Alves
3+
# https://github.com/EuCarlos
4+
5+
def calculate_pi(number)
6+
denominator = 1.0
7+
operation = 1.0
8+
pi = 0
9+
10+
for count in 1..number
11+
pi += operation * (4.0 / denominator)
12+
denominator += 2.0
13+
operation *= -1.0
14+
end
15+
16+
return pi
17+
end
18+
19+
def main
20+
terms = [10, 1000, 100000, 10000000]
21+
22+
terms.each do |term|
23+
puts "PI #{term}: #{calculate_pi(term)}"
24+
end
25+
end
26+
27+
main()

src/ruby/comb_sort.rb

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# Algoritmo de Comb sort em Ruby
2+
# Carlos Alves
3+
# https://github.com/EuCarlos
4+
5+
def comb_sort(arr)
6+
gap = arr.length
7+
shrink = 1.3
8+
swapped = true
9+
10+
while gap > 1 || swapped do
11+
gap = (gap / shrink).floor
12+
gap = 1 if gap < 1
13+
swapped = false
14+
15+
for i in 0..(arr.length - gap)-1
16+
if arr[i] > arr[i + gap]
17+
arr[i], arr[i + gap] = arr[i + gap], arr[i]
18+
swapped = true
19+
end
20+
end
21+
end
22+
23+
return arr
24+
end
25+
26+
array = [86, 16, 10, 39, 49, 6, 32, 57, 72, 29]
27+
print "Lista: #{array} \n"
28+
print "Lista Ordenada: #{comb_sort(array)}"

src/ruby/deque.rb

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
# Algoritmo Deque em Ruby
2+
# Carlos Alves
3+
# https://github.com/EuCarlos
4+
5+
class Deque
6+
attr_accessor :deque
7+
8+
def initialize(deque = [])
9+
self.deque = deque
10+
end
11+
12+
# adds an item in front of the deque:
13+
def insert_front(value)
14+
self.deque.unshift(value)
15+
end
16+
17+
# adds an item in back of the deque:
18+
def insert_last(value)
19+
self.deque.push(value)
20+
end
21+
22+
# adds an item in front of the deque:
23+
def delete_front()
24+
self.deque.shift() if self.deque.length != 0
25+
end
26+
27+
# adds an item in back of the deque:
28+
def delete_last()
29+
self.deque.pop() if self.deque.length != 0
30+
end
31+
32+
# gets the item at the front of the queue:
33+
def read_front()
34+
return self.deque.length >= 1 ? self.deque[0] : 'Lista está vazia'
35+
end
36+
37+
# gets the item at the last of the queue:
38+
def read_rear()
39+
return self.deque.length >= 1 ? self.deque[-1] : 'Lista está vazia'
40+
end
41+
42+
# checks whether deque is full or not
43+
def is_full()
44+
result = self.deque.length >= 1 ? 'está' : 'não esta'
45+
return "Lista #{result} cheia!"
46+
end
47+
48+
# checks whether deque is empty or not
49+
def is_empty()
50+
result = self.deque.length == 0 ? 'está' : 'não esta'
51+
return "Lista #{result} vazia"
52+
end
53+
end
54+
55+
def main
56+
# test with full list
57+
deque = Deque.new([86, 16, 10, 39])
58+
59+
deque.insert_front(54)
60+
deque.insert_last(91)
61+
62+
deque.delete_front
63+
deque.delete_last
64+
65+
puts deque.read_front
66+
puts deque.read_rear
67+
68+
puts deque.is_full
69+
puts deque.is_empty
70+
71+
# test with empty list
72+
deque_empty = Deque.new
73+
puts deque_empty.is_full
74+
puts deque_empty.is_empty
75+
end
76+
77+
main()

src/ruby/factorial.rb

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
2+
# Fatorial em Ruby
3+
# Carlos Alves
4+
# https://github.com/EuCarlos
5+
6+
def factorial(value)
7+
aux = 1
8+
for i in 1..value
9+
aux *= i
10+
end
11+
12+
return aux
13+
end
14+
15+
def main
16+
for number in 0..10
17+
puts "#{number}! = #{factorial(number)}"
18+
end
19+
end
20+
21+
main()

src/ruby/gnome_sort.rb

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# Algoritmo Gnome sort em Ruby
2+
# Carlos Alves
3+
# https://github.com/EuCarlos
4+
5+
def gnome_sort(array)
6+
count = 0
7+
8+
while count < array.length - 1 do
9+
if array[count] > array[count + 1]
10+
array[count + 1], array[count] = array[count], array[count + 1]
11+
count -= 2 if count > 0
12+
end
13+
count += 1
14+
end
15+
16+
return array
17+
end
18+
19+
def main
20+
array = [10, 8, 4, 3, 1, 9, 0, 2, 7, 5, 6]
21+
print(gnome_sort(array))
22+
end
23+
24+
main()

src/ruby/interpolation_search.rb

Lines changed: 33 additions & 0 deletions
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()

0 commit comments

Comments
 (0)