diff --git a/CONTRIBUTORS.md b/CONTRIBUTORS.md index 4cf9d5069..d5ebc677d 100644 --- a/CONTRIBUTORS.md +++ b/CONTRIBUTORS.md @@ -35,4 +35,5 @@ This file lists everyone, who contributed to this repo and wanted to show up her - Cyrus Burt - Patrik Tesarik - Ken Power -- PaddyKe \ No newline at end of file +- PaddyKe +- nic-hartley diff --git a/contents/bogo_sort/bogo_sort.md b/contents/bogo_sort/bogo_sort.md index 8f61110fa..05c674b13 100644 --- a/contents/bogo_sort/bogo_sort.md +++ b/contents/bogo_sort/bogo_sort.md @@ -45,6 +45,8 @@ In code, it looks something like this: [import:16-18, lang:"nim"](code/nim/bogo_sort.nim) {% sample lang="ruby" %} [import:12-16, lang:"ruby"](code/ruby/bogo.rb) +{% sample lang="factor" %} +[import:10-12, lang:"factor"](code/factor/bogo_sort.factor) {% sample lang="f90" %} [import:24-32, lang:"fortran"](code/fortran/bogo.f90) {% sample lang="st" %} @@ -93,6 +95,8 @@ We are done here! [import, lang:"nim"](code/nim/bogo_sort.nim) {% sample lang="ruby" %} [import, lang:"ruby"](code/ruby/bogo.rb) +{% sample lang="factor" %} +[import, lang:"factor"](code/factor/bogo_sort.factor) {% sample lang="f90" %} [import, lang:"fortran"](code/fortran/bogo.f90) {% sample lang="st" %} diff --git a/contents/bogo_sort/code/factor/bogo_sort.factor b/contents/bogo_sort/code/factor/bogo_sort.factor new file mode 100644 index 000000000..44515b56a --- /dev/null +++ b/contents/bogo_sort/code/factor/bogo_sort.factor @@ -0,0 +1,25 @@ +! There's no built-in "is sorted" function, so let's make one: +USING: locals ; +: sorted? ( seq -- ? ) + 2 clump ! split it up into overlapping pairs + ! so now, for example, { 1 2 3 } has turned into { { 1 2 } { 2 3 } } + ! and now we make sure that for every pair, the latter is >= the former + [| pair | pair first pair last <= ] all? +; + +USING: random ; +: bogosort ( seq -- seq' ) + ! `dup` duplicates the array, because `sorted?` pops its reference to it + ! randomize works in-place + ! so we `randomize` `until` it's `sorted?` + [ dup sorted? ] [ randomize ] until +; + +! WARNING: Increasing this number beyond 5 or so will make this take a very long time. +! That said, if you have an afternoon to kill... +5 >array randomize ! generate a random array to demo +dup . ! show the array +bogosort ! bogosort it +. ! show it again + +