File Coverage

blib/lib/Sort/Radix.pm
Criterion Covered Total %
statement 21 21 100.0
branch 2 2 100.0
condition n/a
subroutine 4 4 100.0
pod 0 1 0.0
total 27 28 96.4


line stmt bran cond sub pod time code
1             package Sort::Radix;
2              
3 1     1   5762 use 5.008005;
  1         4  
  1         48  
4 1     1   8 use strict;
  1         1  
  1         33  
5 1     1   6 use warnings;
  1         13  
  1         926  
6              
7             require Exporter;
8              
9             our @ISA = qw(Exporter);
10             our @EXPORT = qw(radix_sort);
11              
12             # Items to export into callers namespace by default. Note: do not export
13             # names by default without a very good reason. Use EXPORT_OK instead.
14             # Do not simply export all your public functions/methods/constants.
15              
16             # This allows declaration use Sort::Radix ':all';
17             # If you do not need this, moving things directly into @EXPORT or @EXPORT_OK
18             # will save memory.
19             our @EXPORT_OK = qw(_internalfunctions);
20              
21             our %EXPORT_TAGS = ( all => \@EXPORT,
22             test => \@EXPORT_OK,);
23              
24             #our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } );
25              
26              
27              
28             our $VERSION = '0.04';
29              
30              
31              
32              
33             # Preloaded methods go here.
34              
35             sub radix_sort {
36 1     1 0 1443 my $array = shift;
37              
38 1         4 my $from = $array;
39 1         2 my $to;
40              
41             # All lengths expected equal.
42 1         7 for ( my $i = length ($array->[ 0 ]) - 1; $i >= 0; $i-- ) {
43             # A new sorting bin.
44 4         7 $to = [ ];
45 4         15 foreach my $card ( @$from ) {
46             # Stability is essential, so we use push().
47 28         29 push @{ $to->[ ord( substr $card, $i ) ] }, $card;
  28         105  
48             }
49              
50             # Concatenate the bins.
51              
52 4 100       10 $from = [ map { @{ $_ || [ ] } } @$to ];
  467         396  
  467         1471  
53             }
54              
55             # Now copy the elements back into the original array.
56              
57 1         9 @$array = @$from;
58             }
59              
60             1;
61             __END__