/: holds all the info you need to swap.
t=:6 15 19 12 14 19 0 17 0 14
[t0=: (<t),~ |.<@~."1 |:([,:]{~ [...@.>)/^:_ (i...@# ,:&}: /:) t
+-+-+---+---+-+-+---+---+---+--------------------------+
|8|7|6 8|5 9|4|3|2 6|1 8|0 6|6 15 19 12 14 19 0 17 0 14|
+-+-+---+---+-+-+---+---+---+--------------------------+
|.><@(C.>)/\.t0
6 15 19 12 14 19 0 17 0 14
0 15 19 12 14 19 6 17 0 14
0 0 19 12 14 19 6 17 15 14
0 0 6 12 14 19 19 17 15 14
0 0 6 12 14 19 19 17 15 14
0 0 6 12 14 19 19 17 15 14
0 0 6 12 14 14 19 17 15 19
0 0 6 12 14 14 15 17 19 19
0 0 6 12 14 14 15 17 19 19
0 0 6 12 14 14 15 17 19 19
Or simply
(C.>)/ (<t) ,~ |.<@~."1 |:([ ,: ] {~ [...@.>)/^:_ (i...@# ,:&}: /:) t
0 0 6 12 14 14 15 17 19 19
R.E Boss
> -----Oorspronkelijk bericht-----
> Van: [email protected] [mailto:programming-
> [email protected]] Namens Roger Hui
> Verzonden: dinsdag 1 september 2009 1:00
> Aan: Programming forum
> Onderwerp: Re: [Jprogramming] Selection Sort
>
> 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
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm