Initial data commit
This commit is contained in:
parent
72d218235f
commit
f23f22d71c
199087 changed files with 3378941 additions and 0 deletions
|
|
@ -0,0 +1,18 @@
|
|||
USING: kernel locals math math.ranges sequences ;
|
||||
|
||||
:: cocktail-sort! ( seq -- seq' )
|
||||
f :> swapped! ! bind false to mutable lexical variable 'swapped'. This must be done outside both while quotations so it is in scope of both.
|
||||
[ swapped ] [ ! is swapped true? Then execute body quotation. 'do' executes body quotation before predicate on first pass.
|
||||
f swapped! ! set swapped to false
|
||||
seq length 2 - [| i | ! for each i in 0 to seq length - 2 do
|
||||
i i 1 + [ seq nth ] bi@ > ! is element at index i greater than element at index i + 1?
|
||||
[ i i 1 + seq exchange t swapped! ] when ! if so, swap them and set swapped to true
|
||||
] each-integer
|
||||
swapped [ ! skip to end of loop if swapped is false
|
||||
seq length 2 - 0 [a,b] [| i | ! for each i in seq length - 2 to 0 do
|
||||
i i 1 + [ seq nth ] bi@ > ! is element at index i greater than element at index i + 1?
|
||||
[ i i 1 + seq exchange t swapped! ] when ! if so, swap them and set swapped to true
|
||||
] each
|
||||
] when
|
||||
] do while
|
||||
seq ; ! return the sequence
|
||||
|
|
@ -0,0 +1,21 @@
|
|||
USING: fry kernel math math.ranges namespaces sequences ;
|
||||
|
||||
SYMBOL: swapped?
|
||||
|
||||
: dupd+ ( m obj -- m n obj ) [ dup 1 + ] dip ;
|
||||
|
||||
: 2nth ( n seq -- elt1 elt2 ) dupd+ [ nth ] curry bi@ ;
|
||||
|
||||
: ?exchange ( n seq -- )
|
||||
2dup 2nth > [ dupd+ exchange swapped? on ] [ 2drop ] if ;
|
||||
|
||||
: cocktail-pass ( seq forward? -- )
|
||||
'[ length 2 - 0 _ [ swap ] when [a,b] ] [ ] bi
|
||||
[ ?exchange ] curry each ;
|
||||
|
||||
: (cocktail-sort!) ( seq -- seq' )
|
||||
swapped? off dup t cocktail-pass
|
||||
swapped? get [ dup f cocktail-pass ] when ;
|
||||
|
||||
: cocktail-sort! ( seq -- seq' )
|
||||
[ swapped? get ] [ (cocktail-sort!) ] do while ;
|
||||
Loading…
Add table
Add a link
Reference in a new issue