0: # for those times when Perl's built-in sort is just too fast
1:
2: use strict;
3:
4: my @data = qw(7 6 5 4 3 2 1 0) ;
5:
6: rpsort(\@data);
7: print "$_\t" foreach (@data);
8:
9: exit;
10:
11: # a random permutation sort
12: # ref. Perl Cookbook recipe 4.17 for the shuffle
13:
14: sub rpsort {
15: my $list = shift;
16: my ($k,$j);
17:
18: try_again:
19: for ($j = @$list; --$j; ) {
20: $k = int rand($j+1);
21: next if ($j == $k);
22: @$list[$j, $k] = @$list[$k, $j];
23: }
24:
25: for ($j = 0 ; $j < (@$list -1); $j++) {
26: goto try_again if (@$list[$j] > @$list[$j+1]);
27: }
28: }
In reply to Random Permutation Sort by cforde
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |