--- On Mon, 4/28/08, Oleg Kobchenko <[EMAIL PROTECTED]> wrote:
> I wanted to model bubble sort.
> Here's a first shot. Any improvements?
>
> ]C=. 10 [EMAIL PROTECTED] 10
> 6 5 9 2 4 9 0 7 0 4
> /:~ C
> 0 0 2 4 4 5 6 7 9 9
> <./ C
> 0
> (([ ,`(,~)@.> [EMAIL PROTECTED]) , [EMAIL PROTECTED])/ C
> 0 6 5 9 2 4 9 0 7 4
> (([ ,`(,~)@.> [EMAIL PROTECTED]) , [EMAIL PROTECTED])/^:_ C
> 0 0 2 4 4 5 6 7 9 9
(slightly more compact above)
((,~`,@.< [EMAIL PROTECTED]) , [EMAIL PROTECTED])/^:_
To answer Devon's remark, I am looking for a way
to express the algorithmic pattern of bubble sort
using J's functional programming repertoire.
The above is not very bubble-sorty: it lacks the
iterative kind of "suffix" present in bubble sort
<\. i.5
+---------+-------+-----+---+-+
|0 1 2 3 4|1 2 3 4|2 3 4|3 4|4|
+---------+-------+-----+---+-+
Iterative, because unlike J suffix, it applies to
successive results rather than input.
Here are a couple of closer shots:
NB. recurse the above on tail while not empty
({. , $:@}.)@(((,~`,@.< [EMAIL PROTECTED]) , [EMAIL PROTECTED])/)^:(1<#) C
0 0 2 4 4 5 6 7 9 9
NB. successively move mins from {: to {.
>{.(( ,&.>&{. , {:@]) ((#~ ; (#~ -.)) (= <./))@>@{:)^:_ '';C
0 0 2 4 4 5 6 7 9 9
____________________________________________________________________________________
Be a better friend, newshound, and
know-it-all with Yahoo! Mobile. Try it now.
http://mobile.yahoo.com/;_ylt=Ahu06i62sR8HDtDypao8Wcj9tAcJ
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm