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?
Perl, , "". , , , . , - , , . - , , .
, , Fisher-Yates.
, , .
$ perldoc List::Util ⋮ shuffle LIST Returns the elements of LIST in a random order @cards = shuffle 0..51 # 0..51 in a random order ⋮
.
, . Perl sort . , ! "foo" lt "bar", - "bar" lt "foo". , - .
sort
"foo" lt "bar"
"bar" lt "foo"
perl sort
. ( , $x [1] , $x [2], , , ), .
ETA: . 100000 FY-shuffle 10 .
-, , , sort(), , , O (n log n). , , , , .
, ? ( ) . - , , n - 1 2 ^ n , 1 n . , . , , .
, , , - . - . , /, , - ( , )?
, , , , , , , sort, AFAIK rand() . , .
rand()
There is the best Fisher-Yates Shuffle feature that doesn't use the sortbuilt-in perlfaq4: how do I randomly shuffle an array? .
@shuffled = map { $_->[1] } sort { $a->[0] <=> $b->[0] } map { [ rand(), $_ ] } @array;