]> gitweb.factorcode.org Git - factor.git/commitdiff
sorting.bubble: adding Bubblesort.
authorJohn Benediktsson <mrjbq7@gmail.com>
Sun, 30 Nov 2014 02:18:59 +0000 (18:18 -0800)
committerJohn Benediktsson <mrjbq7@gmail.com>
Sun, 30 Nov 2014 02:18:59 +0000 (18:18 -0800)
extra/sorting/bubble/authors.txt [new file with mode: 0644]
extra/sorting/bubble/bubble-tests.factor [new file with mode: 0644]
extra/sorting/bubble/bubble.factor [new file with mode: 0644]
extra/sorting/bubble/summary.txt [new file with mode: 0644]
extra/sorting/bubble/tags.txt [new file with mode: 0644]

diff --git a/extra/sorting/bubble/authors.txt b/extra/sorting/bubble/authors.txt
new file mode 100644 (file)
index 0000000..e091bb8
--- /dev/null
@@ -0,0 +1 @@
+John Benediktsson
diff --git a/extra/sorting/bubble/bubble-tests.factor b/extra/sorting/bubble/bubble-tests.factor
new file mode 100644 (file)
index 0000000..30b956b
--- /dev/null
@@ -0,0 +1,7 @@
+USING: kernel tools.test ;
+IN: sorting.bubble
+
+{ { } } [ { } dup natural-bubble-sort! ] unit-test
+{ { 1 } } [ { 1 } dup natural-bubble-sort! ] unit-test
+{ { 1 2 3 4 5 } } [ { 1 4 2 5 3 } dup natural-bubble-sort! ] unit-test
+
diff --git a/extra/sorting/bubble/bubble.factor b/extra/sorting/bubble/bubble.factor
new file mode 100644 (file)
index 0000000..3caa6c6
--- /dev/null
@@ -0,0 +1,26 @@
+! Copyright (C) 2014 John Benediktsson
+! See http://factorcode.org/license.txt for BSD license
+
+USING: kernel locals math math.order math.ranges sequences
+sequences.private ;
+
+IN: sorting.bubble
+
+<PRIVATE
+
+:: (bubble-sort!) ( seq quot: ( obj1 obj2 -- <=> ) -- )
+    seq length [
+        1 - f over [0,b) [| i |
+            i i 1 + [ seq nth-unsafe ] bi@ 2dup quot call +gt+ =
+            [ i 1 + i [ seq set-nth-unsafe ] bi-curry@ bi* drop t ]
+            [ 2drop ] if
+        ] each
+    ] loop drop ; inline
+
+PRIVATE>
+
+: bubble-sort! ( seq quot: ( obj1 obj2 -- <=> ) -- )
+    over length 2 < [ 2drop ] [ (bubble-sort!) ] if ; inline
+
+: natural-bubble-sort! ( seq -- )
+    [ <=> ] bubble-sort! ;
diff --git a/extra/sorting/bubble/summary.txt b/extra/sorting/bubble/summary.txt
new file mode 100644 (file)
index 0000000..4021f8c
--- /dev/null
@@ -0,0 +1 @@
+Bubblesort
diff --git a/extra/sorting/bubble/tags.txt b/extra/sorting/bubble/tags.txt
new file mode 100644 (file)
index 0000000..1e3d675
--- /dev/null
@@ -0,0 +1,2 @@
+collections
+algorithms