On Jan 24, sanilkumar said:

>> for($i=0;$i<10;$i++)
>> {
>>      print("Enetr the $i num: ");
>>      $a[$i]=<STDIN>;
>>      print("@a[$i]");

This code wasn't written with 'strict' or '-w' -- two bad moves.

Why'd you use $a[$i] in once place and @a[$i] in the other?  You should
always use $a[$i] when referring to a single element.

>> }
>> print("@a");
>>
>> $first=0;
>> $last=10;
>> quicksort(@a,$first,$last);

You're calling the quicksort() function before Perl has seen its
definition, and thus, before Perl has seen its prototype.  If you're going
to use prototyped functions, they must be declared beforehand.  Add the
line

  sub quicksort (\@$$);

at the top of your program.  Better yet, DON'T USE PROTOYPES.  Pass the
array in as a reference YOURSELF.

  quicksort(\@a, $first, $last);

Prototypes are magical spells that are rarely understood and even more
rarely used CORRECTLY.  Don't play with them.

>> print("@a");
>>
>>
>> sub quicksort(\@$$)

Get rid of the prototype.

>> {
>>      local(@num,$fi,$la)=@_;

This puts all three elements in @num, and nothing in $fi or $la.  I also
don't know why you're using local instead of my.  The first argument to
your function is a reference to an array, not an array.

  my ($num, $fi, $la) = @_;

>>      $ipivot=@num[$fi];
>>      $spl=splitpoint(@num,$fi,$la,$pivot);

You called it $ipivot when you assigned to it, but $pivot later.  If you
were using strict or -w, you'd have caught this a long time ago.

  # $num->[...] because $num is a reference to an array
  my $pivot = $num->[$fi];
  my $spl = splitpoint($num, $fi, $la, $pivot);

You'll notice that the first argument to splitpoint() is not an array
anymore, but an array reference.  Since it would be more effort than it is
worth to MAKE an array from $num, we're just going to keep using $num.  So
what does this mean for splitpoint()?  It needs to prototype, because
there's no magic involved with calling it.

Also, you use $pivot once.  Why make it at all?!

  my $spl = splitpoint($num, $fi, $la, $num->[$fi]);

>>      $wi=spl-1;
>>      $ww=$spl+1;

Typo there.  Notice how those two lines SHOULD line up, but don't?  You're
missing a $.  And whitespace is your friend, man.

  my $wi = $spl - 1;
  my $ww = $spl + 1;

>>      quicksort(@num,$fi,$wi);
>>      quicksort(@num,$ww,$la);

  quicksort($num, $fi, $wi);
  quicksort($num, $ww, $la);

>> }
>> sub splitpoint(\@$$$)

Again, lose the prototype.

>> {
>>      local(@nu,$fn,$ln,$pi)=@_;

Sam problem here with @nu.  The first argument is a reference to an array,
not the array itself.

  my ($num, $fn, $ln, $pi) = @_;

>>      while($fn < $ln)
>>      {
>>              if(@nu[$fn] < $pi)
>>              {
>>                      $fn=$fn+1;
>>              }

These three lines could be condensed to:

  $fn++ if $num->[$fn] < $pi;

>>              if(@nu[$ln] > $pi)
>>              {
>>                      $ln=$ln-1;
>>              }

Likewise:

  $ln-- if $num->[$ln] > $pi;

>>              $tm=@nu[$fn];
>>              @nu[$fn]=@nu[$ln];
>>              @nu[$ln]=$tm;

Perl does not require temporary variables for swapping two values.

  @{ $num }[$fn, $ln] = @{ $num }[$ln, $fn];

Granted, the slicing syntax looks better with a real array:

  @array[0,1] = @array[1,0];

but the concept remains the same:

  ($x, $y) = ($y, $x);

will properly swap two values.

>>      }
>> return($fn);
>> }


-- 
Jeff "japhy" Pinyan      [EMAIL PROTECTED]      http://www.pobox.com/~japhy/
RPI Acacia brother #734   http://www.perlmonks.org/   http://www.cpan.org/
** Look for "Regular Expressions in Perl" published by Manning, in 2002 **
<stu> what does y/// stand for?  <tenderpuss> why, yansliterate of course.


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to