Skip to content

Instantly share code, notes, and snippets.

@hakobe
Created September 9, 2008 02:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hakobe/9601 to your computer and use it in GitHub Desktop.
Save hakobe/9601 to your computer and use it in GitHub Desktop.
#!/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