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";
}


Sign In
Create Account

Back to top









