Skip to content

Commit 7bc2b14

Browse files
NIFR91jiegillet
authored andcommitted
Added Bubble Sort in Crystal (#319)
* Added Bubble Sort in Crystal * added crystal-lang in book.json
1 parent 136027c commit 7bc2b14

File tree

4 files changed

+31
-0
lines changed

4 files changed

+31
-0
lines changed

CONTRIBUTORS.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,3 +49,5 @@ Max Weinstein
4949
Gibus Wearing Brony
5050
<br>
5151
Arun Sahadeo
52+
<br>
53+
NIFR91

book.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,12 @@
116116
{
117117
"lang": "lua",
118118
"name": "Lua"
119+
},
120+
{
121+
"lang": "crystal",
122+
"name": "Crystal"
119123
}
124+
120125
]
121126
}
122127
}

contents/bubble_sort/bubble_sort.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ This means that we need to go through the vector $$\mathcal{O}(n^2)$$ times with
4040
[import:2-13, lang:"ti-83_basic"](code/ti83basic/BUBLSORT.txt)
4141
{% sample lang="ruby" %}
4242
[import:3-13, lang:"ruby"](code/ruby/bubble.rb)
43+
{% sample lang="crystal" %}
44+
[import:1-11, lang:"crystal"](code/crystal/bubble.cr)
4345
{% endmethod %}
4446

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

9094
<script>
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
def bubble_sort(arr)
2+
arr = arr.dup
3+
(0 ... arr.size).each do
4+
(0 ... arr.size-1).each do |k|
5+
if arr[k] > arr[k+1]
6+
arr[k+1],arr[k] = arr[k],arr[k+1]
7+
end
8+
end
9+
end
10+
arr
11+
end
12+
13+
def main
14+
number = 10.times.map{rand(0..1_000)}.to_a
15+
pp "The array before sorting is #{number}"
16+
number = bubble_sort number
17+
pp "The array after sorting is #{number}"
18+
end
19+
20+
main

0 commit comments

Comments
 (0)