From: Fabian Streitel Date: 2009-07-12T01:14:16+09:00 Subject: Re: one line sorting --0015174c15f01c1d5d046e7061d8 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Well, you could just do [7,4,9,55,98,11,42].sort ;-) That's much more optimized than anything else you can do in one line... What you described is called a "Selection sort" ( http://en.wikipedia.org/wiki/Selection_sort) If you really must implement it yourself (why in one line?) You could try something like this: 0.upto(n.size-1) do |i| lowest_idx =3D i + n[i..-1].index(n[i..-1].min) n[lowest_idx], n[i] =3D n[i], n[lowest_idx] end Which is already pretty hackishly short and inefficient. ;-) Greetz! 2009/7/11 Haris Bogdanovi=C4=87 > Hi. > > I would like to implement a sorting algorithm in one line of code if > possible. > > n=3D[7,4,9,55,98,11,42] > n.inject(n[0]) {|min, i| min=3Di if i > I first find lowest number in list and then would like to replace it with > first element. After that I need to start from the second element, find t= he > lowest element, replace it with that second element and so on till I reac= h > the end of a list. > I don't know how this sorting algorithm is called ? > > I managed to find the minimum in the above code. What next ? How to repla= ce > two elements ? > The result is a number object. Where do I (in general) put a dot in above > expression to perform some methods on that object (when expression has a > block {} of code in itself ) ? > > Can you also show me that for a quick sort ? > > Thanks > Haris > > > > --0015174c15f01c1d5d046e7061d8--