More faithful to the specs (actually doing interchanges).
Also using components.
ix=: C.~ <@~.@(0, (i. <./))
ss1=: ({. , $:@}.)@ix^:(*...@#)
data
6 15 19 12 14 19 0 17 0 14
ix data
0 15 19 12 14 19 6 17 0 14
ss1 data
0 0 6 12 14 14 15 17 19 19
----- Original Message -----
From: Roger Hui <[email protected]>
Date: Monday, August 31, 2009 13:03
Subject: Re: [Jprogramming] Selection Sort
To: Programming forum <[email protected]>
> A problem not worth solving is not worth solving well?
> For whatever it's worth:
>
> ss=: (i.<./) ({ , $:@((~:i...@#) # ]))^:(*...@#@]) ]
>
> ] data=: 10 ?...@$ 20
> 6 15 19 12 14 19 0 17 0 14
> ss data
> 0 0 6 12 14 14 15 17 19 19
>
>
>
> ----- Original Message -----
> From: Alex Rufon <[email protected]>
> Date: Monday, August 31, 2009 12:49
> Subject: [Jprogramming] Selection Sort
> To: Programming forum <[email protected]>
>
> > I can't sleep so I tried working on Selection Sort which states:
> >
> > First find the smallest element in the array and exchange it
> > with the element in the first position, then find the second
> > smallest element and exchange it with the element in the
> second
> > position, and continue in this way until the entire array is
> > sorted. Its asymptotic complexity is
> > O<http://rosettacode.org/wiki/O>(n2) making it inefficient
> on
> > large arrays.
> > http://rosettacode.org/wiki/Selection_sort
> >
> > The best I can do is:
> > selectionSort=: verb define
> > data=. y
> > for_xyz. y do.
> > temp=. xyz_index }. data
> > nvidx=. xyz_index + temp i. <./ temp
> > data=. ((xyz_index, nvidx) { data) (nvidx, xyz_index) } data
> > end.
> > data
> > )
> >
> >
> > NB. We generate 10 random numbers between 0 and 99 and assign
> to
> > a variable
> >
> > [data=. 10 ? 100
> >
> > 51 18 81 46 11 54 74 63 56 76
> >
> > selectionSort data
> >
> > 11 18 46 51 54 56 63 74 76 81
> >
> > I tried figuring out how to do this without looping but I
> can't.
> > Can it be done without loops?
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm