Skip to content

Add Bogosort in Factor #426

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 7 commits into from
Oct 8, 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
3 changes: 2 additions & 1 deletion CONTRIBUTORS.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
- PaddyKe
- nic-hartley
4 changes: 4 additions & 0 deletions contents/bogo_sort/bogo_sort.md
Original file line number Diff line number Diff line change
Expand Up @@ -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" %}
Expand Down Expand Up @@ -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" %}
Expand Down
25 changes: 25 additions & 0 deletions contents/bogo_sort/code/factor/bogo_sort.factor
Original file line number Diff line number Diff line change
@@ -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 <iota> >array randomize ! generate a random array to demo
dup . ! show the array
bogosort ! bogosort it
. ! show it again