Skip to content

Commit 1ffa3b8

Browse files
Nic Hartleyjiegillet
Nic Hartley
authored andcommitted
Add Bogosort in Factor (#426)
* Add Factor bogosort * added missing information * added explanatory comment * complying with .editorconfig from #426 * added demo of usage
1 parent 8c78043 commit 1ffa3b8

File tree

3 files changed

+31
-1
lines changed

3 files changed

+31
-1
lines changed

CONTRIBUTORS.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,5 @@ This file lists everyone, who contributed to this repo and wanted to show up her
3535
- Cyrus Burt
3636
- Patrik Tesarik
3737
- Ken Power
38-
- PaddyKe
38+
- PaddyKe
39+
- nic-hartley

contents/bogo_sort/bogo_sort.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ In code, it looks something like this:
4545
[import:16-18, lang:"nim"](code/nim/bogo_sort.nim)
4646
{% sample lang="ruby" %}
4747
[import:12-16, lang:"ruby"](code/ruby/bogo.rb)
48+
{% sample lang="factor" %}
49+
[import:10-12, lang:"factor"](code/factor/bogo_sort.factor)
4850
{% sample lang="f90" %}
4951
[import:24-32, lang:"fortran"](code/fortran/bogo.f90)
5052
{% sample lang="st" %}
@@ -93,6 +95,8 @@ We are done here!
9395
[import, lang:"nim"](code/nim/bogo_sort.nim)
9496
{% sample lang="ruby" %}
9597
[import, lang:"ruby"](code/ruby/bogo.rb)
98+
{% sample lang="factor" %}
99+
[import, lang:"factor"](code/factor/bogo_sort.factor)
96100
{% sample lang="f90" %}
97101
[import, lang:"fortran"](code/fortran/bogo.f90)
98102
{% sample lang="st" %}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
! There's no built-in "is sorted" function, so let's make one:
2+
USING: locals ;
3+
: sorted? ( seq -- ? )
4+
2 clump ! split it up into overlapping pairs
5+
! so now, for example, { 1 2 3 } has turned into { { 1 2 } { 2 3 } }
6+
! and now we make sure that for every pair, the latter is >= the former
7+
[| pair | pair first pair last <= ] all?
8+
;
9+
10+
USING: random ;
11+
: bogosort ( seq -- seq' )
12+
! `dup` duplicates the array, because `sorted?` pops its reference to it
13+
! randomize works in-place
14+
! so we `randomize` `until` it's `sorted?`
15+
[ dup sorted? ] [ randomize ] until
16+
;
17+
18+
! WARNING: Increasing this number beyond 5 or so will make this take a very long time.
19+
! That said, if you have an afternoon to kill...
20+
5 <iota> >array randomize ! generate a random array to demo
21+
dup . ! show the array
22+
bogosort ! bogosort it
23+
. ! show it again
24+
25+

0 commit comments

Comments
 (0)