Skip to content

Commit 6a4e99b

Browse files
ArunSahadeojiegillet
authored andcommitted
Implement bubble sort in Ruby (algorithm-archivists#130)
* Add Ruby implementation of bubble sort
1 parent f24a194 commit 6a4e99b

File tree

3 files changed

+27
-0
lines changed

3 files changed

+27
-0
lines changed

CONTRIBUTORS.md

+1
Original file line numberDiff line numberDiff line change
@@ -48,3 +48,4 @@ Max Weinstein
4848
<br>
4949
Gibus Wearing Brony
5050
<br>
51+
Arun Sahadeo

contents/bubble_sort/bubble_sort.md

+4
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ This means that we need to go through the vector $$\mathcal{O}(n^2)$$ times with
3838
[import:1-13, lang:"swift"](code/swift/bubblesort.swift)
3939
{% sample lang="ti83b" %}
4040
[import:2-13, lang:"ti-83_basic"](code/ti83basic/BUBLSORT.txt)
41+
{% sample lang="ruby" %}
42+
[import:3-13, lang:"ruby"](code/ruby/bubble.rb)
4143
{% endmethod %}
4244

4345
... And that's it for the simplest bubble sort method.
@@ -81,6 +83,8 @@ Program.cs
8183
[import, lang:"swift"](code/swift/bubblesort.swift)
8284
{% sample lang="ti83b" %}
8385
[import, lang:"ti-83_basic"](code/ti83basic/BUBLSORT.txt)
86+
{% sample lang="ruby" %}
87+
[import, lang:ruby"](code/ruby/bubble.rb)
8488
{% endmethod %}
8589

8690
<script>
+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#!/usr/bin/env ruby
2+
3+
def bubble_sort(arr)
4+
(0..arr.length - 1).each do
5+
(0..arr.length - 2).each do |k|
6+
if arr[k] > arr[k + 1]
7+
arr[k + 1], arr[k] = arr[k], arr[k + 1]
8+
end
9+
end
10+
end
11+
12+
return arr
13+
end
14+
15+
def main
16+
range = [200, 79, 69, 45, 32, 5, 15, 88, 620, 125]
17+
puts "The range before sorting is #{range}"
18+
range = bubble_sort(range)
19+
puts "The range after sorting is #{range}"
20+
end
21+
22+
main()

0 commit comments

Comments
 (0)