From: Pierre-Charles David Date: 2001-10-09T18:01:22+09:00 Subject: [ruby-talk:22291] Re: [Newbie] Switching from Perl : suffling Frykholm, Niklas wrote: > [Pierre-Charles David] > >>One of my teachers once showed me this method, which I find quite >>elegant (although perhaps not very efficient because of the arrays >>creation, but still O(n) ): >> >>module Enumerable >> def shuffle >> # Associate a random number to each element >> pairs = self.collect { |item| [rand(), item] } >> # Sort the pairs according to the random part >> # and extract the original data >> (pairs.sort { |x, y| x[0] <=> y[0] }).collect { |pair| pair[1] } >> end >>end >> > > Sorting is O(n log n). Yes! How stupid of me... -- Pierre-Charles David (pcdavid emn fr) Computer Science PhD Student, �cole des Mines de Nantes, France Homepage: http://purl.org/net/home/pcdavid