Skip to content

Commit 6d7013d

Browse files
Vexatosjiegillet
authored andcommitted
Implemented Bogo Sort in Crystal (#530)
1 parent 96845d2 commit 6d7013d

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

contents/bogo_sort/bogo_sort.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,8 @@ In code, it looks something like this:
6161
[import:93-113, lang:"asm-x64"](code/asm-x64/bogo_sort.s)
6262
{% sample lang="lisp" %}
6363
[import:20-24, lang:"lisp"](code/clisp/bogo-sort.lisp)
64+
{% sample lang="crystal" %}
65+
[import:10-14, lang:"crystal"](code/crystal/bogo.cr)
6466
{% endmethod %}
6567

6668
That's it.
@@ -121,6 +123,8 @@ We are done here!
121123
[import, lang:"asm-x64"](code/asm-x64/bogo_sort.s)
122124
{% sample lang="lisp" %}
123125
[import, lang:"lisp"](code/clisp/bogo-sort.lisp)
126+
{% sample lang="crystal" %}
127+
[import, lang:"crystal"](code/crystal/bogo.cr)
124128
{% endmethod %}
125129

126130

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
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 bogo_sort!(a)
11+
while !is_sorted?(a)
12+
a.shuffle!
13+
end
14+
end
15+
16+
def main
17+
a = [1.0, 3.0, 2.0, 4.0]
18+
bogo_sort!(a)
19+
puts a
20+
end
21+
22+
main

0 commit comments

Comments
 (0)