-
Notifications
You must be signed in to change notification settings - Fork 0
/
test-sort-bogosort.pl
executable file
·57 lines (45 loc) · 1.02 KB
/
test-sort-bogosort.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#!/usr/bin/env perl
use Data::Dumper;
use List::Util qw/shuffle/;
use Benchmark;
sub make_random {
my $n = shift;
my $top =
int( rand(5) ) +
$n * int( 2 + rand(17) ); # using prime numbers make me look good
my @a = ();
my $i = $n;
while ( $i-- ) {
push @a, int( rand($top) );
}
return \@a;
}
sub is_ordered {
my $a = shift;
for ( my $i = 1 ; $i < @{$a} ; $i++ ) {
return 0 if ( $a->[ $i - 1 ] > $a->[$i] );
}
return 1;
}
sub bogosort {
my $array = shift;
my @a = @{$array};
while ( !is_ordered( \@a ) ) {
@a = shuffle(@a);
}
return [@a];
}
sub a_fmt {
my @a = ref( $_[0] ) eq 'ARRAY' ? @{ $_[0] } : @_;
return '[ ' . join( q{, }, @a ) . " ]\n";
}
my $numelem = shift || die q{usage: test-sort-bogosort.pl <num_elem> [times]};
my $times = shift || 1;
timethis(
$times,
sub {
my $randomlist = make_random($numelem);
my $result = bogosort($randomlist);
#print a_fmt($result);
}
);