Skip to content

Implement bubble sort in Ruby #130

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CONTRIBUTORS.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,4 @@ Max Weinstein
<br>
Gibus Wearing Brony
<br>
Arun Sahadeo
4 changes: 4 additions & 0 deletions contents/bubble_sort/bubble_sort.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ This means that we need to go through the vector $$\mathcal{O}(n^2)$$ times with
[import:1-13, lang:"swift"](code/swift/bubblesort.swift)
{% sample lang="ti83b" %}
[import:2-13, lang:"ti-83_basic"](code/ti83basic/BUBLSORT.txt)
{% sample lang="ruby" %}
[import:3-13, lang:"ruby"](code/ruby/bubble.rb)
{% endmethod %}

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

<script>
Expand Down
22 changes: 22 additions & 0 deletions contents/bubble_sort/code/ruby/bubble.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#!/usr/bin/env ruby

def bubble_sort(arr)
(0..arr.length - 1).each do
(0..arr.length - 2).each do |k|
if arr[k] > arr[k + 1]
arr[k + 1], arr[k] = arr[k], arr[k + 1]
end
end
end

return arr
end

def main
range = [200, 79, 69, 45, 32, 5, 15, 88, 620, 125]
puts "The range before sorting is #{range}"
range = bubble_sort(range)
puts "The range after sorting is #{range}"
end

main()