473,432 Members | 1,629 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,432 software developers and data experts.

recursion with perl

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); }
}
Jul 19 '05 #1
4 5868
Ugo
On Sat, 01 Nov 2003 15:03:02 +0000, B McInnes wrote:
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.


You can use sort even with other rules for the comparison,
in such a way:
sort { # block of code examining the special variables $a and $b
# and returning -1, 0 or 1 with some rule of comparison
....
} # No punctation between the block and the list
@list_to_be_sorted ;

Example:
#!/usr/bin/perl
%age = ( Anne => 34,
Marco => 23,
Tamara => 31,
Susan => 26,
Alessandra => 16,
);
@name = keys %age;
@name = sort { $age{$a} <=> $age{$b} } @name;
print "People ordered by age:\n";
print "$_ is $age{$_} years old.\n" for @name;
__END__

--
Ugo
Jul 19 '05 #2
÷ ÐÉÓØÍÅ Sat, 01 Nov 2003 15:03:02 -0800, B McInnes ÎÁÐÉÓÁÌ:
Hello, I am working on creating a perl implementatin of quick sort, I ....
Thats good idea about removing stack, but next lines: $left = shift;
$right = shift; are also modification
.... $l = $left; $r = $right;
$pivot = $numbers[$left];
.... $numbers[$left] = $pivot;
$pivot = $left;
$left = $l; $right = $r;

if ($left < $pivot) { &q_sort($left, $pivot-1); } now $right can be something other if ($right > $pivot) { &q_sort($pivot+1, $right); }

....
I don't readed this example, so this is only first view tips.
I didn't wrote any recursion without stack of states, excep for
task where it isn't need. I think you can use $_[0] & $_[1] as $left &
$right.

--
With best wishes Nikolay
mail: ni*****@asu.ntu-kpi.kiev.ua
ICQ#: 136497739

Jul 19 '05 #3
In article <75**************************@posting.google.com >, B McInnes
<bt*******@yahoo.com> wrote:
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
By not declaring $left, $right, and $pivot as lexically local to the
q_sort subroutine with the "my" qualifier, they are global to your
program. Therefore, when they are modified by the first call to q_sort,
the values of $right and $pivot passed to q_sort in the second call are
not what they should be.

You should put "use strict;" at the beginning of your program to
prevent uninteded use of global variables. Then declare variables with
'our', 'local', or 'my' as appropriate or use package names

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;
my $left = shift;
$right = shift;
my $right = shift;

#push @stack, $left;
#push @stack, $right;

$l = $left; $r = $right;
$pivot = $numbers[$left];
my $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); }
}


FYI: this newsgroup is defunct. Use comp.lang.perl.misc in the future.

HTH.
Jul 19 '05 #4
Jim Gibson <jg*****@mail.arc.nasa.gov> wrote in message news:<031120031253405769%jg*****@mail.arc.nasa.gov >...

By not declaring $left, $right, and $pivot as lexically local to the
q_sort subroutine with the "my" qualifier, they are global to your
program. Therefore, when they are modified by the first call to q_sort,
the values of $right and $pivot passed to q_sort in the second call are
not what they should be.

You should put "use strict;" at the beginning of your program to
prevent uninteded use of global variables. Then declare variables with
'our', 'local', or 'my' as appropriate or use package names

Thank you! I am not feeling very bright right now. I really appreciate
your help!!

FYI: this newsgroup is defunct. Use comp.lang.perl.misc in the future.


Thanks for letting me know. I will post on the above one for now on.
Hopefully, I will have a more intelligent question when the time comes
:)

B
Jul 19 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Peri | last post by:
I'm trying to create Python parser/interpreter using ANTLR. Reading grammar from language refference I found: or_expr::= xor_expr | or_expr "|" xor_expr For me it looks like infinite recursion....
1
by: Tim Mohler | last post by:
All - I have a script that provides a web page interface to various system utilities. Once the user has selected the utility and input various parameters, it calls itself with a different method...
12
by: da Vinci | last post by:
Greetings. I want to get everyone's opinion on the use of recursion. We covered it in class tonight and I want a good solid answer from people in the "know" on how well recursion is accepted...
43
by: Lorenzo Villari | last post by:
I've tried to transform this into a not recursive version but without luck... #include <stdio.h> void countdown(int p) { int x;
75
by: Sathyaish | last post by:
Can every problem that has an iterative solution also be expressed in terms of a recursive solution? I tried one example, and am in the process of trying out more examples, increasing their...
13
by: Mumia W. | last post by:
Hello all. I have a C++ program that can count the YOYOs that are in a grid of Y's and O's. For example, this Y O Y O O Y O Y O Y O O Y O Y Y O Y O Y O O Y O O Y Y O Y O
20
by: athar.mirchi | last post by:
..plz define it.
1
by: sharathdotbv | last post by:
Hi all, Does perl supports recursive function calls ?
35
by: Muzammil | last post by:
int harmonic(int n) { if (n=1) { return 1; } else { return harmonic(n-1)+1/n; } } can any help me ??
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.