Jump to content


Check out our Community Blogs

hsincredible

Member Since 31 May 2011
Offline Last Active May 31 2011 05:29 AM
-----

Topics I've Started

Quick sort help in perl

31 May 2011 - 05:29 AM

can anyone please help... whats wrong with my algo here : its a perl program for quick-sort. O/P is wrong
sample i/p:
6
3
8
1
6
5
4
o/p:
1
1
4
4
4
4

#!/usr/bin/perl
my $t=<STDIN>;
int($t);
my @n;
while($t)
{
        push(@n,int(<STDIN>));
        $t--;
}
&quick(0,$#n);
sub quick
{
  if($_[0]<$_[1])
  {
        my $pivot = $n[$_[1]];
        my $from = $_[0];
        my $to = $_[1];
        my $i=$from-1;
        my $j=$from;
        my $temp;
        while($j<$to)
        {
                if($n[$j]<=$pivot)
                {
                        $i=$i+1;
                        $temp=$n[$i];
                        $n[$i]=$n[$j];
                        $n[$j]=$temp;
                }
                $j=$j+1;
        }
        $i=$i+1;
        $temp=$n[$i];
        $n[$i]=$pivot;
        $pivot=$n[$i];
        &quick($from,$i-1);
        &quick($i+1,$to);
  }
}
foreach(@n)
{
        print $_."\n";
}

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download