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

--- On Mon, 4/28/08, Raul Miller <[EMAIL PROTECTED]> wrote:

> I do not know if this is an "improvement" but here
> is another approach;
>    repair=: ] C.~ _2 (([EMAIL PROTECTED]: /:~)\ # (<\ [EMAIL PROTECTED])) ]
>    bubsort=: repair @ ({. , [EMAIL PROTECTED])^:_
>    bubsort C
> 0 0 2 4 4 5 6 7 9 9
> 
> Actually, I am not totally sure if this counts as bubble
> sort -- it's the same basic principle but slightly more
> parallel.

/:~ shouldn't be in a "sort" algorithm. Let's see.

   bi=: <\ [EMAIL PROTECTED]     NB. boxed indecies
   uno=: [EMAIL PROTECTED]: /:~  NB. un-ordered
   _2 (uno\ # bi) C
+---+---+
|0 1|2 3|
+---+---+
   C C.~ _2 (uno\ # bi) C
5 6 2 9 4 9 0 7 0 4
   C
6 5 9 2 4 9 0 7 0 4
   
Isn't "uno" just "greater than"?

   bi=: <\ [EMAIL PROTECTED]     NB. boxed indecies
   gt=: >/          NB. greater than
   _2 (gt\ # bi) C
+---+---+
|0 1|2 3|
+---+---+
   C C.~ _2 (gt\ # bi) C
5 6 2 9 4 9 0 7 0 4

   repair=: C.~ _2&(gt\ # bi)
   repair C
5 6 2 9 4 9 0 7 0 4

Another shot at "repair" is flip as we go

   fig=: |.^:(>/)   NB. flip if greater
   ;_2 <@fig\ C
5 6 2 9 4 9 0 7 0 4



      
____________________________________________________________________________________
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

Reply via email to