Skip to content

Commit 0ca5ac6

Browse files
authored
Make Bogosort in Nim more idiomatic (#783)
1 parent 153317d commit 0ca5ac6

File tree

1 file changed

+16
-17
lines changed

1 file changed

+16
-17
lines changed

contents/bogo_sort/code/nim/bogo_sort.nim

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,23 @@ import random
33
randomize()
44

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

9-
proc is_sorted(a: openArray[int]): bool =
10-
for n in 1 .. len(a)-1:
11-
if a[n] > a[n-1]:
12-
return false
13-
14-
return true
9+
func is_sorted(a: openArray[int]): bool =
10+
result = true
11+
for n in 1 .. len(a)-1:
12+
if a[n] > a[n-1]:
13+
result = false
14+
break
1515

1616
proc bogo_sort(a: var openArray[int]) =
17-
while not is_sorted(a):
18-
shuffle(a)
19-
17+
while not is_sorted(a):
18+
shuffle(a)
2019

21-
var x: array[10,int] = [32,32,64,16,128,8,256,4,512,2]
22-
23-
print_array(x)
24-
bogo_sort(x)
25-
echo "\n"
26-
print_array(x)
20+
when isMainModule:
21+
var x = [32, 32, 64, 16, 128, 8, 256, 4, 512, 2]
22+
print_array(x)
23+
bogo_sort(x)
24+
echo "\n"
25+
print_array(x)

0 commit comments

Comments
 (0)