From: "Frykholm, Niklas" Date: 2001-10-09T17:57:41+09:00 Subject: [ruby-talk:22290] Re: [Newbie] Switching from Perl : suffling [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). // Niklas