vinoth.ree has asked for the wisdom of the Perl Monks concerning the following question:
I was doing insertion sort and I was facing a problem, in the below code I used the 'lt' instead of '<' in for loop So I did not get the output properly, in this array first two elements 8 and 5 getting swapped and the for loop not proceed further When I used the '<' symbol in for its working fine but my question is for while loop this 'gt' working fine why not for?
use Data::Dumper; my ($i,$n,$j,@a,$temp); @a=(8,5,2,4,88,1,5,4,3,2); $n=10; for ($i=1; $i lt $n; $i++){ # print $i; $j=$i; $temp=$a[$j]; while ($j gt 0 and $a[$j-1] gt $temp) { $a[$j]=$a[$j-1]; $j--; } $a[$j]=$temp; } print Dumper(\@a);
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Insertion sort in perl
by almut (Canon) on Apr 25, 2009 at 11:07 UTC | |
by AnomalousMonk (Archbishop) on Apr 25, 2009 at 13:26 UTC | |
|
Re: Inertion sort in perl
by Marshall (Canon) on Apr 25, 2009 at 15:44 UTC | |
by Corion (Patriarch) on Apr 25, 2009 at 15:54 UTC | |
by Marshall (Canon) on Apr 25, 2009 at 17:15 UTC |