Sort::Radix - A multiple passes distribution sort algorithm


Sort-Radix documentation Contained in the Sort-Radix distribution.

Index


Code Index:

NAME

Top

Sort::Radix - A multiple passes distribution sort algorithm

SYNOPSIS

Top

  use Sort::Radix;

  @array = qw(flow loop pool Wolf root sort tour);
  radix_sort(\@array);
  print "@array\n";

DESCRIPTION

Top

This is an implementation based on Jarkko's Wolf book (Mastering Algorithms with Perl, pp. 145-147).

By definition: radix sort is a multiple pass distribution sort algorithm that distributes each item to a bucket according to part of the item's key beginning with the least significant part of the key. After each pass, items are collected from the buckets, keeping the items in order, then redistribute according to the next most significant part of the key.

Radix sort is nice as it take N * M passes, where N is the length of the keys. It is very useful for sorting large volumes of keys of the same length, such as postal codes.

The algorithm will only works when the strings to be sorted are of the same length. Variable length strings therefore have to be padded with zeroes (\x00) to equalize the length.

BUGS

Top

Unknown so far. But please kindly inform if you find one ;-)

HISTORY

Top

Fixed warning caused by operator precedence and undefined error caused by misplacing the routines after __END__ marker.

SEE ALSO

Top

Sort::Merge, Sort::Fields

IMPLEMENTOR

Top

Edward Wijaya, <ewijaya@singnet.com.sg>

AUTHOR

Top

Jarkko Hietaniemi, <jhi@iki.fi>

COPYRIGHT AND LICENSE

Top


Sort-Radix documentation Contained in the Sort-Radix distribution.

package Sort::Radix;

use 5.008005;
use strict;
use warnings;

require Exporter;

our @ISA = qw(Exporter);
our @EXPORT = qw(radix_sort);

# Items to export into callers namespace by default. Note: do not export
# names by default without a very good reason. Use EXPORT_OK instead.
# Do not simply export all your public functions/methods/constants.

# This allows declaration	use Sort::Radix ':all';
# If you do not need this, moving things directly into @EXPORT or @EXPORT_OK
# will save memory.
our @EXPORT_OK = qw(_internalfunctions);

our %EXPORT_TAGS = (  all  => \@EXPORT,
                      test => \@EXPORT_OK,);

#our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } );



our $VERSION = '0.04';




# Preloaded methods go here.

sub radix_sort {
    my $array = shift;

    my $from = $array;
    my $to;

    # All lengths expected equal.
    for ( my $i = length ($array->[ 0 ]) - 1; $i >= 0; $i-- ) {
        # A new sorting bin.
        $to = [ ];
        foreach my $card ( @$from ) {
            # Stability is essential, so we use push().
            push @{ $to->[ ord( substr $card, $i ) ] }, $card;
        }

        # Concatenate the bins.

        $from = [ map { @{ $_ || [ ] } } @$to ];
    }

    # Now copy the elements back into the original array.

    @$array = @$from;
}

1;
__END__


#///////////////////////////////////////////////////////////////////////#
#                                                                       #
#///////////////////////////////////////////////////////////////////////#

# Below is stub documentation for your module. You'd better edit it!