Skip to content

Commit 221c775

Browse files
committed
Implemented Bogo Sort in Crystal
1 parent e50c8b6 commit 221c775

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

contents/bogo_sort/bogo_sort.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ In code, it looks something like this:
5959
[import:93-113, lang:"asm-x64"](code/asm-x64/bogo_sort.s)
6060
{% sample lang="lisp" %}
6161
[import:20-24, lang:"lisp"](code/lisp/bogo-sort.lisp)
62+
{% sample lang="crystal" %}
63+
[import:17-21, lang:"crystal"](code/crystal/bogo.cr)
6264
{% endmethod %}
6365

6466
That's it.
@@ -117,6 +119,8 @@ We are done here!
117119
[import, lang:"asm-x64"](code/asm-x64/bogo_sort.s)
118120
{% sample lang="lisp" %}
119121
[import, lang:"lisp"](code/lisp/bogo-sort.lisp)
122+
{% sample lang="crystal" %}
123+
[import, lang:"crystal"](code/crystal/bogo.cr)
120124
{% endmethod %}
121125

122126

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
def is_sorted?(a)
2+
0.upto(a.size - 2) do |i|
3+
if a[i] > a[i + 1]
4+
return false
5+
end
6+
end
7+
true
8+
end
9+
10+
def shuffle!(a)
11+
0.upto(a.size - 2) do |i|
12+
r = rand(i..a.size - 1)
13+
a[i], a[r] = a[r], a[i]
14+
end
15+
end
16+
17+
def bogo_sort!(a)
18+
while !is_sorted?(a)
19+
shuffle!(a)
20+
end
21+
end
22+
23+
def main
24+
a = [1.0, 3.0, 2.0, 4.0]
25+
bogo_sort!(a)
26+
puts a
27+
end
28+
29+
main

0 commit comments

Comments
 (0)