diff --git a/book.json b/book.json index 9da5e002f..d13710307 100644 --- a/book.json +++ b/book.json @@ -132,10 +132,10 @@ "lang": "lisp", "name": "Lisp" }, - { - "lang": "nim", + { + "lang": "nim", "name": "Nim" - } + } ] } } diff --git a/contents/bogo_sort/bogo_sort.md b/contents/bogo_sort/bogo_sort.md index 59fa35ae2..3c052d470 100644 --- a/contents/bogo_sort/bogo_sort.md +++ b/contents/bogo_sort/bogo_sort.md @@ -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. @@ -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 %} diff --git a/contents/bogo_sort/code/nim/bogo_sort.nim b/contents/bogo_sort/code/nim/bogo_sort.nim new file mode 100644 index 000000000..aca98f749 --- /dev/null +++ b/contents/bogo_sort/code/nim/bogo_sort.nim @@ -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)