In this post i have written a code to sort an array.
Program Code:
#! /Perl/bin/perl
print("Enter elemnets of array to be sorted\n Enter an empty line to quit\n");
$count=1;
$inputline=<STDIN>;
chop($inputline);
while($inputline ne "")
{
@array[$count-1]=$inputline;
$count++;
$inputline=<STDIN>;
chop($inputline);
}
$count=1;
while($count<@array)
{
$x=1;
while($x<@array)
{
if($array[$x-1] gt $array[$x])
{
@array[$x-1,$x]=@array[$x,$x-1];
}
$x++;
}
$count++;
}
print("@array\n");
Ouput:#! /Perl/bin/perl
print("Enter elemnets of array to be sorted\n Enter an empty line to quit\n");
$count=1;
$inputline=<STDIN>;
chop($inputline);
while($inputline ne "")
{
@array[$count-1]=$inputline;
$count++;
$inputline=<STDIN>;
chop($inputline);
}
$count=1;
while($count<@array)
{
$x=1;
while($x<@array)
{
if($array[$x-1] gt $array[$x])
{
@array[$x-1,$x]=@array[$x,$x-1];
}
$x++;
}
$count++;
}
print("@array\n");
0 comments:
Post a Comment