#!/usr/bin/perl -w<br />use strict;<br />use 5.010;<br />sub getDigit($$){ # get digit from num<br /> my($num ,$flag) = @_;<br /> return $num/(10**$flag)%10;<br />}</p><p>sub radixSort($/@){<br /> my ($size,$arr) = @_;<br /> my @sortArr;<br /> my $i;my $k;<br /> for(0..3){<br /> my @count = undef;<br /> for $i (0..$size){<br /> my $digit = getDigit($arr->[$i], $_);<br /> $sortArr[$digit]->[$count[$digit]++]= $arr->[$i];<br /> }<br /> my $index = 0;<br /> for $i (0..9){<br /> next if !defined $count[$i];<br /> for $k (0..$count[$i]-1){<br /> $arr->[$index] = $sortArr[$i]->[$k];<br /> $index++;<br /> }<br /> }#for(0..2)<br /> }#for(1..3)<br />}</p><p>my @test_num = qw/49 38 65 937 19 76 13 27 2349/;<br />my $size = $#test_num ;<br />radixSort($size, @test_num);<br />say "@test_num";</p><p>本文來自CSDN部落格,轉載請標明出處:http://blog.csdn.net/cliffbaby/archive/2011/05/05/6396137.aspx