Skip to content

Implemented Bogo Sort in Crystal #530

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 1 commit into from
Oct 25, 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
4 changes: 4 additions & 0 deletions contents/bogo_sort/bogo_sort.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ In code, it looks something like this:
[import:93-113, lang:"asm-x64"](code/asm-x64/bogo_sort.s)
{% sample lang="lisp" %}
[import:20-24, lang:"lisp"](code/clisp/bogo-sort.lisp)
{% sample lang="crystal" %}
[import:10-14, lang:"crystal"](code/crystal/bogo.cr)
{% endmethod %}

That's it.
Expand Down Expand Up @@ -121,6 +123,8 @@ We are done here!
[import, lang:"asm-x64"](code/asm-x64/bogo_sort.s)
{% sample lang="lisp" %}
[import, lang:"lisp"](code/clisp/bogo-sort.lisp)
{% sample lang="crystal" %}
[import, lang:"crystal"](code/crystal/bogo.cr)
{% endmethod %}


Expand Down
22 changes: 22 additions & 0 deletions contents/bogo_sort/code/crystal/bogo.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
def is_sorted?(a)
0.upto(a.size - 2) do |i|
if a[i] > a[i + 1]
return false
end
end
true
end

def bogo_sort!(a)
while !is_sorted?(a)
a.shuffle!
end
end

def main
a = [1.0, 3.0, 2.0, 4.0]
bogo_sort!(a)
puts a
end

main