Skip to content

Added Bubble Sort in Crystal #319

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
merged 4 commits into from
Aug 2, 2018
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
2 changes: 2 additions & 0 deletions CONTRIBUTORS.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,5 @@ Max Weinstein
Gibus Wearing Brony
<br>
Arun Sahadeo
<br>
NIFR91
5 changes: 5 additions & 0 deletions book.json
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,12 @@
{
"lang": "lua",
"name": "Lua"
},
{
"lang": "crystal",
"name": "Crystal"
}

]
}
}
Expand Down
4 changes: 4 additions & 0 deletions contents/bubble_sort/bubble_sort.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ This means that we need to go through the vector $$\mathcal{O}(n^2)$$ times with
[import:2-13, lang:"ti-83_basic"](code/ti83basic/BUBLSORT.txt)
{% sample lang="ruby" %}
[import:3-13, lang:"ruby"](code/ruby/bubble.rb)
{% sample lang="crystal" %}
[import:1-11, lang:"crystal"](code/crystal/bubble.cr)
{% endmethod %}

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

<script>
Expand Down
20 changes: 20 additions & 0 deletions contents/bubble_sort/code/crystal/bubble.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
def bubble_sort(arr)
arr = arr.dup
(0 ... arr.size).each do
(0 ... arr.size-1).each do |k|
if arr[k] > arr[k+1]
arr[k+1],arr[k] = arr[k],arr[k+1]
end
end
end
arr
end

def main
number = 10.times.map{rand(0..1_000)}.to_a
pp "The array before sorting is #{number}"
number = bubble_sort number
pp "The array after sorting is #{number}"
end

main