Shortcut for chapter specific information

Thursday, April 28, 2011

Why I fail so easily in my perl one-liner insertion sort?

I guess part of it is that the program is complicated and takes simplification.

For instance, one can reduce the two assignments in the outer loop to
$k=$A[$j]; 
$i=$j-1;
to ($k,$i)=($A[$j],$j-1);
and
$A[$i+1]=$A[$i];
$i--;
to ($A[$i+1],$i)=($A[$i],$i-1);

Also the while loop should be gone because it is always the loop require more inspection adn error checking.
This should shorten the loop and reduce the chance of error.

In the case of perl, it also seems to be a good idea to have less $ in the program.  Got to think about it.

No comments:

Post a Comment