K
kokolo
Hi all.
I know Perl is not the best choice for resursive function calls but I wanted
to write a quicksort in Perl.
I used Memoize module to help me do it but I still get errors like:
_____________________________________________________________________
Deep recursion on anonymous subroutine at QuickSort.pl line 40, <STDIN> line
1.
Deep recursion on subroutine "Memoize::_memoizer" at (eval 2) line 1,
<STDIN> line 1.
Deep recursion on subroutine "main::qs" at C:/Perl/lib/Memoize.pm line 269,
<STDIN> line 1.
________________________________________________________________________
This happens when the array to be sorted has more than 60k elements and only
when run under Windows.
And I'm not very happy with the speed either, I hoped it would be way
faster.
Any help? The code 's below.
Thx
----------------------------------------------------------------------------
----------------------------------------------------------------------------
-----------------
#!/usr/bin/perl -w
use strict;
use Memoize;
memoize ('qs');
my @array;
my $size;
print "Enter the number of elements to be sorted: \n";
chomp($size = <STDIN>);
foreach (1..$size) {push @array, int(rand(1000))}
my $start = time();
@array = &qs(@array);
my $finish = time();
#print "The sorted array is:\n @array \n";
print "The array of $size elements QuickSorted in ",$finish - $start,"
seconds \n";
sub qs {
my @array = @_;
my $pivot = $#array;
my @smaller_numbers;
my @larger_numbers;
my $i;
for ($i=0;$i<$pivot;$i++){
if ($array[$i] <= $array[$pivot]){
unshift @smaller_numbers, $array[$i]
}
else{
push @larger_numbers, $array[$i]
}
}
if ($#smaller_numbers > 0){@smaller_numbers = &qs(@smaller_numbers)}
if ($#larger_numbers > 0) {@larger_numbers = &qs(@larger_numbers)}
push @smaller_numbers,($array[$pivot],@larger_numbers);
return @smaller_numbers;
}
I know Perl is not the best choice for resursive function calls but I wanted
to write a quicksort in Perl.
I used Memoize module to help me do it but I still get errors like:
_____________________________________________________________________
Deep recursion on anonymous subroutine at QuickSort.pl line 40, <STDIN> line
1.
Deep recursion on subroutine "Memoize::_memoizer" at (eval 2) line 1,
<STDIN> line 1.
Deep recursion on subroutine "main::qs" at C:/Perl/lib/Memoize.pm line 269,
<STDIN> line 1.
________________________________________________________________________
This happens when the array to be sorted has more than 60k elements and only
when run under Windows.
And I'm not very happy with the speed either, I hoped it would be way
faster.
Any help? The code 's below.
Thx
----------------------------------------------------------------------------
----------------------------------------------------------------------------
-----------------
#!/usr/bin/perl -w
use strict;
use Memoize;
memoize ('qs');
my @array;
my $size;
print "Enter the number of elements to be sorted: \n";
chomp($size = <STDIN>);
foreach (1..$size) {push @array, int(rand(1000))}
my $start = time();
@array = &qs(@array);
my $finish = time();
#print "The sorted array is:\n @array \n";
print "The array of $size elements QuickSorted in ",$finish - $start,"
seconds \n";
sub qs {
my @array = @_;
my $pivot = $#array;
my @smaller_numbers;
my @larger_numbers;
my $i;
for ($i=0;$i<$pivot;$i++){
if ($array[$i] <= $array[$pivot]){
unshift @smaller_numbers, $array[$i]
}
else{
push @larger_numbers, $array[$i]
}
}
if ($#smaller_numbers > 0){@smaller_numbers = &qs(@smaller_numbers)}
if ($#larger_numbers > 0) {@larger_numbers = &qs(@larger_numbers)}
push @smaller_numbers,($array[$pivot],@larger_numbers);
return @smaller_numbers;
}