[Jprogramming] Bubble sort

Oleg Kobchenko olegykj at yahoo.com
Tue Apr 29 00:42:29 HKT 2008


--- On Mon, 4/28/08, Oleg Kobchenko <olegykj at yahoo.com> wrote:

> I wanted to model bubble sort.
> Here's a first shot. Any improvements?
> 
>    ]C=. 10 ?.@$ 10
> 6 5 9 2 4 9 0 7 0 4
>    /:~ C
> 0 0 2 4 4 5 6 7 9 9
>    <./ C
> 0
>    (([ ,`(,~)@.> {.@]) , }.@])/ C
> 0 6 5 9 2 4 9 0 7 4
>    (([ ,`(,~)@.> {.@]) , }.@])/^:_ C
> 0 0 2 4 4 5 6 7 9 9

(slightly more compact above)
     ((,~`, at .< {.@]) , }.@])/^:_

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
   ({. , $:@}.)@(((,~`, at .< {.@]) , }.@])/)^:(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


More information about the Programming mailing list