sorting.bubble: adding Bubblesort.

db4
John Benediktsson 2014-11-29 18:18:59 -08:00
parent bcbd6c55e4
commit 0d3fc9d976
5 changed files with 37 additions and 0 deletions

View File

@ -0,0 +1 @@
John Benediktsson

View File

@ -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

View File

@ -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! ;

View File

@ -0,0 +1 @@
Bubblesort

View File

@ -0,0 +1,2 @@
collections
algorithms