Perl's default sort algorithm is mergesort (perlsec: Algorithmic Complexity Attacks) from 5.8.0 version (Although there may be some optimizations or different behavior if an array length is less than 18? e.g. RFC: Simple switches for 'sort' and list 'reverse').@array = sort { 0.5 <=> rand } @array;
OUTPUT:#!/usr/bin/perl use warnings; use strict; $\ = $/; srand 1; # https://perlmonks.org/?node_id=1905 # randomly permutate @array in place my $fisher_yates_shuffle = sub { my $array = shift; my $i = @$array; while ( --$i ) { my $j = int rand( $i+1 ); @$array[$i,$j] = @$array[$j,$i]; } }; my $pseudoshuffle_with_sort = sub { my( $ref_array ) = shift; @{ $ref_array } = sort { 0.5 <=> rand } @{ $ref_array }; }; my $size_of_array = 9; my $A = 'A'; my @array = map { $A ++ } 1 .. $size_of_array; my $times = 10000; my @distribution; @distribution = &distribution_of_occurrences( \@array, $pseudoshuffle_with_sort, $times ); &pretty_squeeze( \@distribution, \@array ); print for '1)', @distribution; @distribution = &distribution_of_occurrences( \@array, $fisher_yates_shuffle, $times ); &pretty_squeeze( \@distribution, \@array ); print for '2)', @distribution; sub distribution_of_occurrences{ my( $ref_array, $shuffle_sub, $times ) = @_; my @distribution; for my $i ( 1 .. $times ){ my @copy_of_array = @{ $ref_array }; $shuffle_sub->( \@copy_of_array ); my $j = 0; map { $distribution[ $j ++ ] .= " " . $_ } @copy_of_array; } return @distribution = map { join ' ', sort { length $a <=> length $b || $a cmp $b } split } @distribution; } sub pretty_squeeze{ my( $ref_distribution, $ref_array ) = @_; my $times_approx = split ' ', $ref_distribution->[ 0 ]; my $del_times = int $times_approx / @{ $ref_array } / 4; s/(\b\w+\b) \K (?:[ ]\1){0,$del_times}//xg for @{ $ref_distributio +n }; my $k = -1; my $rx_every_second = join '|', grep $k ++ % 2 == 0, @{ $ref_array + }; s/\b(?:$rx_every_second)\b/./g for @{ $ref_distribution }; }
One can vary a value of variable $size_of_array to see how columns change with different array sizes. In the output example (with $size_of_array = 9) we can see non-vertical columns in '1)' (pseudo-shuffle with sort) and vertical columns in '2)' (Fisher-Yates shuffle). This data means that the first element of an array more often jumped to the end of an array, and the last element jumped more often to the beginning of an array.1) A A A . . . C C C . . . E E E E E . . . . . G G G G G . . . . . I I I +I I I I I I I A A A . . . C C C . . . E E E E E . . . . . G G G G G G . . . . . . I +I I I I I I A A A . . . C C C . . . E E E E E . . . . . G G G G G G G . . . . . . +. I I I I I I A A A . . . C C C C . . . . E E E E E . . . . . G G G G G G . . . . . +. I I I I I A A A A . . . . C C C C . . . . E E E E E . . . . . G G G G G . . . . +. I I I I A A A A A . . . . . C C C C C . . . . . E E E E E . . . . . G G G G . +. . . I I I A A A A A A . . . . . . C C C C C C . . . . . . E E E E . . . . G G G +. . . I I A A A A A A A . . . . . . . C C C C C C C . . . . . . . E E E . . . G +G G . . . I I A A A A A A A . . . . . . . C C C C C C C . . . . . . . E E E . . . G +G . . I I 2) A A A A . . . . C C C C . . . . . E E E E . . . . . G G G G G . . . . +I I I I I A A A A . . . . C C C C C . . . . . E E E E E . . . . G G G G G . . . +. . I I I I A A A A A . . . . . C C C C . . . . . E E E E . . . . G G G G . . . . +. I I I I A A A A A . . . . C C C C C . . . . E E E E E . . . . G G G G . . . . +I I I I I A A A A A . . . . . C C C C C . . . . E E E E E . . . . G G G G . . . +. . I I I I I A A A A A . . . . C C C C . . . . . E E E E E . . . . . G G G G . . . +. I I I I I A A A A . . . . C C C C . . . . . E E E E . . . . . G G G G . . . . . +I I I I A A A A . . . . . C C C C . . . . . E E E E . . . . G G G G G . . . . +I I I I A A A A . . . . . C C C C . . . . E E E E . . . . . G G G G G . . . . +. I I I I
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: RFC: Kinda pseudo-shuffle using sort
by ikegami (Patriarch) on Mar 15, 2021 at 10:22 UTC | |
|
Re: RFC: Kinda pseudo-shuffle using sort
by haukex (Archbishop) on Mar 15, 2021 at 10:23 UTC | |
|
Re: RFC: Kinda pseudo-shuffle using sort
by bliako (Abbot) on Mar 16, 2021 at 08:50 UTC | |
by rsFalse (Chaplain) on Mar 16, 2021 at 09:29 UTC |