Created
September 9, 2008 02:43
-
-
Save hakobe/9601 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env perl | |
use strict; | |
use warnings; | |
use Perl6::Say; | |
use Data::Dumper; | |
#use Test::More qw(no_plan); | |
sub p ($) { | |
say Dumper @_; | |
} | |
sub radix_sort { | |
my ($array, $keys) = @_; | |
for my $key (@$keys) { | |
my @bucket; | |
push @{ $bucket[$key->($_)] ||= [] }, $_ for @$array; | |
$array = [ map {@{$bucket[$_]}} grep {$bucket[$_]} (0..9) ]; | |
} | |
return $array; | |
} | |
p radix_sort([ 1, 14, 3, 25, 101, 5, 321, 9], [ | |
sub { $_[0] % 10 }, # 1 2|3| | |
sub { $_[0] / 10 % 10 }, # 1|2|3 | |
sub { $_[0] / 100 % 10 }, # |1|2 3 | |
]); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment