Sorting algorithms/Bogosort: Difference between revisions
Content deleted Content added
→{{header|Java}}: minor changes |
Underscore (talk | contribs) Added Perl. |
||
Line 88: | Line 88: | ||
} |
} |
||
}</java> |
}</java> |
||
=={{header|Perl}}== |
|||
<perl>sub bogosort |
|||
{my @l = @_; |
|||
shuffle(\@l) until in_order(@l); |
|||
return @l;} |
|||
sub in_order |
|||
{my $last = shift(@_); |
|||
foreach (@_) |
|||
{$_ >= $last or return 0; |
|||
$last = $_;} |
|||
return 1;} |
|||
sub shuffle |
|||
# This uses the algorithm described at: |
|||
# http://en.wikipedia.org/wiki/Fisher-Yates_shuffle#The_modern_algorithm |
|||
{our @l; local *l = shift; |
|||
# @l is now an alias of the original argument. |
|||
for (my $n = $#l ; $n ; --$n) |
|||
{my $k = int rand($n + 1); |
|||
@l[$k, $n] = @l[$n, $k] if $k != $n;}}</perl> |