B
B McInnes
Hello, I am working on creating a perl implementatin of quick sort, I
know that there is a perl sort function but I am doing this so that I
can later sort a vec based on the information in another vec.
I have tried my program with: perl v5.8.0 for sun4-solaris and perl
v5.6.1 for MSWin32-x86-multithread.
I wrote an implementation of quicksort in C++ and it works. I then
wrote it in Perl and it doesn't work. I can not see what is wrong with
it. It does not seem to sort the entire list unless the list is
completely backwards (ie 9876...). Maybe I have been at if for to
long. Any help would be greatly appreciated. I pasted my perl code
below.
Thanks!
B
CODE:
#!/usr/local/bin/perl
#@stack = (); was using to see what was comming into q_sort
$N = 99;
for $i(0..$N-1) { $numbers[$i] = int(rand(9)); }
print "@numbers\n\n";
quickSort();
print "@numbers\n\n";
#print "@stack\n";
sub quickSort { &q_sort(0, $N); }
sub q_sort
{
$left = shift;
$right = shift;
#push @stack, $left;
#push @stack, $right;
$l = $left; $r = $right;
$pivot = $numbers[$left];
while ($left < $right)
{
while (($numbers[$right] >= $pivot) && ($left <
$right)) {
$right--;
}
if ($left != $right) {
$numbers[$left] = $numbers[$right]; $left++;
}
while (($numbers[$left] <= $pivot) && ($left <
$right)) {
$left++;
}
if ($left != $right) {
$numbers[$right] = $numbers[$left]; $right--;
}
}
$numbers[$left] = $pivot;
$pivot = $left;
$left = $l; $right = $r;
if ($left < $pivot) { &q_sort($left, $pivot-1); }
if ($right > $pivot) { &q_sort($pivot+1, $right); }
}
know that there is a perl sort function but I am doing this so that I
can later sort a vec based on the information in another vec.
I have tried my program with: perl v5.8.0 for sun4-solaris and perl
v5.6.1 for MSWin32-x86-multithread.
I wrote an implementation of quicksort in C++ and it works. I then
wrote it in Perl and it doesn't work. I can not see what is wrong with
it. It does not seem to sort the entire list unless the list is
completely backwards (ie 9876...). Maybe I have been at if for to
long. Any help would be greatly appreciated. I pasted my perl code
below.
Thanks!
B
CODE:
#!/usr/local/bin/perl
#@stack = (); was using to see what was comming into q_sort
$N = 99;
for $i(0..$N-1) { $numbers[$i] = int(rand(9)); }
print "@numbers\n\n";
quickSort();
print "@numbers\n\n";
#print "@stack\n";
sub quickSort { &q_sort(0, $N); }
sub q_sort
{
$left = shift;
$right = shift;
#push @stack, $left;
#push @stack, $right;
$l = $left; $r = $right;
$pivot = $numbers[$left];
while ($left < $right)
{
while (($numbers[$right] >= $pivot) && ($left <
$right)) {
$right--;
}
if ($left != $right) {
$numbers[$left] = $numbers[$right]; $left++;
}
while (($numbers[$left] <= $pivot) && ($left <
$right)) {
$left++;
}
if ($left != $right) {
$numbers[$right] = $numbers[$left]; $right--;
}
}
$numbers[$left] = $pivot;
$pivot = $left;
$left = $l; $right = $r;
if ($left < $pivot) { &q_sort($left, $pivot-1); }
if ($right > $pivot) { &q_sort($pivot+1, $right); }
}