What is the efficiency and quality of this shuffling algorithm?

This recent question about random sorting using C # made me wonder how I sometimes shuffled my arrays in Perl.

@shuffled = sort { rand() <=> rand() } @array;

Proposed solution to this issue of Fisher-Yates shuffle , which runs for linear time.

Question: How effective is my fragment and such a random random random case?

+5
source share
8 answers

Perl, , "". , , , . , - , , . - , , .

, , Fisher-Yates.

, , .

+9
$ perldoc List::Util
  shuffle LIST
       Returns the elements of LIST in a random order

           @cards = shuffle 0..51      # 0..51 in a random order

.

+9

, . Perl sort . , ! "foo" lt "bar", - "bar" lt "foo". , - .

+8

perl sort

. ( , $x [1] , $x [2], , , ), .

.

ETA: . 100000 FY-shuffle 10 .

+4

-, , , sort(), , , O (n log n). , , , , .

, ? ( ) . - , , n - 1 2 ^ n , 1 n . , . , , .

+4

, , , - . - . , /, , - ( , )?

, , , , , , , sort, AFAIK rand() . , .

+3

There is the best Fisher-Yates Shuffle feature that doesn't use the sortbuilt-in perlfaq4: how do I randomly shuffle an array? .

+3
source

@shuffled = map {
  $_->[1]
} sort {
  $a->[0] <=> $b->[0]
} map {
  [ rand(), $_ ]
} @array;
0
source

All Articles