Skip to content

implemented bogo sort in nim #355

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 20, 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
6 changes: 3 additions & 3 deletions book.json
Original file line number Diff line number Diff line change
Expand Up @@ -132,10 +132,10 @@
"lang": "lisp",
"name": "Lisp"
},
{
"lang": "nim",
{
"lang": "nim",
"name": "Nim"
}
}
]
}
}
Expand Down
4 changes: 4 additions & 0 deletions contents/bogo_sort/bogo_sort.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ In code, it looks something like this:
[import:25-31, lang:"swift"](code/swift/bogosort.swift)
{% sample lang="php" %}
[import:11-16, lang:"php"](code/php/bogo_sort.php)
{% sample lang="nim" %}
[import:16-18, lang:"nim"](code/nim/bogo_sort.nim)
{% endmethod %}

That's it.
Expand Down Expand Up @@ -77,6 +79,8 @@ We are done here!
[import, lang:"swift"](code/swift/bogosort.swift)
{% sample lang="php" %}
[import, lang:"php"](code/php/bogo_sort.php)
{% sample lang="nim" %}
[import, lang:"nim"](code/nim/bogo_sort.nim)
{% endmethod %}


Expand Down
26 changes: 26 additions & 0 deletions contents/bogo_sort/code/nim/bogo_sort.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import random

randomize()

proc print_array(a: openArray[int]) =
for n in 0 .. len(a)-1:
echo a[n]

proc is_sorted(a: openArray[int]): bool =
for n in 1 .. len(a)-1:
if a[n] > a[n-1]:
return false

return true

proc bogo_sort(a: var openArray[int]) =
while not is_sorted(a):
shuffle(a)


var x: array[10,int] = [32,32,64,16,128,8,256,4,512,2]

print_array(x)
bogo_sort(x)
echo "\n"
print_array(x)