title: QuickSort in Perl
body: <p>I tried to implement QuickSort in Perl as I have done in Python and Ruby with the following code:</p>
  
  <pre><code>use strict;
  use warnings;
  
  sub sort {
      my ($lista, $p, $r) = @_;
      if ($p &lt; $r) {
          my $q = &amp;partition(\@$lista, $p, $r);
          &amp;sort(\@$lista, $p, $q - 1);
          &amp;sort(\@$lista, $q + 1, $r);
      }
  }
  
  sub partition {
      my ($lista, $p, $r) = @_;
      my $x = $$lista[$r];
      my $i = $p - 1;
      for (my $j = $p; $j &lt; @$lista - 1; $j++) {
          if ($$lista[$j] &lt;= $x) {
              $i++;
              ($$lista[$i], $$lista[$j]) = ($$lista[$j], $$lista[$i]);
          }
      }
      ($$lista[$i + 1], $$lista[$r]) = ($$lista[$r], $$lista[$i + 1]);
      return $i + 1;
  }
  
  my @lista = (4, 3, 9, 2, 1, 7, 5, 8);
  &amp;sort(\@lista, 0, $#lista);
  print @lista
  </code></pre>
  
  <p>In this case the execution falls in an infinite recursion and I don't know why because the code seems to be the same as in Python and Ruby (algorithm from Cormen, An Introduction to Algorithms)</p>
  
  <p>Note:
  If I try to execute:</p>
  
  <pre><code>my @lista = (3, 2, 1);
  &amp;sort(\@lista, 0, $#lista);
  print @lista;
  </code></pre>
  
  <p>The execution ends and the result is correct.</p>
  
  <p>Thanks in advance for your help.</p>