Skip to content

Make Bogosort in Nim more idiomatic #783

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
Nov 11, 2020
Merged
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
33 changes: 16 additions & 17 deletions contents/bogo_sort/code/nim/bogo_sort.nim
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,23 @@ import random
randomize()

proc print_array(a: openArray[int]) =
for n in 0 .. len(a)-1:
echo a[n]
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
func is_sorted(a: openArray[int]): bool =
result = true
for n in 1 .. len(a)-1:
if a[n] > a[n-1]:
result = false
break

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

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)
when isMainModule:
var x = [32, 32, 64, 16, 128, 8, 256, 4, 512, 2]
print_array(x)
bogo_sort(x)
echo "\n"
print_array(x)