in reply to 52 Perl Pickup
The inner loop can be replaced with a hash to change the algorithm from O(N2) to O(N), a major improvement.
my @groups; my %in_num_groups; while (@pile) { my $item = shift @pile; push @{$groups[$in_num_groups{$item}++]}, $item; }
I also fixed the needless use of a reference to the array of groups and the inability to store false values in @pile.
Update: For fun, did you know @pile could also be initialized using
@pile = shuffle( (@card) x $n );
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^2: 52 Perl Pickup
by dogz007 (Scribe) on Aug 02, 2007 at 06:10 UTC |